python_activity_programs
python_activity_programs
2.Write a Python program to implement insertion sort and merge sort using lists.
4.Program to find and print all permutations of a given sequence (Integers/String) using list
and functions.
5.Program to find and print all combinations of a given sequence (Integers/String) using list
and functions.
8.Program to solve a given 1st order differential equation using Laplace transform.
9.Program to calculate mean, median, mode, standard deviation, and variance.
12.Program to print all permutations of coin tossing for a given number of flips.
13.Program 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.
OUTPUT:
Program 2: Write a Python program to implement insertion sort and merge sort
using lists.
# 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
return arr
if choice == '1':
sorted_arr = insertion_sort(arr.copy())
print("Sorted using Insertion Sort:", sorted_arr)
else:
print("Invalid choice. Please enter 1 or 2.")
OUTPUT:
return result
# ------ Main Code ------
# Print results
print("All possible permutations:")
for p in all_perms:
print(p)
OUTPUT:
Program 5: Program to find and print all combinations of a given sequence
(Integers/String) using list and functions.
OUPUT:
OUTPUT:
Program 7: Program to solve a given 1st order difference equation using Z
transform.
import numpy as np
import matplotlib.pyplot as plt
# Parameters
a = 0.5 # constant in the equation
n_samples = 10 # number of samples
x = np.ones(n_samples) # input sequence x[n] = 1 for all n
y = difference_equation(a, x) # solve the difference equation
# Output
print("Input x[n]:", x)
print("Output y[n]:", y)
# Plotting
plt.stem(range(n_samples), x, basefmt="b-", linefmt="b-", markerfmt="bo", label="Input
x[n]")
plt.stem(range(n_samples), y, basefmt="r-", linefmt="r-", markerfmt="ro", label="Output
y[n]")
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.title("First-Order Difference Equation: y[n] = a*y[n-1] + x[n]")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
Program 8: Program to solve a given 1st order differential equation using
Laplace transform.
import sympy as sp
# Define symbols
t, s = sp.symbols('t s')
y = sp.Function('y')
x = sp.Function('x')
a = sp.Symbol('a')
OUTPUT:
Program 9: Program to calculate mean, median, mode, standard deviation, and
variance.
variance_val = stats.variance(data)
std_dev_val = stats.stdev(data)
📊
# ---- Output ----
print("\n Statistical Measures:")
print(f"Mean: {mean_val}")
print(f"Median: {median_val}")
print(f"Mode: {mode_val}")
print(f"Variance: {variance_val}")
print(f"Standard Deviation: {std_dev_val}")
OUTPUT:
Program 10: Program 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)
import random
OUTPUT:
Program 11: Program to print all permutations for a given length of sequence:
a. Using List
b. Using Library functions
recurse([], [False]*len(seq))
return result
manual_result = generate_permutations(elements, r)
for item in manual_result:
print(item)
print(f"Total permutations (manual): {len(manual_result)}")
def generate_coin_tosses(n):
results = []
backtrack('', n)
return results
OUTPUT:
Program 13: Program to print all combinations of the dice using recursion and
memorization.
@lru_cache(maxsize=None)
def dice_combinations(n):
if n == 0:
return [[]]
result = []
for face in range(1, 7):
for combo in dice_combinations(n - 1):
result.append([face] + combo)
return result
OUTPUT: