-
Notifications
You must be signed in to change notification settings - Fork 27
Ntsika Test Done #3
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 | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,31 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import random | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| """Learning Outcome: Functions""" | ||||||||||||||||||||||||||||||||||||||||||||||
| def sum_of_squares(n: int): | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| Calculate the sum of the squares of all integers from 1 to n. | ||||||||||||||||||||||||||||||||||||||||||||||
| total = 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||
| if n == 0: | ||||||||||||||||||||||||||||||||||||||||||||||
| return n | ||||||||||||||||||||||||||||||||||||||||||||||
| elif n > 0: | ||||||||||||||||||||||||||||||||||||||||||||||
| for _ in range(1, n + 1): | ||||||||||||||||||||||||||||||||||||||||||||||
| sum = n * n | ||||||||||||||||||||||||||||||||||||||||||||||
| total += sum | ||||||||||||||||||||||||||||||||||||||||||||||
| return sum | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| return sum | |
| return total |
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.
Bare except clause catches all exceptions including system exits. Should specify the expected exception type (e.g., except TypeError:) or remove the try-except block entirely since no exception-raising code is present in the try block.
| try: | |
| if n == 0: | |
| return n | |
| elif n > 0: | |
| for _ in range(1, n + 1): | |
| sum = n * n | |
| total += sum | |
| return sum | |
| except: | |
| raise ValueError | |
| if n == 0: | |
| return n | |
| elif n > 0: | |
| for _ in range(1, n + 1): | |
| sum = n * n | |
| total += sum | |
| 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.
Unnecessary loop to count list elements. Replace with num_of_grades = len(grades) for clearer and more efficient code.
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.
Uses global variable count_positive_numbers_nums instead of the function parameter nums. Should iterate over nums parameter instead.
| for i in count_positive_numbers_nums: | |
| for i in nums: |
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.
Counts zero as positive, but zero is neither positive nor negative. The condition should be if i > 0: to count only positive numbers as indicated by the function name.
| if i >= 0: | |
| if i > 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.
Uses global variable sum_unique_elements_nums instead of the function parameter elements. Should use set(elements) instead.
| sum_unique_elements_set = set(sum_unique_elements_nums) | |
| sum_unique_elements_set = 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.
Uses global variable list_of_square_num instead of the function parameter nums. Should iterate over nums parameter instead.
| for i in list_of_square_num: | |
| for i in nums: |
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.
Uses global variable sum_and_average_list instead of the function parameter nums. Should iterate over nums parameter instead.
| for i in sum_and_average_list: | |
| total += i | |
| average = total / len(sum_and_average_list) | |
| for i in nums: | |
| total += i | |
| average = total / len(nums) |
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.
Uses global variable sum_and_average_list instead of the function parameter nums. Should use len(nums) instead.
| for i in sum_and_average_list: | |
| total += i | |
| average = total / len(sum_and_average_list) | |
| for i in nums: | |
| total += i | |
| average = total / len(nums) |
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.
Returns a set instead of a tuple as specified in the docstring. Should return (total, average) or tuple(new_list) instead of converting to a set, which loses ordering.
| new_list = [] | |
| for i in sum_and_average_list: | |
| total += i | |
| average = total / len(sum_and_average_list) | |
| new_list.append(total) | |
| new_list.append(average) | |
| sum_and_average_set = set(new_list) | |
| return sum_and_average_set | |
| for i in sum_and_average_list: | |
| total += i | |
| average = total / len(sum_and_average_list) | |
| return (total, 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.
Uses global variable filtering_nums instead of the function parameter nums. Should iterate over nums parameter instead.
| for i in filtering_nums: | |
| for i in nums: |
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.
Uses global variable largest_number_nums instead of the function parameter nums. Should iterate over nums parameter instead.
| for i in largest_number_nums: | |
| for i in nums: |
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.
Initialization to 0 fails for lists containing only negative numbers. Should initialize to nums[0] or float('-inf') to handle all cases correctly, or return None for empty lists as per docstring.
| largest_num = 0 | |
| for i in largest_number_nums: | |
| if i > largest_num: | |
| largest_num = i | |
| # print(largest_num) | |
| if not nums: | |
| return None | |
| largest_num = nums[0] | |
| for i in nums: | |
| if i > largest_num: | |
| largest_num = i |
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 is inverted. The condition checks if i is divisible by n, but should check if n is divisible by i (i.e., if n % i == 0:). This makes the function always return True for n > 2.
| if i % n == 0: | |
| return False | |
| else: | |
| return True | |
| if n % i == 0: | |
| return False | |
| return True | |
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.
Returns immediately after checking only the first divisor (2). The function should continue checking all divisors and only return True after the loop completes without finding any divisors.
| for i in range(2, n): | |
| if i % n == 0: | |
| return False | |
| else: | |
| return True | |
| if n < 2: | |
| return False | |
| for i in range(2, n): | |
| 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.
The loop variable is unused and the calculation is incorrect. Line 12 should use the loop variable (e.g.,
i) instead ofn, and should calculatei * i. Currently, this addsn * na total ofntimes, producingn^3instead of the sum of squares.