0% found this document useful (0 votes)
6 views

Python Practicals Sem2

The document contains various practical programming exercises demonstrating algorithms and data structures, including recursive and iterative factorial calculations, 1-D and 2-D array operations, the Tower of Hanoi problem, sorting algorithms (Bubble Sort and Selection Sort), search algorithms (Linear Search and Binary Search), and the N-Queen problem. Each section provides code examples and outputs for clarity. The exercises cover fundamental concepts in programming and algorithm design.

Uploaded by

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

Python Practicals Sem2

The document contains various practical programming exercises demonstrating algorithms and data structures, including recursive and iterative factorial calculations, 1-D and 2-D array operations, the Tower of Hanoi problem, sorting algorithms (Bubble Sort and Selection Sort), search algorithms (Linear Search and Binary Search), and the N-Queen problem. Each section provides code examples and outputs for clarity. The exercises cover fundamental concepts in programming and algorithm design.

Uploaded by

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

Practical 1: Factorial (Recursion vs Iteration)

# Recursive factorial
def factorial_recursive(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial_recursive(n-1)

# Iterative factorial
def factorial_iterative(n):
result = 1
for i in range(2, n+1):
result *= i
return result

n = 5
print("Recursive Factorial of", n, ":", factorial_recursive(n))
print("Iterative Factorial of", n, ":", factorial_iterative(n))

Practical 2: 1-D Array Operations


# Basic operations on 1-D array

arr = [10, 20, 30, 40, 50]

# Traversal
print("Array Elements:")
for i in arr:
print(i, end=" ")

# Insertion
arr.append(60)
print("\nAfter Insertion:", arr)

# Deletion
arr.remove(30)
print("After Deletion:", arr)

# Searching
if 40 in arr:
print("40 found at index", arr.index(40))
else:
print("40 not found")

Practical 3a: 2-D Array Row, Column, and Diagonal Sums


# Operations on 2-D array

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

# Row sum
for i in range(3):
print("Sum of row", i, "=", sum(matrix[i]))

# Column sum
for j in range(3):
col_sum = sum(matrix[i][j] for i in range(3))
print("Sum of column", j, "=", col_sum)

# Diagonal sums
main_diag = sum(matrix[i][i] for i in range(3))
sec_diag = sum(matrix[i][2-i] for i in range(3))
print("Sum of main diagonal =", main_diag)
print("Sum of secondary diagonal =", sec_diag)

Practical 4: Tower of Hanoi


# Tower of Hanoi

def tower_of_hanoi(n, source, auxiliary, target):


if n == 1:
print("Move disk 1 from", source, "to", target)
return
tower_of_hanoi(n-1, source, target, auxiliary)
print("Move disk", n, "from", source, "to", target)
tower_of_hanoi(n-1, auxiliary, source, target)

n = 3
tower_of_hanoi(n, 'A', 'B', 'C')

Practical 6: Bubble Sort and Selection Sort


# Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

# Selection Sort
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

arr1 = [64, 34, 25, 12, 22, 11, 90]


arr2 = arr1.copy()

bubble_sort(arr1)
print("Bubble Sorted array:", arr1)

selection_sort(arr2)
print("Selection Sorted array:", arr2)

Practical 8: Linear Search vs Binary Search


# Linear Search
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1

# Binary Search
def binary_search(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

arr = [10, 20, 30, 40, 50, 60]


x = 50

print("Linear Search:", linear_search(arr, x))


print("Binary Search:", binary_search(arr, x))

Practical 10: N-Queen Problem


# N-Queen Problem

def print_board(board):
for row in board:
print(" ".join(row))
print()

def is_safe(board, row, col, n):


for i in range(col):
if board[row][i] == 'Q':
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
for i, j in zip(range(row, n, 1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
return True

def solve_nq_util(board, col, n):


if col >= n:
print_board(board)
return
for i in range(n):
if is_safe(board, i, col, n):
board[i][col] = 'Q'
solve_nq_util(board, col+1, n)
board[i][col] = '.'

def solve_nq(n):
board = [['.' for _ in range(n)] for _ in range(n)]
solve_nq_util(board, 0, n)

n = 4
solve_nq(n)

You might also like