From 5b79f2fde993b014e128122f77ba3008562df05b Mon Sep 17 00:00:00 2001 From: ~Chiluka Akshitha <22wh1a12b5@bvrithyderabad.edu.in> Date: Mon, 7 Oct 2024 19:03:10 +0530 Subject: [PATCH] added recursion files --- .../Recurssion/Binary_Search.py | 10 ++++++++++ .../Recurssion/Check_for_Palindrome.py | 4 ++++ .../Recurssion/Count_Vowels_in_a_String.py | 5 +++++ Algorithms_and_Data_Structures/Recurssion/Factorial.py | 7 +++++++ .../Recurssion/Fibonacci_Series.py | 9 +++++++++ .../Recurssion/Power_Function.py | 7 +++++++ .../Recurssion/Reverse_a_String.py | 5 +++++ .../Recurssion/TowerofHanoi.py | 7 +++++++ 8 files changed, 54 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Recurssion/Binary_Search.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Check_for_Palindrome.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Count_Vowels_in_a_String.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Factorial.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Power_Function.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py create mode 100644 Algorithms_and_Data_Structures/Recurssion/TowerofHanoi.py diff --git a/Algorithms_and_Data_Structures/Recurssion/Binary_Search.py b/Algorithms_and_Data_Structures/Recurssion/Binary_Search.py new file mode 100644 index 0000000000..e16a9eda8f --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Binary_Search.py @@ -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) diff --git a/Algorithms_and_Data_Structures/Recurssion/Check_for_Palindrome.py b/Algorithms_and_Data_Structures/Recurssion/Check_for_Palindrome.py new file mode 100644 index 0000000000..dd3ee21111 --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Check_for_Palindrome.py @@ -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]) diff --git a/Algorithms_and_Data_Structures/Recurssion/Count_Vowels_in_a_String.py b/Algorithms_and_Data_Structures/Recurssion/Count_Vowels_in_a_String.py new file mode 100644 index 0000000000..802112ed8a --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Count_Vowels_in_a_String.py @@ -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:]) diff --git a/Algorithms_and_Data_Structures/Recurssion/Factorial.py b/Algorithms_and_Data_Structures/Recurssion/Factorial.py new file mode 100644 index 0000000000..5e238ea08a --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Factorial.py @@ -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) diff --git a/Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py b/Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py new file mode 100644 index 0000000000..6c2866d3be --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Fibonacci_Series.py @@ -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) diff --git a/Algorithms_and_Data_Structures/Recurssion/Power_Function.py b/Algorithms_and_Data_Structures/Recurssion/Power_Function.py new file mode 100644 index 0000000000..ca104c89df --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Power_Function.py @@ -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) diff --git a/Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py b/Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py new file mode 100644 index 0000000000..d70fd0ae94 --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/Reverse_a_String.py @@ -0,0 +1,5 @@ +def reverse_string(s): + if len(s) == 0: + return s + else: + return s[-1] + reverse_string(s[:-1]) diff --git a/Algorithms_and_Data_Structures/Recurssion/TowerofHanoi.py b/Algorithms_and_Data_Structures/Recurssion/TowerofHanoi.py new file mode 100644 index 0000000000..68bfebc67a --- /dev/null +++ b/Algorithms_and_Data_Structures/Recurssion/TowerofHanoi.py @@ -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)