Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions Algorithms_and_Data_Structures/Maths/count_of_matches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def input_value():
n = int(input("Enter number of teams n: "))
return n

def tournament_matches(n):
total_matches = 0
while n > 1:
if n % 2 == 0:
matches = n // 2
n = matches
else:
matches = (n - 1) // 2
n = matches + 1
total_matches += matches
return total_matches

if __name__ == "__main__":
n = input_value()
result = tournament_matches(n)
print(f"Total matches played until a winner is decided: {result}\n")
17 changes: 17 additions & 0 deletions Algorithms_and_Data_Structures/Maths/fascinating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def input_value():
n = int(input("Enter 3-digit integer n: "))
return n

def is_fascinating(n):
if n < 100 or n > 999:
return False # Ensure 3-digit input
concat_str = str(n) + str(2*n) + str(3*n)
if '0' in concat_str:
return False
digits_set = set(concat_str)
return len(concat_str) == 9 and digits_set == set("123456789")

if __name__ == "__main__":
n = input_value()
result = is_fascinating(n)
print(f"Is {n} fascinating? {result}\n")
19 changes: 19 additions & 0 deletions Algorithms_and_Data_Structures/Maths/k_beauty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def input_values():
num = int(input("Enter integer num: "))
k = int(input("Enter substring length k: "))
return num, k

def k_beauty(num, k):
num_str = str(num)
count = 0
for i in range(len(num_str) - k + 1):
sub_str = num_str[i:i+k]
sub_num = int(sub_str)
if sub_num != 0 and num % sub_num == 0:
count += 1
return count
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

if __name__ == "__main__":
num, k = input_values()
result = k_beauty(num, k)
print(f"The k-beauty of {num} with k = {k} is: {result}\n")
23 changes: 23 additions & 0 deletions Algorithms_and_Data_Structures/Maths/pow(x,n).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def input_values():
x = int(input("Enter base x: "))
n = int(input("Enter exponent n: "))
return x, n

def pow_func(x, n):
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
result = 1
while n > 0:
if n % 2 == 1:
result *= x
x *= x
n //= 2
return result

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.

⚠️ Potential issue

Fix division-by-zero for 0 base with negative exponent.

When x == 0 and n < 0, line 10 raises ZeroDivisionError. Handle explicitly and return 0 for x == 0, n > 0.

 def pow_func(x, n):
     if n == 0:
         return 1
+    if x == 0:
+        if n < 0:
+            raise ZeroDivisionError("0 cannot be raised to a negative power")
+        return 0
     if n < 0:
         x = 1 / x
         n = -n
     result = 1
     while n > 0:
         if n % 2 == 1:
             result *= x
         x *= x
         n //= 2
     return result
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def pow_func(x, n):
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
result = 1
while n > 0:
if n % 2 == 1:
result *= x
x *= x
n //= 2
return result
def pow_func(x, n):
if n == 0:
return 1
if x == 0:
if n < 0:
raise ZeroDivisionError("0 cannot be raised to a negative power")
return 0
if n < 0:
x = 1 / x
n = -n
result = 1
while n > 0:
if n % 2 == 1:
result *= x
x *= x
n //= 2
return result
🤖 Prompt for AI Agents
In Algorithms_and_Data_Structures/Maths/pow(x,n).py around lines 6 to 18, the
code does x = 1 / x for n < 0 which raises ZeroDivisionError when x == 0; fix by
handling x == 0 before any inversion: if x == 0 and n > 0 return 0, and if x ==
0 and n <= 0 raise a clear ZeroDivisionError (or ValueError) indicating 0 cannot
be raised to a non-positive power; only after these checks perform the n < 0
inversion and proceed with the fast exponentiation loop.


if __name__ == "__main__":
x, n = input_values()
power = pow_func(x, n)
print(f"Result of pow({x},{n}): {power}\n")
30 changes: 30 additions & 0 deletions Algorithms_and_Data_Structures/Maths/reverse_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def input_array(arr, n): # Not needed for single integer, but keeping format
num = int(input("Enter integer x: "))
arr.append(num)

def display_array(arr):
print(f"\nInput integer is: {arr}")

def reverse_integer(x):
INT_MAX = 2**31 - 1 # 2147483647
INT_MIN = -2**31 # -2147483648
rev = 0
sign = -1 if x < 0 else 1
x = abs(x)
while x != 0:
digit = x % 10
x //= 10
# Check overflow before multiplying or adding
if rev > INT_MAX // 10 or (rev == INT_MAX // 10 and digit > 7):
return 0
rev = rev * 10 + digit
return sign * rev if INT_MIN <= sign * rev <= INT_MAX else 0

if __name__ == "__main__":
nums = []
input_array(nums, 1)
display_array(nums)
x = nums[0] # Extract integer from list
result = reverse_integer(x) # Pass the integer, not the list
print(f"Reversed integer is: {result}")

25 changes: 25 additions & 0 deletions Algorithms_and_Data_Structures/Maths/sqrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def input_value():
x = int(input("Enter non-negative integer x: "))
return x

def sqrt_floor(x):
if x < 2:
return x # sqrt(0)=0, sqrt(1)=1

left, right = 1, x
ans = 1
while left <= right:
mid = (left + right) // 2
if mid * mid == x:
return mid
elif mid * mid < x:
ans = mid
left = mid + 1
else:
right = mid - 1
return ans

if __name__ == "__main__":
x = input_value()
result = sqrt_floor(x)
print(f"Square root of {x} (rounded down) is: {result}\n")