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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| def input_value() -> int: | ||
| 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") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Fix leading indentation causing SyntaxError.
Top-level def must not be indented.
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.12.2)
1-1: SyntaxError: Unexpected indentation
🤖 Prompt for AI Agents