Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

To write a Python program that take in command line arguments as

input and print the number of arguments.


# command line arguments
import sys
# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)
# Arguments passed
print("\nName of Python script:", sys.argv[0])
print("\nArguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")
# Addition of numbers
Sum = 0
# Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
To write a Python program to perform Matrix Multiplication
# Program to multiply two matrices using nested loops
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
[114, 160, 60, 27]

[74, 97, 73, 14]

[119, 157, 112, 23]


To write a Python program to compute the GCD of two numbers.
num1 = 36
num2 = 60
a = num1
b = num2
while num1 != num2:
if num1 > num2:
num1 -= num2
else:
num2 -= num1
print("GCD of", a, "and", b, "is", num1)
Output
GCD of 36 and 60 is 12
# Recursive function to return GCD of two number
def findGCD(num1, num2):
# Everything divides 0
if num1 == 0 or num2 == 0:
return num1 + num2
# base case
if num1 == num2:
return num1
To write a Python program to find the most frequent word in a text
file.
count = 0;
word = "";
maxCount = 0;
words = [];
#Opens a file in read mode
file = open("data.txt", "r")

#Gets each line till end of file is reached


for line in file:
#Splits each line into words
string = line.lower().replace(',','').replace('.','').split(" ");
#Adding all words generated in previous step into words
for s in string:
words.append(s);

#Determine the most repeated word in a file


for i in range(0, len(words)):
count = 1;
#Count each word in the file and store it in variable count
for j in range(i+1, len(words)):
if(words[i] == words[j]):
count = count + 1;
To write a Python program Linear search
# Linear Search in Python
def linearSearch(array, n, x):
# Going through array sequencially
for i in range(0, n):
if (array[i] == x):
return i
return -1
array = [2, 4, 0, 1, 9]
x=1
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
[09:56, 8/20/2023] mansisharma: To write a python program selection sort
[09:56, 8/20/2023] mansisharma: # Selection sort in Python
# time complexity O(n*n)
#sorting by finding min_index
def selectionSort(array, size):
for ind in range(size):
min_index = ind
for j in range(ind + 1, size):
# select the minimum element in every iteration
if array[j] < array[min_index]:
min_index = j
# swapping the elements to sort the array
(array[ind], array[min_index]) = (array[min_index], array[ind])

arr = [-2, 45, 0, 11, -9,88,-97,-202,747]


size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection sort is:')
print(arr)
#OUTPUT
The array after sorting in Ascending Order by selection sort is:
[-202, -97, -9, -2, 0, 11, 45, 88, 747]

You might also like