-
Notifications
You must be signed in to change notification settings - Fork 10
Fixed Issues #2
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?
Fixed Issues #2
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,55 +1,40 @@ | ||
| def add(a, b): | ||
| # ISSUE 1: This function incorrectly handles negative numbers | ||
| if a < 0 or b < 0: | ||
| return abs(a) + abs(b) # BUG: Should preserve negatives | ||
| return a + b | ||
|
|
||
| def subtract(a, b): | ||
| return a - b # This one is correct | ||
|
|
||
| def multiply(a, b): | ||
| # ISSUE 2: This function has performance issues with large numbers | ||
| result = 0 | ||
| if b > 0: | ||
| for i in range(b): | ||
| result += a | ||
| elif b < 0: | ||
| for i in range(abs(b)): | ||
| result -= a | ||
| return result | ||
| return a * b | ||
|
|
||
| def divide(a, b): | ||
| # ISSUE 3: This function doesn't handle division by zero properly | ||
| if b == 0: | ||
| return float('inf') # BUG: Should raise an exception instead | ||
| raise ZeroDivisionError("Cannot divide by zero") # BUG: Should raise an exception instead | ||
| return a / b | ||
|
Comment on lines
12
to
16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correctness: |
||
|
|
||
| def power(base, exponent): | ||
| # ISSUE 4: This function has multiple bugs | ||
| if exponent == 0: | ||
| return 1 | ||
| elif exponent < 0: | ||
| # BUG: Doesn't handle negative exponents correctly | ||
| return 1 / power(base, exponent) | ||
|
|
||
| result = 1 | ||
| for i in range(exponent): | ||
| result *= base | ||
| return result | ||
| return base ** exponent | ||
|
Comment on lines
18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correctness: |
||
|
|
||
| def factorial(n): | ||
| # ISSUE 5: This function has recursion issues | ||
| if n < 0: | ||
| return -1 # BUG: Should raise ValueError for negative inputs | ||
| if not isinstance(n, int) or n < 0: | ||
| raise ValueError("Factorial is only defined for non-negative integers") | ||
| if n == 0: | ||
| return 1 | ||
| return n * factorial(n - 1) | ||
| return 1 # Base case to stop recursion | ||
| else: | ||
| return n * factorial(n - 1) | ||
|
Comment on lines
22
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correctness: |
||
|
|
||
| import math | ||
| def is_prime(n): | ||
| # ISSUE 6: This function has logic errors | ||
| if n < 2: | ||
| return False | ||
| for i in range(2, n): | ||
| # Check for divisors from 2 up to the square root of n | ||
| for i in range(2, int(math.sqrt(n)) + 1): | ||
| if n % i == 0: | ||
| return False | ||
| return True | ||
| return False # Found a divisor, so it's not prime | ||
| return True # No divisors were found, so it is prime | ||
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.
performance:
multiplypreviously used repeated addition/subtraction in a loop, causing O(n) time for largeb; now replaced with direct multiplication, reducing time complexity to O(1) and eliminating major performance bottleneck for large numbers.🤖 AI Agent Prompt for Cursor/Windsurf