Skip to content
Merged
Show file tree
Hide file tree
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
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")
23 changes: 23 additions & 0 deletions Algorithms_and_Data_Structures/Maths/k_beauty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def input_values():
num = int(input("Enter integer num: "))
k = int(input("Enter substring length k: "))
return num, k

def k_beauty(num: int, k: int) -> int:
"""Count length-k substrings of |num| that evenly divide num (ignoring 0)."""
if k <= 0:
raise ValueError("k must be a positive integer")
num_str = str(abs(num))
if k > len(num_str):
return 0
count = 0
for i in range(len(num_str) - k + 1):
sub_num = int(num_str[i : i + k])
if sub_num != 0 and num % sub_num == 0:
count += 1
return count

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")
25 changes: 25 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,25 @@
def input_values() -> tuple[int, int]:
x = int(input("Enter base x: "))
n = int(input("Enter exponent n: "))
return x, n

def pow_func(x: int, n: int):
if n == 0:
return 1
if n < 0:
if x == 0:
raise ValueError("0 cannot be raised to a negative exponent")
x = 1 / x
n = -n
result = 1
while n > 0:
if n % 2 == 1:
result *= x
x *= x
n //= 2
return result

if __name__ == "__main__":
x, n = input_values()
power = pow_func(x, n)
print(f"Result of pow({x},{n}): {power}\n")
28 changes: 28 additions & 0 deletions Algorithms_and_Data_Structures/Maths/reverse_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def input_value() -> int:
return int(input("Enter integer x: "))

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__":
x = input_value()
display_array([x])
result = reverse_integer(x)
print(f"Reversed integer is: {result}")


27 changes: 27 additions & 0 deletions Algorithms_and_Data_Structures/Maths/sqrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def input_value() -> int:

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 leading indentation causing SyntaxError.

Top-level def must not be indented.

- def input_value() -> int:
+def input_value() -> int:
📝 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 input_value() -> int:
def input_value() -> int:
🧰 Tools
🪛 Ruff (0.12.2)

1-1: SyntaxError: Unexpected indentation

🤖 Prompt for AI Agents
In Algorithms_and_Data_Structures/Maths/sqrt.py around line 1, the top-level
function definition is incorrectly indented; remove the leading indentation so
"def input_value() -> int:" starts at column 0 (no preceding spaces or tabs).
Ensure subsequent function body lines are indented consistently (using 4 spaces)
relative to the def, and run a quick syntax check (python -m py_compile) to
confirm the SyntaxError is resolved.

x = int(input("Enter non-negative integer x: "))
return x

def sqrt_floor(x: int) -> int:
if x < 0:
raise ValueError("x must be non-negative")
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")