-
Notifications
You must be signed in to change notification settings - Fork 27
Cebolwethu mzanywa test #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,13 @@ def sum_of_squares(n: int): | |||||||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||||||||
| ValueError: If n is a negative integer. | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| sum = 0 | ||||||||||||||||||||||||||||
| for i in range(1, n + 1): | ||||||||||||||||||||||||||||
| sum += i **2 | ||||||||||||||||||||||||||||
| return sum | ||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||
| return f"Enter a postive number" | ||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+21
|
||||||||||||||||||||||||||||
| 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 |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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" |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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" |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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} |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error: this checks for multiples of 3, not odd numbers. Should be if i % 2 != 0: to identify odd numbers.
| if i%3 == 0: | |
| if i % 2 != 0: |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function doesn't implement the documented behavior. According to the docstring, it should sum numbers "up to (but not including) the first zero", but this implementation sums all numbers including zeros. Need to add a break condition when encountering zero: if i == 0: break.
| for i in nums: | |
| for i in nums: | |
| if i == 0: | |
| break |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'postive' to 'positive'.