Ntsika Test Done#3
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements multiple Python functions for learning outcomes related to functions, loops, data processing, and simple algorithms. The changes replace placeholder pass statements with working implementations across various utility functions.
Key changes:
- Implementation of mathematical functions (sum_of_squares, sum_multiples_of_num)
- Implementation of list filtering and processing functions (even_numbers, odd_numbers, filter_even_numbers)
- Implementation of algorithm functions (largest_number, is_prime, reverse_string)
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| for _ in range(1, n + 1): | ||
| sum = n * n | ||
| total += sum | ||
| return sum |
There was a problem hiding this comment.
The loop variable is unused and the calculation is incorrect. Line 12 should use the loop variable (e.g., i) instead of n, and should calculate i * i. Currently, this adds n * n a total of n times, producing n^3 instead of the sum of squares.
| for _ in range(1, n + 1): | |
| sum = n * n | |
| total += sum | |
| return sum | |
| for i in range(1, n + 1): | |
| total += i * i | |
| return total |
| for _ in range(1, n + 1): | ||
| sum = n * n | ||
| total += sum | ||
| return sum |
There was a problem hiding this comment.
Returns the wrong variable. Should return total instead of sum to provide the accumulated sum of squares.
| return sum | |
| return total |
| 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 |
There was a problem hiding this comment.
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 |
| for _ in range(len(grades)): | ||
| num_of_grades += 1 |
There was a problem hiding this comment.
Unnecessary loop to count list elements. Replace with num_of_grades = len(grades) for clearer and more efficient code.
| pass | ||
| count = 0 | ||
|
|
||
| for i in count_positive_numbers_nums: |
There was a problem hiding this comment.
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: |
| pass | ||
| even_nums = [] | ||
|
|
||
| for i in filtering_nums: |
There was a problem hiding this comment.
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: |
| largest_num = 0 | ||
|
|
||
| for i in largest_number_nums: | ||
| if i > largest_num: | ||
| largest_num = i | ||
| # print(largest_num) |
There was a problem hiding this comment.
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 |
| pass | ||
| largest_num = 0 | ||
|
|
||
| for i in largest_number_nums: |
There was a problem hiding this comment.
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: |
| if i % n == 0: | ||
| return False | ||
| else: | ||
| return True | ||
|
|
There was a problem hiding this comment.
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 | |
| for i in range(2, n): | ||
| if i % n == 0: | ||
| return False | ||
| else: | ||
| return True | ||
|
|
There was a problem hiding this comment.
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 |
No description provided.