Cebolwethu mzanywa test#4
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements multiple Python functions that were previously placeholders (pass statements). The changes add implementations for mathematical calculations, list/dictionary processing, and basic algorithm problems.
Key Changes:
- Implemented mathematical functions (sum of squares, finding median, checking prime numbers)
- Added data processing functions (filtering, counting, averaging)
- Implemented control flow functions (break/continue patterns, iteration logic)
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| sum += i **2 | ||
| return sum | ||
| except ValueError: | ||
| return f"Enter a postive number" |
There was a problem hiding this comment.
Corrected spelling of 'postive' to 'positive'.
| return f"Enter a postive number" | |
| return f"Enter a positive number" |
| try: | ||
| sum = 0 | ||
| for i in range(1, n + 1): | ||
| sum += i **2 | ||
| return sum | ||
| except ValueError: | ||
| return f"Enter a postive number" |
There was a problem hiding this comment.
The try-except block doesn't match the documented behavior. The function should raise ValueError for negative integers, but the current implementation will never trigger the except block since range() doesn't raise ValueError. Input validation should check if n < 0 and raise ValueError explicitly.
| try: | |
| sum = 0 | |
| for i in range(1, n + 1): | |
| sum += i **2 | |
| return sum | |
| except ValueError: | |
| return f"Enter a postive number" | |
| if n < 0: | |
| raise ValueError("n must be a non-negative integer") | |
| sum = 0 | |
| for i in range(1, n + 1): | |
| sum += i **2 | |
| return sum |
| count += i | ||
|
|
||
| if count/len(grades) >= min_pass: | ||
| return f"pass" |
There was a problem hiding this comment.
Using f-string for a static string is unnecessary. Return the plain string "Pass" instead (note: should be capitalized "Pass" to match the docstring specification).
| return f"pass" | |
| return "Pass" |
| return f"pass" | ||
| else: | ||
| return f"fail" |
There was a problem hiding this comment.
Using f-string for a static string is unnecessary. Return the plain string "Fail" instead (note: should be capitalized "Fail" to match the docstring specification).
| return f"pass" | |
| else: | |
| return f"fail" | |
| return "Pass" | |
| else: | |
| return "Fail" |
| my_dict[average_score] = list_below_average | ||
|
|
||
| return my_dict | ||
|
|
There was a problem hiding this comment.
The return dictionary structure is incorrect. According to the docstring, it should return a dictionary with keys like "average_score" and "below_average", not use the numeric average_score value as a key. Expected structure: {"average_score": average_score, "below_average": list_below_average}.
| my_dict[average_score] = list_below_average | |
| return my_dict | |
| return {"average_score": average_score, "below_average": list_below_average} |
| """ | ||
| pass | ||
| sum = 0 | ||
| for i in tuple(elements): |
There was a problem hiding this comment.
Converting to tuple doesn't remove duplicates as required by the function's purpose. Use set(elements) instead of tuple(elements) to sum only unique elements.
| for i in tuple(elements): | |
| for i in set(elements): |
| """ | ||
| pass | ||
| my_list = [] | ||
| for i in range(1, length): |
There was a problem hiding this comment.
Range is off by one. Should be range(1, length + 1) to include all integers from 1 to length as specified in the docstring.
| for i in range(1, length): | |
| for i in range(1, length + 1): |
| my_dict = {} | ||
| for word in words: | ||
| count = words.count(word) | ||
| my_dict[word] = count | ||
|
|
||
| return my_dict |
There was a problem hiding this comment.
Calling words.count(word) inside a loop over the same list results in O(n²) time complexity. Use a more efficient approach by incrementing counts in a single pass through the list.
| my_dict = {} | |
| for word in words: | |
| count = words.count(word) | |
| my_dict[word] = count | |
| return my_dict | |
| from collections import Counter | |
| my_dict = Counter(words) | |
| return dict(my_dict) |
| max_num = 0 | ||
| for num in nums: | ||
| if max_num < num: | ||
| max_num = num | ||
|
|
There was a problem hiding this comment.
Initializing max_num to 0 fails for lists containing only negative numbers. For an empty list, the function returns 0 instead of None as documented. Should check if list is empty first and return None, otherwise initialize max_num to the first element or use max(nums).
| max_num = 0 | |
| for num in nums: | |
| if max_num < num: | |
| max_num = num | |
| if not nums: | |
| return None | |
| max_num = nums[0] | |
| for num in nums[1:]: | |
| if max_num < num: | |
| max_num = num |
| for i in range(2, n): | ||
| if n % i != 0: | ||
| return True | ||
| else: | ||
| return False |
There was a problem hiding this comment.
Prime number logic is incorrect. The function returns True on the first non-divisor, which is wrong. It should check all numbers from 2 to n-1 (or sqrt(n) for efficiency) and only return True if none divide n evenly. Also, numbers less than 2 should return False.
| for i in range(2, n): | |
| if n % i != 0: | |
| return True | |
| else: | |
| return False | |
| if n < 2: | |
| return False | |
| for i in range(2, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| return False | |
| return True |
No description provided.