-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from aniruddhaadak80/patch-1
10 simple python algorithms Solutions
- Loading branch information
Showing
10 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,13 @@ | ||
def binary_exponentiation(base, exp): | ||
result = 1 | ||
while exp > 0: | ||
if exp % 2 == 1: | ||
result *= base | ||
base *= base | ||
exp //= 2 | ||
return result | ||
|
||
# Get user input | ||
base = float(input("Enter the base: ")) | ||
exp = int(input("Enter the exponent: ")) | ||
print(f"{base} raised to the power {exp} is: {binary_exponentiation(base, exp)}") |
This file contains 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,12 @@ | ||
def exponentiation_by_squaring(base, exp): | ||
if exp < 0: | ||
return 1 / exponentiation_by_squaring(base, -exp) | ||
if exp == 0: | ||
return 1 | ||
half = exponentiation_by_squaring(base, exp // 2) | ||
return half * half if exp % 2 == 0 else base * half * half | ||
|
||
# Get user input | ||
base = float(input("Enter the base: ")) | ||
exp = int(input("Enter the exponent: ")) | ||
print(f"{base} raised to the power {exp} is: {exponentiation_by_squaring(base, exp)}") |
This file contains 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,36 @@ | ||
def flood_fill(image, sr, sc, new_color): | ||
original_color = image[sr][sc] | ||
if original_color == new_color: | ||
return image | ||
|
||
def fill(r, c): | ||
if r < 0 or r >= len(image) or c < 0 or c >= len(image[0]): | ||
return | ||
if image[r][c] != original_color: | ||
return | ||
|
||
image[r][c] = new_color | ||
fill(r + 1, c) | ||
fill(r - 1, c) | ||
fill(r, c + 1) | ||
fill(r, c - 1) | ||
|
||
fill(sr, sc) | ||
return image | ||
|
||
# Get user input | ||
rows = int(input("Enter the number of rows in the image: ")) | ||
cols = int(input("Enter the number of columns in the image: ")) | ||
image = [] | ||
print("Enter the image row by row (space-separated values):") | ||
for _ in range(rows): | ||
image.append(list(map(int, input().split()))) | ||
|
||
sr = int(input("Enter the starting row: ")) | ||
sc = int(input("Enter the starting column: ")) | ||
new_color = int(input("Enter the new color: ")) | ||
|
||
flood_fill(image, sr, sc, new_color) | ||
print("Updated image after flood fill:") | ||
for row in image: | ||
print(row) |
This file contains 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,9 @@ | ||
def gcd(a, b): | ||
while b: | ||
a, b = b, a % b | ||
return a | ||
|
||
# Get user input | ||
a = int(input("Enter the first number: ")) | ||
b = int(input("Enter the second number: ")) | ||
print(f"The GCD of {a} and {b} is: {gcd(a, b)}") |
This file contains 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,13 @@ | ||
def kadane(arr): | ||
max_current = max_global = arr[0] | ||
|
||
for i in range(1, len(arr)): | ||
max_current = max(arr[i], max_current + arr[i]) | ||
if max_current > max_global: | ||
max_global = max_current | ||
|
||
return max_global | ||
|
||
# Get user input | ||
arr = list(map(int, input("Enter numbers separated by spaces: ").split())) | ||
print(f"The maximum subarray sum is: {kadane(arr)}") |
This file contains 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,12 @@ | ||
def gcd(a, b): | ||
while b: | ||
a, b = b, a % b | ||
return a | ||
|
||
def lcm(a, b): | ||
return abs(a * b) // gcd(a, b) | ||
|
||
# Get user input | ||
a = int(input("Enter the first number: ")) | ||
b = int(input("Enter the second number: ")) | ||
print(f"The LCM of {a} and {b} is: {lcm(a, b)}") |
This file contains 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,19 @@ | ||
def lcs(X, Y): | ||
m, n = len(X), len(Y) | ||
dp = [[0] * (n + 1) for _ in range(m + 1)] | ||
|
||
for i in range(m + 1): | ||
for j in range(n + 1): | ||
if i == 0 or j == 0: | ||
dp[i][j] = 0 | ||
elif X[i - 1] == Y[j - 1]: | ||
dp[i][j] = dp[i - 1][j - 1] + 1 | ||
else: | ||
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) | ||
|
||
return dp[m][n] | ||
|
||
# Get user input | ||
X = input("Enter the first string: ") | ||
Y = input("Enter the second string: ") | ||
print(f"The length of the longest common subsequence is: {lcs(X, Y)}") |
This file contains 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,15 @@ | ||
def sieve_of_eratosthenes(n): | ||
primes = [] | ||
is_prime = [True] * (n + 1) | ||
is_prime[0] = is_prime[1] = False | ||
|
||
for i in range(2, n + 1): | ||
if is_prime[i]: | ||
primes.append(i) | ||
for j in range(i * i, n + 1, i): | ||
is_prime[j] = False | ||
return primes | ||
|
||
# Get user input | ||
n = int(input("Enter the upper limit to find prime numbers: ")) | ||
print(f"Prime numbers up to {n} are: {sieve_of_eratosthenes(n)}") |
This file contains 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,14 @@ | ||
def max_sum_subarray(arr, k): | ||
max_sum = sum(arr[:k]) | ||
window_sum = max_sum | ||
|
||
for i in range(len(arr) - k): | ||
window_sum = window_sum - arr[i] + arr[i + k] | ||
max_sum = max(max_sum, window_sum) | ||
|
||
return max_sum | ||
|
||
# Get user input | ||
arr = list(map(int, input("Enter numbers separated by spaces: ").split())) | ||
k = int(input("Enter the size of the subarray: ")) | ||
print(f"The maximum sum of a subarray of size {k} is: {max_sum_subarray(arr, k)}") |
This file contains 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,21 @@ | ||
def two_pointer(arr, target): | ||
left, right = 0, len(arr) - 1 | ||
while left < right: | ||
current_sum = arr[left] + arr[right] | ||
if current_sum == target: | ||
return (arr[left], arr[right]) | ||
elif current_sum < target: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
return None | ||
|
||
# Get user input | ||
arr = list(map(int, input("Enter sorted numbers separated by spaces: ").split())) | ||
target = int(input("Enter the target sum: ")) | ||
result = two_pointer(arr, target) | ||
|
||
if result: | ||
print(f"The pair that sums to {target} is: {result}") | ||
else: | ||
print("No pair found.") |