Question

Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :)

Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_

Here is the questions in this assignment:

«Assignment 4 Questions draw rows () Define the draw rows () function which is passed a Python dictionary as a parameter The# 2222222222222222222222222222222222222222222222222 # get text-valuation() Define the get_text valuation() function which is# word-len-dict() get Define the get_word len dict() function which is passed a string of text as a parameter. The function r# 4444444444444444444444444444444444444444444444444 # get-triples-dict() Define the get triples diet) funetion which is passe

Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs in sorted key order. The testing code for function 3 in this assignment makes use of the remove less than 2(a dict) function which removes any keyvalue pairs which have a corresponding value of 1 from the dictionary. # Two helper functions def print_dict in_key order (a dict): all-keys = list(adict.keys()) all_keys.sort) for key in all keys: print (key,a dict [key) def remove less than 2 (a dict): all keys 1ist(a_dict.keys() for key in all keys: if a-dict[key] It del a dict[keyl
«Assignment 4 Questions draw rows () Define the draw rows () function which is passed a Python dictionary as a parameter The keys of the dictionary are integers and the corresponding values are strings, e.g., (4: a', 2: f 0 x4: z'. For each key, followed by:followed by a string which is a repetition of the corresponding value. The number of times the corresponding value printed is given by the key value. The keys are printed in numeric order. Note that nothing is printed if the key is a number less than l key-value pair in the dictionary the function prints the For example, the following code: print(. print print(2." draw rows (4: a20x,423 2' prints: 2. 2: ff def draw rows (histogram dict): pass
# 2222222222222222222222222222222222222222222222222 # get text-valuation() Define the get_text valuation() function which is passed two parameters, a dictionary and a string of text. The keys of the parameter dictionary are single letters and the corresponding values are integers (the value of the key letter), e.g., ('b': 5, 'a': 6, c': 3). The function returns the total valuation (an integer) of the string of text where: . if the letter from the text is a keyletter of the dictionary then its value is the integer corresponding to the letter in the dictionary " any alphabetic characters from the text which are not in the dictionary are worth 1, and, all non alphabetic characters are worth 0 (use the isalpha() method to check if a character is alphabetic or not). Notes: you will need to change the text to lowercase before you work out the total value of the text . you can assume that all the keys in the dictionary are lowercase characters. For example, the following code: 2, "s": 2, "h":4, "t":3, "m": 7, "g", "v":8) {"r": letter-value, dict letters"BLAH" print("1.", letters, "-", get_text_valuation(letter_value_dict, letters)) letters 'thought provoking print("2.", letters, "-*, get_text_valuation(letter_value_dict, letters)) letters"too much month at the end of the money" print("3.", letters, "-", get_text_valuation(letter_value_dict, letters)) prints: 1. BLAH 7 2: tbou mbeh rowth as the end of the money - 70 3. too much month at the end of the money def get_text_valuation(1etter_worth dict, text) return 0
# word-len-dict() get Define the get_word len dict() function which is passed a string of text as a parameter. The function returns a dictionary with keys which are integers and corresponding values which are lists of unique words. The list of words corresponding to a key contains all the unique words from the text that have a length equal to the key value. The corresponding lists of unique words should be in sorted alphabetical order. Note: the testing code makes use of the print_dict_in_key_order(a dict) which prints the dictionary pairs in sorted key order. For example, the following code: text "May your coffee be strong and your Monday be short" the dict -get_word_len_ dict (text) print_dict_in key_order(the dict) print() text why does someone believe you when you say there are four billion stars but they have to check when you say the paint is wet' the dict get_word_len_dict(text) print dict_in_key_order (the dict) prints: 2 ['be'] 3 I'May' 'and' 5 : ['short'1 6 : I'Monday, 'coffee, 'strong'] 2: 'is', 'to' 3 ['are', 'but', 'say,'the', 'wet', 'why, 4 : I'does','four', 'have, 'they, 'when' 1 5 : ['check', 'paint', 'stars', 'there'] 7: I'believe', 'billion', 'someone' 1 'why''you' def get_word_len_dict (text): return (th
# 4444444444444444444444444444444444444444444444444 # get-triples-dict() Define the get triples diet) funetion which is passed a string of text as a The function first converts the parameter string to lower case turns a dictionary with keys which are all the unique consecutive tic characters from the text, and the corresponding values are characters appear in parameter. and then re three alphabe the number of times the three consecutive alphabetic text. Use the isalpha() method to check if a character is alphabetic or the not. The dictionary After your dietionary has been created and populated, you need to remove any key-value pairs which have a corresponding value of 1. For example, text is "Super, duper" the algorithm proceeds as follows: should only contain entries which occur more than once. Character 's' String is s,Dictionary is Character u': String is "su", Dictionary is ( Character 'p': String is "sup", change string to "up" Dictionary is ('sup': 1) Character e: string is "upe", change string to "pe", Dictionary is ('sup': 1, upe: 1) Character 'r': string is "per" change string t er Dictionary is ('sup 1, upe 11, per': 1) upe' 1, per CharacterString is er, Dict tonary is 'sup Character String is erDictionary is 'sup1, upe': 1, per: Character d': String is "erd", change string to rdDictionary is E'sup upe1, per'1, erd': 1 Character 'u':Stringis "rdu change string to "du Dictionary is Dictionary is Character 'p': String is "dup change string to "up Character 'e': String is "upe change string to pe" Dictionary is Character 'r: String is "per", change string to erDictionary is Remove all entries with a value of 1: Dictionary is ('upe': 2, per 2) For example, executing the following code: print("1.*) print dict in key order(get_triples dict( super, duper)) print("In2.") print dict-in key-order( get-triples, dict( "ABC ABC ABC- print( n3.) print dict in key order(get triples dict( "Sometimes the smallest things make more room in your heart")) print("An4.) print dict in key order(get triples dict("My favourite painting is the painting i did of my dog in that painting in my den)) 3. est -2 sma -2 prints: (output is shown here in four ain3 abc-3 bca -2 per -2 upe 2 gin 2 ing -3 int -4 myd 2 ngi - 3 nti3 cab -2 separate columns) tin 3 def get triples dict (text): return t)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLANATION IN CODE COMMENTS :

def remove_less_than_2(dic):
all_keys = list(dic.keys())
for key in all_keys:
if dic[key]==1:
del dic[key]
return dic
def print_dic_in_key_order(dic):
#print dictionary content in sorted key order
for key in sorted(dic):
print(key ,":", dic[key])
print()
  
def draw_rows(dic):
#run two loops and print the value key times
for key in sorted(dic):
print(key ,":",end =" ")
for i in range(key):
print(dic[key],end ="")
print()
print()
  
def get_text_valuation(dic,text):
sumA=0
#loop over text , if character is alphabet and it exits in dictionary then add value to sum
#else add 1 to the sum
for i in text.lower():
if i.isalpha()==True:
if i in dic.keys():
sumA+=dic[i]
else:
sumA+=1
return sumA

"""
33333333333333333333333333333333333333333333333333

"""
def get_word_len_dict(text):
l = text.split(" ")
dic = {}
#for each word in the list, get its length
#if len exists as key in dictionary , append the word to it
#else create a new entry in the dictionary
for i in l:
key = len(i)
if key in dic.keys():
if i not in dic[key]:
dic[key].append(i)
else:
dic[key]=[i]
for value in dic.values():
value.sort()
return dic

"""
444444444444444444444444444444444444444444444444444

"""
def get_triples(text):
dic = {}
s=""
#loop over text as lower() , take string as s
#in each iteration add each character if it is alphabet to s
#when length of s reaches 3, if it exists in the dictionary , add one to it
#else create a new entry
for i in text.lower():
if i.isalpha() == True:
s += i
if len(s)==3:
if s in dic.keys():
dic[s]+=1
else:
dic[s]=1
s = s[1:]
#remove all the entries in the dictionary whose value is == 1
return remove_less_than_2(dic)

dic = {4:'a',7:'c',4:'b'}
draw_rows(dic)

dic = {"r":2,"s":2,"h":4,"t":3,"m":7,"g":4,"v":8}
letters = "BLAH"
print("1. ",letters," - ",get_text_valuation(dic,letters),"\n")

text = "May your coffee be strong and your monday be short"
dic = get_word_len_dict(text)
print_dic_in_key_order(dic)

text = "super , duper"
print_dic_in_key_order(get_triples(text))

Add a comment
Know the answer?
Add Answer to:
Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey guys I need help with this question: # 4444444444444444444444444444444444444444444444444 # get-triples-dict() Define the get triples...

    Hey guys I need help with this question: # 4444444444444444444444444444444444444444444444444 # get-triples-dict() Define the get triples diet) funetion which is passed a string of text as a The function first converts the parameter string to lower case turns a dictionary with keys which are all the unique consecutive tic characters from the text, and the corresponding values are characters appear in parameter. and then re three alphabe the number of times the three consecutive alphabetic text. Use the isalpha() method...

  • Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti s...

    Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti synonyms function which is passed a dictionary as a parameter- The keys of the parameter dictionary are words and the corresponding values are 1ists of synonyms (synonyms are words which have the same or nearly the same meaning). The function romoves all the eynonyme which have ous than 8 charactors from each corresponding list of synonyms-As well, the funet...

  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...

  • The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = ""...

    The skeleton code (starter file) for the problem is pasted below: def phoneNumber(letters): result = "" # ADD YOUR CODE HERE return result def loadEvents(filename): events = {} # ADD YOUR CODE HERE return events def timeline(events): # ADD YOUR CODE HERE return # DO NOT modify or remove the code below! We will use it for testing. if __name__ == "__main__": # Problem 1: Decoding Telephone Numbers ph = input("Enter the text to translate: ") print("The corresponding phone number...

  • Hey I have a task which consists of two part. Part A asks for writing a...

    Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it. Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION  to use these implemented code and show the time...

  • I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the...

    I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the parameter list - 4 marks Os Define the get_longest_e_word() function which is passed a list of strings as a parameter. The function returns the word in the list which has the most characters (i.e., the longest word) BUT only words which have 6 or more characters and contain the letter...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • hello. i need help with number 2 ONLY 1. Use if statements to write a Java...

    hello. i need help with number 2 ONLY 1. Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way 2=ABC 3 = DEF 4 GHI 5 JKL 6 - MNO 7 - PRS 8 - TUV 9-WXY No digit corresponds to either Qor Z. For these 2 letters your program should print a message indicating that they...

  • In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

    In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line: At first, I have an empty dictionary then to that I...

  • Question #2 A DNA molecule can be specified using a string of the characters , ‘g...

    Question #2 A DNA molecule can be specified using a string of the characters , ‘g 'a', 't'. Each of these characters represents one of the four nucleobases Cytosine, Guanine, Adenine, and Thymine. Consult the wikipedia page on DNA for more details. A codon consists of a sequence of three DNA nucle- obases and can be represented by a string of length 3 consisting of characters from the set ‘c', ‘g', ‘a", ‘t'. So "cga" and "ttg" are examples of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT