Skip to content

Commit

Permalink
added recursion files
Browse files Browse the repository at this point in the history
  • Loading branch information
AKSHITHA-CHILUKA committed Oct 7, 2024
1 parent 4da94f4 commit 5b79f2f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Algorithms_and_Data_Structures/Recurssion/Binary_Search.py
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)
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])
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:])
7 changes: 7 additions & 0 deletions Algorithms_and_Data_Structures/Recurssion/Factorial.py
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 Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py
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)
7 changes: 7 additions & 0 deletions Algorithms_and_Data_Structures/Recurssion/Power_Function.py
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 Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py
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])
7 changes: 7 additions & 0 deletions Algorithms_and_Data_Structures/Recurssion/TowerofHanoi.py
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)

0 comments on commit 5b79f2f

Please sign in to comment.