Write a program that reads in the text file attached, converts all words to lowercase, and prints out all words in the file that begin with that key letter a, the letter b, and so on. Build a dictionary whose keys are the lowercase letters, and whose values are sets of words which begin with that key letter. After processing the data, print the dictionary to the screen in alphabetical order aligned in two fixed sized columns. Such as:

Respuesta :

Answer:

Follows are the code to this question:

def dic_sorted(data):#defining a method print_dic_sorted

   s = data.split()#defining a variable that use split method

   d = {} #defining a dictionary

   alpha = '0123456789abcdefghijklmnopqrstuvwxyz' #defining a variable alpha that hold english alphabets values

 for k in alpha:#defining for loop that stores values in the dictionary

       d[k]= set()# use dictionary to set value in list

   for i in s:# defining for loop to convert value in lower case

       i = i.lower()  #convert value into lower case by using lower method  

       a = i[0]# adding value into dictionary

       d[a].add(i) # use add method to hold value in dictionary

   for k,v in sorted(d.items()):#use for loop to convert value in assending order

       if len(d[k])!=0: #use if block to check length of dictionary  

           print(k,":", sorted(v))#use print to sort value            

f_name = 'input.txt'#defining a variable that open file  

with open(f_name) as fi:#use open method to read file

   data = fi.read()    # read file data

   dic_sorted(data)#calling method  

output:

please find the attached file.

Explanation:

In the code, the "dic_sorted" method is used, which accepts a data variable as a parameter, and inside the method, a split method and an empty dictionary are defined, that converts and holds the value by alphabetical order, for this it defines three for loops.

In the first for loop, it holds a value in the dictionary, and in the second loop it converts a value into the lower case, and in the third loop, it stores the value in its alphabetical order.

Ver imagen codiepienagoya
Ver imagen codiepienagoya
Ver imagen codiepienagoya