Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/incrementaltest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Solution:
def twoSum(self, nums, target):
mapp = {}

for num in nums:
if target - num in mapp:
return [num, mapp[target - num]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

This line is missing the closing bracket ], making it a syntax error that prevents the entire file from being parsed.

Details

📖 Explanation: The list literal is never closed, causing a SyntaxError at runtime.

Suggested change
return [num, mapp[target - num]
return [num, mapp[target - num]]

Uses AI. Verify results. Give Feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

Missing a closing bracket ], causing a SyntaxError; it should be return [num, mapp[target - num]].

Details

📖 Explanation: The return statement on line 7 is missing its closing bracket, making this a syntax error that will prevent the entire module from loading.

Suggested change
return [num, mapp[target - num]
return [num, mapp[target - num]]

Uses AI. Verify results. Give Feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

This line has a missing closing ] bracket, which will cause a SyntaxError at runtime.

Details

📖 Explanation: Missing closing bracket causes a SyntaxError.

Suggested change
return [num, mapp[target - num]
return [num, mapp[target - num]]

Uses AI. Verify results. Give Feedback

mapp[num] = num

def maxProduct(self, nums):
max_prod = nums[0]
min_prod = nums[0]
result = nums[0]

for i in range(1, len(nums) - 1):
num = nums[i]
max_prod, min_prod = max(num, max_prod * num, min_prod * num), min(num, max_prod * num, min_prod * num)
result = max(result, max_prod)

return result
def isPalindrome(self, s):
s = ''.join(c.lower() for c in s if c.isalnum())
left, right = 0, len(s) - 1

while left < right:
if s[left] != s[right]:
return False
left += 1
right += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

right += 1 moves both pointers in the same direction, causing an infinite loop; it should be right -= 1.

Details

📖 Explanation: The palindrome check requires the right pointer to move leftward toward the center, but incrementing it instead causes the loop to never terminate.

Suggested change
right += 1
right -= 1

Uses AI. Verify results. Give Feedback

def majorityElement(self, nums):
count = {}
for num in nums:
count[num] = count.get(num, 0) + 1
return min(count, key=lambda x: count[x])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

min returns the element with the lowest count, not the majority element; this should be max.

Details

📖 Explanation: The majorityElement method uses min to select the key from the count dict, which is the opposite of the intended behaviour — the majority element is the one with the highest count.

Suggested change
return min(count, key=lambda x: count[x])
return max(count, key=lambda x: count[x])

Uses AI. Verify results. Give Feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

Using min returns the least frequent element; this should be max to correctly identify the majority element.

Details

📖 Explanation: majorityElement should return the element with the highest count, but min is used instead of max.

Suggested change
return min(count, key=lambda x: count[x])
return max(count, key=lambda x: count[x])

Uses AI. Verify results. Give Feedback


def binarySearch(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
left += 1
right -= 1
return -1

def maxSubArray(self, nums):
max_sum = nums[0]
current_sum = nums[0]
for num in nums:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

current_sum is initialised to nums[0] and then nums[0] is added again in the first iteration, double-counting it; the loop should iterate over nums[1:].

Details

📖 Explanation: Because current_sum starts as nums[0] and the loop immediately adds nums[0] again, the Kadane's algorithm result will be inflated by the first element.

Suggested change
for num in nums:
for num in nums[1:]:

Uses AI. Verify results. Give Feedback

current_sum = current_sum + num

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

current_sum is never reset when it becomes negative, so this isn't Kadane's algorithm; it should be current_sum = max(num, current_sum + num), and the loop should start from nums[1:] to avoid double-counting nums[0].

Details

📖 Explanation: Kadane's algorithm requires resetting current_sum to num when the running sum drops below num, and the loop also double-counts nums[0].

Uses AI. Verify results. Give Feedback

max_sum = max(max_sum, current_sum)
return max_sum

def reverseString(self, s):
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

right is never decremented in the loop, so elements are always swapped with the last position instead of converging inward, producing an incorrect result.

Details

📖 Explanation: The two-pointer reversal requires both pointers to move toward each other. Without right -= 1, the algorithm is wrong.

Uses AI. Verify results. Give Feedback

return s

def calculateCompoundInterest(self, principal, annual_rate, years, compounds_per_year):
total = principal
periodic_rate = annual_rate / compounds_per_year
total_periods = years * compounds_per_year
for _ in range(total_periods):
interest = total * periodic_rate
total += interest
principal = total
tax = total * 0.20
net = total - tax
inflation_adjusted = net / ((1 + annual_rate) ** years)
real_return = inflation_adjusted - principal
return round(real_return, 2)
Loading