-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4da94f4
commit 5b79f2f
Showing
8 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
Algorithms_and_Data_Structures/Recurssion/Binary_Search.py
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,10 @@ | ||
def binary_search(arr, target, low, high): | ||
if low > high: | ||
return -1 # Target not found | ||
mid = (low + high) // 2 | ||
if arr[mid] == target: | ||
return mid | ||
elif arr[mid] > target: | ||
return binary_search(arr, target, low, mid - 1) | ||
else: | ||
return binary_search(arr, target, mid + 1, high) |
4 changes: 4 additions & 0 deletions
4
Algorithms_and_Data_Structures/Recurssion/Check_for_Palindrome.py
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,4 @@ | ||
def is_palindrome(s): | ||
if len(s) <= 1: | ||
return True | ||
return s[0] == s[-1] and is_palindrome(s[1:-1]) |
5 changes: 5 additions & 0 deletions
5
Algorithms_and_Data_Structures/Recurssion/Count_Vowels_in_a_String.py
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,5 @@ | ||
def count_vowels(s): | ||
if len(s) == 0: | ||
return 0 | ||
vowels = "aeiouAEIOU" | ||
return (1 if s[0] in vowels else 0) + count_vowels(s[1:]) |
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,7 @@ | ||
def factorial(n): | ||
if n < 0: | ||
raise ValueError("Factorial is not defined for negative numbers.") | ||
elif n == 0 or n == 1: | ||
return 1 | ||
else: | ||
return n * factorial(n - 1) |
9 changes: 9 additions & 0 deletions
9
Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py
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 fibonacci(n): | ||
if n < 0: | ||
raise ValueError("Fibonacci is not defined for negative numbers.") | ||
elif n == 0: | ||
return 0 | ||
elif n == 1: | ||
return 1 | ||
else: | ||
return fibonacci(n - 1) + fibonacci(n - 2) |
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,7 @@ | ||
def power(x, n): | ||
if n == 0: | ||
return 1 | ||
elif n < 0: | ||
return 1 / power(x, -n) | ||
else: | ||
return x * power(x, n - 1) |
5 changes: 5 additions & 0 deletions
5
Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py
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,5 @@ | ||
def reverse_string(s): | ||
if len(s) == 0: | ||
return s | ||
else: | ||
return s[-1] + reverse_string(s[:-1]) |
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,7 @@ | ||
def tower_of_hanoi(n, source, target, auxiliary): | ||
if n == 1: | ||
print(f"Move disk 1 from {source} to {target}") | ||
return | ||
tower_of_hanoi(n - 1, source, auxiliary, target) | ||
print(f"Move disk {n} from {source} to {target}") | ||
tower_of_hanoi(n - 1, auxiliary, target, source) |