0% found this document useful (0 votes)
9 views3 pages

Iat 2

Uploaded by

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

Iat 2

Uploaded by

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

1. Develop a program to print 10 most frequently appearing words in a text file.

[Hint: Use dictionary with distinct words and their frequency of occurrences. Sort
the dictionary in the reverse order of frequency and display dictionary slice of
first 10 items]

fp = open('file_name.txt', 'r+')
word_lst = []
dst = {}
for line in fp.readlines():
word_lst += line.split()
for wd in set(word_lst):

dst[wd] = word_lst.count(wd)
sortedkey = sorted(dst, key=dst.get, reverse=True)
count = 0
for y in sortedkey:
print(count+1," Fequency of word ", y ,'=',dst[y])
count+=1
if(count == 10):
break

14. Write a program to check whether the given string is palindrome or not.

str1=input("Enter a string: ")


str2=str1[::-1]

if(str1.lower()==str2.lower()):
print("Entered string is a palindrome")
else:
print("Entered string is not a palindrome")

Test case 1:
Input:
Enter a string: Mam
Output:
Entered string is a palindrome

2. Write a function that takes three numbers as parameters, and returns the median
value of those parameters as its result. Include a main program that reads three
values from the user and displays their median. Hint: The median value is the
middle of the three values when they are sorted into ascending order. It can be
found using if statements, or with a little bit of mathematical creativity.

def median(a, b, c):


if a <b and b <c or a>b and b >c:
return b
if b <a and a <c or b >a and a> c:
return a
if c <a and b< c or c >a and b> c:
return c
def alternateMedian(a, b, c) :
return a + b + c — min(a, b, c) — max(a, b, c)
def main() :
x=float(input("Enter the first value: "))
y= float (input ("Enter the second value: "))
z =float (input ("Enter the third value:
print ("The median value" , median(x,y,z))
print ("Using the alternative method, it is:" , alternateMedian(x, y, z))
main ( )

3. In a particular jurisdiction, older license plates consist of three uppercase


letters followed by three numbers.

plate=input("enter the liciense plate:")


if len(plate==6) and plate[0]>="A" and plate[0]<="Z" and \
plate[1]>="A" and plate[1]<="Z" and \
plate[2]>="A" and plate[2]<="Z" and \
plate[3]>="0" and plate[3]<="9" and \
plate[4]>="0" and plate[4]<="9" and \
plate[5]>="0" and plate[5]<="9" and \
print("plate is a valid older style plate")
elif len(plate)==7 and plate[0]>="0" and plate[0]<="9" and \
plate[1]>="0" and plate[1]<="9" and \
plate[2]>="0" and plate[2]<="9" and \
plate[3]>="0" and plate[3]<="9" and \
plate[4]>="A" and plate[4]<="Z" and \
plate[5]>="A" and plate[5]<="Z" and \
plate[6]>="A" and plate[5]<="Z" :
print("the plate is a valid newer style plate."
else:
print("the plate is not valid")

MPC164

12. Read N numbers from the console and create a list. Develop a program to print
mean, variance and standard deviation with suitable messages.

import math as m
n=int(input('Enter the number:'))
lst=[]
sum=0
variance=0
lst=(input('Enter the numbers with space:').split())
for i in range(n):
sum+=float(lst[i])

mean=sum/n
for i in range(n):
variance+=(float(lst[i])-mean)**2
variance=variance/n
deviation=m.sqrt(variance)
print('Mean value is:', mean)
print("variance is:", variance)
print('standard deviation is:', deviation)
13. Read a multi-digit number (as chars) from the console. Develop a program to
print the frequency of each digit with a suitable message.

str1=input("Enter a mutidigit number: ")


for i in range(10):
if str1.count(str(i))!=0:
print('Number of count of', i, 'is:', str1.count(str(i)))

1. Develop a program to backing Up a given Folder (Folder in a current working


directory) into a ZIP File by using relevant modules and suitable methods.

Program:
import os
import zipfile

zf = zipfile.ZipFile("myzipfile.zip", "w") #zip file name


for dirname, subdirs, files in os.walk("../Python_Programs"): #folder name to
create zip
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
zf.close()

You might also like