22plc15b python
1
22plc15b python
Dept of CSE, SIT - CPP 2
22plc15b python
Dept of CSE, SIT - CPP 3
22plc15b python
Question 5
Word Frequency
Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use dictio-
nary 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]
12 import sys
13 import string
14 import os.path
16 fname = input("Enter the filename : ")
18 if not os.path.isfile(fname):
19 print("File", fname, "doesn’t exists")
20 sys.exit(0)
22 infile = open(fname, "r")
24 filecontents = ""
26 for line in infile:
27 for ch in line:
28 if ch not in string.punctuation:
29 filecontents = filecontents + ch
30 else:
31 filecontents = filecontents + ’ ’
33 wordFreq = {}
34 wordList = filecontents.split()
for word in wordList:
39 if word not in wordFreq.keys():
40 wordFreq[word] = 1
41 else:
42 wordFreq[word] += 1
43
44 #Sort Dictionary based on values in descending order
45 sortedWordFreq = sorted(wordFreq.items(), key=lambda x:x[1], reverse=True )
46
47 #Display 10 most frequently appearing words with their count
48
49 print("\n===================================================")
50 print("10 most frequently appearing words with their count")
51 print("===================================================")
52 for i in range(10):
53 print(sortedWordFreq[i][0], "occurs", sortedWordFreq[i][1], "times")
9
22plc15b python
Question 6
Sort File Contents
Develop a program to sort the contents of a text file and write the sorted contents into a separatetext
file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods open(),
readlines(), and write()].
15 import os.path
16 import sys
18 fname = input("Enter the filename whose contents are to be sorted : ")
20 if not os.path.isfile(fname):
21 print("File", fname, "doesn’t exists")
22 sys.exit(0)
24 infile = open(fname, "r")
26 myList = infile.readlines()
30 lineList = []
31 for line in myList:
32 lineList.append(line.strip())
34 lineList.sort()
38 outfile = open("sorted.txt","w")
41 for line in lineList:
42 outfile.write(line + "\n")
44 infile.close()
45 outfile.close()
48 if os.path.isfile("sorted.txt"):
49 print("\nFile containing sorted content sorted.txt created successfully")
50 print("sorted.txt contains", len(lineList), "lines")
51 print("Contents of sorted.txt")
52 print("=================================================================")
53 rdFile = open("sorted.txt","r")
54 for line in rdFile:
55 print(line, end="")
11
22plc15b python
Dept of CSE, SIT - CPP 12
22plc15b python
Question 10
Student Class Demo
Develop a program that uses class Student which prompts the user to enter marks in three sub-
jects and calculates total marks, percentage and displays the score card details. [Hint: Use list to
store the marks in three subjects and total marks. Use init method to initialize name, USNand
the lists to store marks and total, Use getMarks() method to read marks into the list, and display()
method to display the score card details.]
13 class Student:
14 def init (self, name = "", usn = "", score = [0,0,0,0]):
15 self.name = name
16 self.usn = usn
17 self.score = score
19 def getMarks(self):
20 self.name = input("Enter student Name : ")
21 self.usn = input("Enter student USN : ")
22 self.score[0] = int(input("Enter marks in Subject 1 : "))
23 self.score[1] = int(input("Enter marks in Subject 2 : "))
24 self.score[2] = int(input("Enter marks in Subject 3 : "))
25 self.score[3] = self.score[0] + self.score[1] + self.score[2]
27 def display(self):
28 percentage = self.score[3]/3
29 spcstr = "=" * 81
30 print(spcstr)
31 print("SCORE CARD DETAILS".center(81))
print(spcstr)
33 print("%15s"%("NAME"), "%12s"%("USN"), "%8s"%"MARKS1","%8s"%"MARKS2","%8s"%"
MARKS3","%8s"%"TOTAL","%12s"%("PERCENTAGE"))
34 print(spcstr)
35 print("%15s"%self.name, "%12s"%self.usn, "%8d"%self.score[0],"%8d"%self.
score[1],"%8d"%self.score[2],"%8d"%self.score[3],"%12.2f"%percentage)
36 print(spcstr)
39 def main():
40 s1 = Student()
41 s1.getMarks()
42 s1.display()
main()
Dept of CSE, SIT - CPP 20