Repository navigation
Fixed Issue #1772: Added a math folder to the Algorithms_and_Data_Structures section. #1786
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
Changes from 1 commit
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 |
|---|---|---|
| @@ -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") |
| 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") |
| 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 | ||
|
|
||
| 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") | ||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x, n = input_values() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| power = pow_func(x, n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Result of pow({x},{n}): {power}\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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}") | ||
|
|
| 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") |
Uh oh!
There was an error while loading. Please reload this page.