Skip to content
Go back

Class Cancelled: Lambda and Filter for Attendance Control

Published:  at  03:02 PM

Have you ever wondered how a simple script can save a Calculus professor from an empty classroom? ⏰ Discover how punctuality, or the lack thereof, can be automated with Python.

🔮 Problem Statement

A Calculus Integral professor, frustrated by chronic tardiness, has decided to automate class cancellation. The class will be cancelled if a minimum number of students are not present on time. Your mission is to write a function that determines whether the class is cancelled based on the students’ arrival times.

Parameters:

Return Value:

Example:

>>> cancelled_class(3, [-1, 0, 2, 3, 4])
'YES'
>>> cancelled_class(2, [-2, 0, 1, 2])
'NO'

Note: A student is considered on time if their arrival time is less than or equal to 0.

🧩 Step-by-Step Solution

Let’s break down this problem into manageable parts. First, we need to identify the students who arrived on time.

Filtering On-Time Students:

To do this, we use the filter function along with an anonymous function lambda. The lambda function acts as a filter, selecting only those arrival times that are less than or equal to 0, that is, the punctual or early students. We convert the result of filter into a list to facilitate its manipulation.

puntuals = list(filter(lambda x: x <= 0, arrivals))

Determining Cancellation:

Once we have the list of on-time students, we simply compare its length with the minimum required number of students (min_students). If the number of on-time students is less than min_students, the class is cancelled.

return 'NO' if len(puntuals) >= min_students else 'YES'

Complete Solution:

def cancelled_class(min_students, arrivals):
	"level: easy; points: 3"
	puntuals = list(filter(lambda x: x <= 0, arrivals))
	return 'NO' if len(puntuals) >= min_students else 'YES'

🧠 Key Concepts

This exercise illustrates several fundamental concepts in Python’s functional programming. 🪄

Higher-order functions, such as filter, take other functions as arguments. This allows you to create more flexible and reusable code. In this case, filter applies the lambda function to each element of the arrivals list, returning an iterator with the results.

lambda functions are anonymous functions, that is, functions that are not bound to a specific identifier. They are useful for simple and concise operations, especially when used with higher-order functions. Their syntax lambda arguments: expression allows you to define a function in a single line.

The filter function is a powerful tool for selecting elements in a sequence based on a condition. Unlike list comprehensions, filter returns an iterator, which can be more memory efficient for very large sequences.

Conditional expressions (also known as ternary operators) allow you to write conditional code in a single line, making the code more concise and readable. In this case, the conditional expression returns ‘NO’ if the class is not cancelled, and ‘YES’ if it is cancelled.

Did you know…? The efficiency of filter can vary depending on the length of the arrivals list and the complexity of the lambda function. In some cases, a list comprehension might be more efficient, especially if the lambda function is very complex.

💫 Final Thoughts

This solution, although simple, highlights Python’s ability to address problems with elegance and efficiency. A possible improvement would be to include error handling, such as validating that min_students is a positive integer and that arrivals is actually a list. In addition, the functionality could be extended to send automatic notifications to students in case of cancellation.

This task reminds us that programming is not only about solving problems, but also about optimizing processes and making life easier (for Calculus professors, at least!). 👨‍🏫

Did you enjoy this journey through programmed punctuality? If you want to explore more creative solutions and unravel the mysteries of code, don’t hesitate to explore other articles on our blog! See you in the next byte! 🚀



Previous Post
Counting Elements in a Python List: A Step-by-Step Guide
Next Post
Prime Function in Python: Optimization and Divisibility