Kea Mokoena Assessment 002 - Submission#2
Conversation
There was a problem hiding this comment.
Pull request overview
This is a student assessment submission implementing three analytics functions for list processing operations. The implementations attempt to solve problems related to filtering sales data, counting product codes, and calculating moving averages.
Key Changes:
- Implementation of
filter_sales_above_threshold()function with logic issues - Implementation of
count_product_codes()function that ignores the prefix parameter - Implementation of
calculate_moving_average()function with multiple calculation errors
Reviewed changes
Copilot reviewed 2 out of 4 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| analytics.py | Added implementations for three analytics functions (filter_sales_above_threshold, count_product_codes, calculate_moving_average) with multiple logic errors and debug print statements |
| .vscode/settings.json | Added VS Code Python testing configuration for unittest |
| pycache/test_analytics.cpython-313.pyc | Compiled Python bytecode (auto-generated) |
| pycache/analytics.cpython-313.pyc | Compiled Python bytecode (auto-generated) |
Comments suppressed due to low confidence (3)
analytics.py:26
- Print statement may execute during import.
print(filter_sales_above_threshold([200, 500, 75], 100))
analytics.py:52
- Print statement may execute during import.
print(count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD"))
analytics.py:76
- Print statement may execute during import.
print(calculate_moving_average([40, 50], 3))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for _ in numbers: | ||
| if len(numbers) < window_size: | ||
| average = sum(numbers) / window_size | ||
| else: | ||
| average = sum(numbers[-1]) / window_size | ||
| return average |
There was a problem hiding this comment.
The function doesn't round the result to 2 decimal places as required by the specifications. The return statement should use round(average, 2).
| for _ in numbers: | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / window_size | |
| else: | |
| average = sum(numbers[-1]) / window_size | |
| return average | |
| if not numbers: | |
| return 0.0 | |
| window = numbers[-window_size:] if len(numbers) >= window_size else numbers | |
| average = sum(window) / len(window) | |
| return round(average, 2) |
| else: | ||
| return [] | ||
| return sales | ||
| print(filter_sales_above_threshold([200, 500, 75], 100)) |
There was a problem hiding this comment.
Debug print statement should be removed. This appears to be test/debug code that shouldn't be in the final submission.
| print(filter_sales_above_threshold([200, 500, 75], 100)) |
| if code.startswith("SERV"): | ||
| count += 1 | ||
| elif code.startswith("PROD"): | ||
| count += 1 |
There was a problem hiding this comment.
The function ignores the prefix parameter and hardcodes checks for "SERV" and "PROD". This should use code.startswith(prefix) instead to count codes that start with the provided prefix parameter.
| if code.startswith("SERV"): | |
| count += 1 | |
| elif code.startswith("PROD"): | |
| count += 1 | |
| if code.startswith(prefix): | |
| count += 1 |
| for _ in numbers: | ||
| if len(numbers) < window_size: | ||
| average = sum(numbers) / window_size | ||
| else: | ||
| average = sum(numbers[-1]) / window_size | ||
| return average |
There was a problem hiding this comment.
sum(numbers[-1]) attempts to sum a single integer, which will cause a TypeError. This should be sum(numbers[-window_size:]) to get the sum of the last window_size elements.
| for _ in numbers: | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / window_size | |
| else: | |
| average = sum(numbers[-1]) / window_size | |
| return average | |
| if not numbers: | |
| return 0.0 | |
| window = numbers[-window_size:] if window_size > 0 else [] | |
| if not window: | |
| return 0.0 | |
| average = sum(window) / len(window) | |
| return round(average, 2) |
| print(calculate_moving_average([40, 50], 3)) | ||
|
|
There was a problem hiding this comment.
Debug print statement should be removed. This appears to be test/debug code that shouldn't be in the final submission.
| print(calculate_moving_average([40, 50], 3)) |
| for amount in sales: | ||
| if threshold > amount: | ||
| sales.append(amount) | ||
| else: | ||
| return [] | ||
| return sales |
There was a problem hiding this comment.
Returning an empty list when the first element exceeds the threshold is incorrect. The function should continue checking all elements and only return an empty list if no elements meet the criteria. This early return prevents the function from processing the entire list.
| for amount in sales: | |
| if threshold > amount: | |
| sales.append(amount) | |
| else: | |
| return [] | |
| return sales | |
| result = [amount for amount in sales if amount > threshold] | |
| return result |
| elif code.startswith("PROD"): | ||
| count += 1 | ||
| return count | ||
| print(count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD")) |
There was a problem hiding this comment.
Debug print statement should be removed. This appears to be test/debug code that shouldn't be in the final submission.
| print(count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD")) |
| for _ in numbers: | ||
| if len(numbers) < window_size: | ||
| average = sum(numbers) / window_size | ||
| else: | ||
| average = sum(numbers[-1]) / window_size | ||
| return average |
There was a problem hiding this comment.
The loop unnecessarily iterates over all elements but doesn't use the iteration variable meaningfully. The average is recalculated on every iteration with the same result. The logic should calculate the average once, not in a loop.
| for _ in numbers: | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / window_size | |
| else: | |
| average = sum(numbers[-1]) / window_size | |
| return average | |
| if not numbers: | |
| return 0.0 | |
| window = numbers[-window_size:] if len(numbers) >= window_size else numbers | |
| average = sum(window) / len(window) | |
| return round(average, 2) |
| for _ in numbers: | ||
| if len(numbers) < window_size: | ||
| average = sum(numbers) / window_size | ||
| else: | ||
| average = sum(numbers[-1]) / window_size | ||
| return average |
There was a problem hiding this comment.
When len(numbers) < window_size, dividing by window_size instead of len(numbers) produces an incorrect average. According to the requirements, if the list has fewer elements than window_size, the average should use all available elements, so the divisor should be len(numbers).
| for _ in numbers: | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / window_size | |
| else: | |
| average = sum(numbers[-1]) / window_size | |
| return average | |
| if not numbers: | |
| return 0.0 | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / len(numbers) | |
| else: | |
| average = sum(numbers[-window_size:]) / window_size | |
| return round(average, 2) |
| for _ in numbers: | ||
| if len(numbers) < window_size: | ||
| average = sum(numbers) / window_size | ||
| else: | ||
| average = sum(numbers[-1]) / window_size | ||
| return average |
There was a problem hiding this comment.
The function doesn't handle the edge case of an empty list, which should return 0.0 according to the requirements. If numbers is empty, the function will fail when trying to return average since it won't be defined.
| for _ in numbers: | |
| if len(numbers) < window_size: | |
| average = sum(numbers) / window_size | |
| else: | |
| average = sum(numbers[-1]) / window_size | |
| return average | |
| if not numbers: | |
| return 0.0 | |
| window = numbers[-window_size:] if len(numbers) >= window_size else numbers | |
| average = sum(window) / len(window) | |
| return round(average, 2) |
No description provided.