EXCERCISE1
EXCERCISE1
Implement Recursive and Non Recursive algorithms and study the order of
growth from log n to n!
NON RECURSION
1.Find the Largest Element from a list
def find_largest_element(lst):
if not lst:
return None
largest = lst[0]
for element in lst:
if element > largest:
largest = element
return largest
my_list = [12, 45, 74, 6, 23, 55, 17]
largest_element = find_largest_element(my_list)
if largest_element is not None:
print("The largest element in the list is:", largest_element)
else:
print("The list is empty.")
OUTPUT:
The largest element in the list is: 45
if result:
print("All elements in the list are unique.")
else:
print("The list contains duplicate elements.")
OUTPUT:
All elements in the list are unique
3.Matrix Multiplication
def matrix_multiply(A, B):
if len(A[0]) != len(B):
raise ValueError("Number of columns in matrix A must be equal to the number of rows in
matrix B")
result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
return result
matrix_A = [
[1, 2, 3],
[4, 5, 6],
]
matrix_B = [
[7, 8],
[9, 10],
[11, 12],
]
OUTPUT:
[58, 64]
[139, 154]
OUTPUT:
result: 3
RECURSION
1. Factorial Number
def fact(n):
if(n==0 or n==1):
return 1
else:
result=n*fact(n-1)
return result
print("result:",fact(3))
OUTPUT:
result: 6
2. Fibonacci series
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_list = fibonacci(n - 1)
fib_list.append(fib_list[-1] + fib_list[-2])
return fib_list
n = int(input("Enter the number of Fibonacci terms: "))
if n <= 0:
print("Please enter a positive integer.")
else:
fib_series = fibonacci(n)
print("Fibonacci Series:")
print(fib_series)
OUTPUT:
Enter the number of Fibonacci terms: 5
Fibonacci Series:
[0, 1, 1, 2, 3]
3. Binary search
def binary_search_recursive(arr, target, low, high):
if low > high:
return -1 # Element not found
if arr[mid] == target:
return mid # Element found at index mid
elif arr[mid] < target:
return binary_search_recursive(arr, target, mid + 1, high) # Search in the right half
else:
return binary_search_recursive(arr, target, low, mid - 1) # Search in the left half
def binary_search(arr, target):
return binary_search_recursive(arr, target, 0, len(arr) - 1)
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5
result = binary_search(arr, target)
if result != -1:
print(f"Element {target} found at index {result}.")
else:
print(f"Element {target} not found in the list.")
OUTPUT:
Element 5 found at index 4.
4.Tower Of Hanoi
def tower_of_hanoi(n, source, auxiliary, target):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n - 1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n - 1, auxiliary, source, target)
if num_disks <= 0:
print("Please enter a positive integer.")
else:
tower_of_hanoi(num_disks, 'A', 'B', 'C')
OUTPUT:
Enter the number of disks: 3
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C