Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

#Question 4: Def in

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

1) Write is a program that reads a file“myfile.

txt” and builds a histogram (a dictionary


having key value pair as word: occurrence) of the words in the file.
a) Now use histogram to print
i) Total number of words
ii) Number of different words
iii) The most common words
a) Using above text file “myfile.txt”, write a program that maps a list of words read
from the file to an integer representing the length of the corresponding words.( use
dictionary having key value pair as length : list of word )
Now using above dictionary design a function find_longest_word() to display a
list of longest words from file.
Define a function filter_long_words(n) that takes an integer n and returns the list
of words that are longer than n from file.

Code:
#Question 4
hist={}
length=[]
wordlist=[]
def histogram():
  f=open('file.txt','r')
  for line in f:
    line=line.strip()
    line=line.lower()
    words=line.split()
    for word in words:
      if word.isalpha():
        if word in hist:
          hist[word]=hist[word]+1
        else:
          hist[word]=1
  print("Histogram created!")
  f.close()

print("-----Part (a)-----")
histogram()
val=hist.values()
print("Total no of words:",sum(val))
print("No of different words:",len(hist))
Keymax = max(hist, key= lambda x: hist[x])
print("Most common word is:",Keymax)

#b)
def lenword():
  f=open('file.txt','r')
  for line in f:
    line=line.strip()
    line=line.lower()
    words=line.split()
    for word in words:
      length.append(len(word))
      wordlist.append(word)
  f.close()
  global freq 
  freq=dict(zip(wordlist,length))
  print("Dictionary successfully created!")
  print(freq)

def find_longest_word():
  temp=freq.copy()
  z=int(input("\nEnter no of longest words to be displayed:"))
  for i in range(z):
    lenmax = max(temp, key= lambda x: temp[x])
    print("The longest words are:",lenmax)
    del temp[lenmax]

def filter_long_words(b):
  longest=[]
  temp=freq.copy()
  for j in range(len(freq)):
    longwords = max(temp, key= lambda x: temp[x])
    del temp[longwords]
    if len(longwords)>b:
      longest.append(longwords)
    else:
      break
  print("\nThe words longer than",b,"letters are:",longest)

print("\n-----Part (b)-----")
lenword()
find_longest_word()
filter_long_words(7)

Output:

2) A dictionary Customer contains the following keys {roomno,name,duration}


A binary file “hotel.dat” contains details of customer checked in the hotel. 
Write Code in python to perform the following using pickle module
i) Read n dictionary objects and load them into the file
ii) Read all the dictionary objects from the file and print them
iii) Counts the number of customers present in the hotel. 
(Counts the total number of customers present in the hotel.(Assume that
file might have few record before adding n records in part i)

You might also like