0% found this document useful (0 votes)
19 views5 pages

EXCERCISE1

The document presents various algorithms implemented in both recursive and non-recursive forms, including finding the largest element in a list, checking for element uniqueness, matrix multiplication, and calculating binary digits. It also covers recursive algorithms for factorial calculation, Fibonacci series generation, binary search, and the Tower of Hanoi problem. Each algorithm is accompanied by sample code and output results.

Uploaded by

ARCHANA M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

EXCERCISE1

The document presents various algorithms implemented in both recursive and non-recursive forms, including finding the largest element in a list, checking for element uniqueness, matrix multiplication, and calculating binary digits. It also covers recursive algorithms for factorial calculation, Fibonacci series generation, binary search, and the Tower of Hanoi problem. Each algorithm is accompanied by sample code and output results.

Uploaded by

ARCHANA M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXCERCISE-1

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

2.Element Uniqueness Problem


def are_elements_unique(lst):
unique_elements = set()
for element in lst:
if element in unique_elements:
return False
unique_elements.add(element)
return True
my_list = [1, 2, 3, 4, 5]
result = are_elements_unique(my_list)

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],
]

result_matrix = matrix_multiply(matrix_A, matrix_B)


for row in result_matrix:
print(row)

OUTPUT:
[58, 64]
[139, 154]

4. Find Number of Binary digits in the binary representation of a positive decimal


Integer
def binary_digit(n):
count=0
while n>=1:
count+=1
n=n//2
return count
print("result:",binary_digit(5))

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

mid = (low + high) // 2

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)

# Input the number of disks


num_disks = int(input("Enter the number of disks: "))

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

You might also like