ACTIVITY PROGRAMS
[Link] a program to convert Octal to Hexadecimal and Hexadecimal to Octal.
[Link] a Python program to implement insertion sort and merge sort using lists.
[Link] a Random Number using inbuilt functions randrange(), shuffle, uniform.
[Link] to find and print all permutations of a given sequence (Integers/String) using list
and functions.
[Link] to find and print all combinations of a given sequence (Integers/String) using list
and functions.
[Link] a Program for Cos Wave Generation.
[Link] to solve a given 1st order difference equation using Z transform.
[Link] to solve a given 1st order differential equation using Laplace transform.
[Link] to calculate mean, median, mode, standard deviation, and variance.
[Link] To Generate Random Numbers:
a. From a given list of specified Random Numbers
b. Random floating-point number between 0 and 1
c. Random integer between a given range (e.g., 1 and 100)
[Link] to print all permutations for a given length of sequence:
a. Using List
b. Using Library functions
[Link] to print all permutations of coin tossing for a given number of flips.
[Link] to print all combinations of the dice using recursion and memorization.
Program 1: Develop a program to convert Octal to Hexadecimal and
Hexadecimal to Octal.
# Function to convert Octal to Hexadecimal
def octal_to_hex(oct_num):
dec = 0 # Initialize decimal value
base = 1 # Start with base 8^0 = 1
# Loop through the octal digits in reverse (right to left)
for digit in reversed(oct_num):
dec += int(digit) * base # Convert digit to decimal and add
base *= 8 # Increase base by power of 8
# Now convert decimal to hexadecimal
hex_digits = "0123456789ABCDEF" # All hex characters
hex_num = "" # Final hex result as string
# Loop to convert decimal to hex
while dec > 0:
rem = dec % 16 # Get remainder
hex_num = hex_digits[rem] + hex_num # Add corresponding hex char
dec //= 16 # Reduce decimal for next iteration
# If input was 0, return '0'
return hex_num if hex_num else "0"
# Function to convert Hexadecimal to Octal
def hex_to_octal(hex_num):
dec = 0 # Initialize decimal value
base = 1 # Start with base 16^0 = 1
hex_num = hex_num.upper() # Convert to uppercase (in case user enters 'a', 'b', etc.)
# Loop through hex digits in reverse
for digit in reversed(hex_num):
if [Link](): # For 0-9
val = int(digit) # Convert character to integer
else:
val = ord(digit) - 55 # For A-F: A=10, B=11, ... Z=35 (but we use only till F)
dec += val * base # Add to decimal value
base *= 16 # Increase base
# Convert decimal to octal
oct_num = "" # Final octal result as string
# Loop to convert decimal to octal
while dec > 0:
rem = dec % 8 # Get remainder
oct_num = str(rem) + oct_num # Add digit to result
dec //= 8 # Reduce decimal
# If input was 0, return '0'
return oct_num if oct_num else "0"
# --------------- MAIN MENU ---------------
print("1. Octal to Hexadecimal") # Show option 1
print("2. Hexadecimal to Octal") # Show option 2
choice = input("Choose option (1 or 2): ") # Ask user to pick
# If user chose octal to hexadecimal
if choice == '1':
octal = input("Enter Octal number: ") # Ask for octal number
print("Hexadecimal:", octal_to_hex(octal)) # Show result
# If user chose hexadecimal to octal
elif choice == '2':
hexa = input("Enter Hexadecimal number: ") # Ask for hex number
print("Octal:", hex_to_octal(hexa)) # Show result
# Invalid choice handling
else:
print("Invalid option.") # Error message
OUTPUT:
Program 2: Write a Python program to implement insertion sort and merge sort using lists.
# Program to implement Insertion Sort and Merge Sort using lists
# -------- Insertion Sort --------
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i] # Current element to be compared
j=i-1
# Move elements that are greater than key to one position ahead
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j] # Shift element to right
j -= 1
arr[j + 1] = key # Place key in its correct position
return arr
# -------- Merge Sort --------
def merge_sort(arr):
if len(arr) <= 1:
return arr # Base case: already sorted
mid = len(arr) // 2 # Find midpoint
left_half = merge_sort(arr[:mid]) # Sort left half
right_half = merge_sort(arr[mid:]) # Sort right half
# Merge the two halves
return merge(left_half, right_half)
# Helper function to merge two sorted lists
def merge(left, right):
result = []
i=j=0
# Compare elements from both halves and merge in order
while i < len(left) and j < len(right):
if left[i] <= right[j]:
[Link](left[i])
i += 1
else:
[Link](right[j])
j += 1
# Add remaining elements
[Link](left[i:])
[Link](right[j:])
return result
# -------- Main Program --------
print("1. Insertion Sort")
print("2. Merge Sort")
choice = input("Enter your choice (1 or 2): ")
data = input("Enter the numbers separated by space: ")
arr = list(map(int, [Link]().split())) # Convert input to list of integers
if choice == '1':
sorted_arr = insertion_sort([Link]())
print("Sorted using Insertion Sort:", sorted_arr)
elif choice == '2':
sorted_arr = merge_sort([Link]())
print("Sorted using Merge Sort:", sorted_arr)
else:
print("Invalid choice. Please enter 1 or 2.")