Iat 2
Iat 2
[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.
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.
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.
Program:
import os
import zipfile