Question

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 k# 6666666666666666666666666666666666666666666666666 # contains-keys-and values() fine the contains keys and values) function+ get_previous words dict) Define the get previous words dict () function which is passed a string of text as a parameter. Th

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 ion sorts each corresponding list of synonyms- For example, the following code: synonyms dict('look : 'gaze see, 'glance, 'watch', 'peruse' 1, 'put': I'place, 'set, attach , keep', 'save', 'set aside', effect', achieve', "dobuild' 1 beautiful l 'prettyovely handsome' 'dazzling', 'splendid', magnificent'] slow: I 'unhurried, 'gradual'leisurely', 'late, 'behind', tediousslack 1, dangerous'periloushazardous, 'uncertain' remove short_synonyms ( synonyms dict) print("1) print dict in key ordertsynonyms dict) printo synonyms dict 'come ('approach 'advance' near, 'arrive, reach show E'displayexhibit, 'present, note, 'point to'. indicate 'explain reveal', prove demonstrate', 'expose'1, good'excellent fine superior wonderful', 'grand', superb edifying 1 bad: 'evil',immoral wicked rotten'contaminated, spoiled'. defectivesubstandard, faulty improper, inappropriatel remove short synonyms (synonyms dict) print( "2.") print dict in key order(synonyms dict) prints: dangerous : [ hazardous' perilous'uncertain' ] look : ( put : ['set aside' 1 slow s ['leisurely, 'unhurried' l bad : [ 'contaminated', 'defective', improper, 'inappropriate ' substandard' 1 come : approach 1 good : I'edifying, excellent', 'superior', 'wonderful'l show [ demonstrate', 'indicate', point tol def remove short_synonyms (synonyms dict): pass
# 6666666666666666666666666666666666666666666666666 # contains-keys-and values() fine the contains keys and values) function which is passed two dict objects as parameters, dicti and dict2. The two parameter ries both have corresponding values which are lists of elements (numbers or strings). The function return True if the following conditions are met: . dictl contains all the keys which are in dict2 (dictl may contain extra keys), and, . the elements in all the corresponding value lists of diet2 are also elements in one or more of the corresponding value lists of dictl. Note: when testing this part of the condition, the elements can be in any order and in any of the corresponding value lists, e.g., if one of the corresponding values lists of dict2 is [4, 2] and any one of the corresponding value lists of dictl contains the element 4 and any one of the corresponding value lists of dicti contains the element 2, this part of the condition is satisfied. The function returns False in al1 other cases. For example, the following code: dictl 0 dict2 = {} print( "1." contains keys and values (dicti, dict2)) dicti f'a: [4, 3 d 16, 21,"z": "t":[2, 231) dict2 [2, 3, 6, 23 a: 141) print("2., contains keys and values (dicti, dict2)) dict2t"a" (3, 6, 3, "p [6, 11) print ("3., contains_keys and values(dictl, dict2)) dicti "a": (6, 3, "p": [1) print("4.", contains_keys and values(dictl, dict2)) dictl ("a": (6, 3, "p": 'a'l, "t": I's'1 dict2 = {"a": [3, 6, 31, "p": ['a, ] , "s". I'ah print ("5., contains keys and values(dictl, dict2)) prints: 1. True 2. True 3. False 4. False 5. False def contains_keys_ and_values (filename) pass
+ get_previous words dict) Define the get previous words dict () function which is passed a string of text as a parameter. The function returns a dictionary with keys which are all the unique words from the text, and, the corresponding values which are lists of all the unique words in the text which come before the key word. Note that in each corresponding list of previous words, the same word should not appear more than once. Notes: the first word in the sentence will initially have the empty string as its previous word, , you can assume that the text is all in lower case and contains no punctuation , the testing code makes use of the print dict_in key order (a dict) which prints the dictionary pairs in sorted key order. , each list of previous words must be sorted into ascending order. For example, the following code: sentence 'a man we saw saw a saw previous words dict get previous_ words dict (sentence) print dict in key order(previous_words_dïet) print() sentence, my favourite painting is the painting i did of my dog in that previous-words-dict get-previous-words-dict (sentence) print dict in key order(previous_words_dict) prints: man : ['a saw : t'a, saw', 'we'1 we: Iman dog : [my 1 favourite : [my'1 í : ['painting') n : ['dog', painting] is : [ painting' 1 of : I'did'] painting : ['favourite', 'that' 'the'l the : ['is def get previous words dict (text) return t)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: I Hope the below solution meets your requirements as per the Question. Please Do UPVOTE if it Does. :)

All test cases of the question are being checked and shown in below screenshots.

## Python Code:

## Part 1 of the problem
print()
print('-'*10,'PART 1 OF THE PROBLEM','-'*10)
print()


## Function to remove the short synonyms
def remove_short_synonyms(synonyms_dict):
# Iterating each key,value pair in the dictionary
for key,value in synonyms_dict.items():
# Empty list to store the new values for the key
new_value = []
# checking the lenght of the words in the value
for word in value:
if len(word) >= 8:
new_value.append(word)
synonyms_dict[key] = new_value
# returning the dictionary
return synonyms_dict

## Function to print the dictionary in sorted order
def print_dict_in_key_order(synonyms_dict):
for key,value in sorted(synonyms_dict.items()):
print(key,':',sorted(value))


# Storing the words with their synonyms in the form of dictionary
synonyms_dict = {'look' : ['gaze', 'see', 'glance', 'watch', 'persue'],
'put' : ['place', 'set', 'attach', 'keep', 'save', 'set aside', 'achieve', 'do', 'build'],
'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificient'],
'slow' : ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
'dangerous' : ['perilous', 'hazardous', 'uncertain']
}

# Removing the short synonyms from the dictionary
synonyms_dict = remove_short_synonyms(synonyms_dict)
print('1.')
# printing the dictionary
print_dict_in_key_order(synonyms_dict)
print()

# Storing the words with their synonyms in the form of dictionary
synonyms_dict = {'come' : ['approach', 'advance', 'near', 'arrive', 'reach'],
'show' : ['display', 'exhibit', 'present', 'note', 'point to', 'indicate', 'explain', 'reveal', 'prove', 'demonstrate', 'expose'],
'good' : ['excellent', 'fine', 'superior', 'wonderful', 'grand', 'superb', 'edifying'],
'bad' : ['evil', 'immoral', 'wicked', 'rotten', 'contaminated', 'spoiled', 'defective', 'substandard', 'faulty', 'improper', 'inappropriate']
}

# Removing the short synonyms from the dictionary
synonyms_dict = remove_short_synonyms(synonyms_dict)
print('2.')
# printing the dictionary
print_dict_in_key_order(synonyms_dict)
print()
print()

## Part 2 of the problem
print()
print('-'*10,'PART 2 OF THE PROBLEM','-'*10)
print()

# Function to check the donditions of dict1 and dict2
def contains_keys_and_values(dict1, dict2):
# If any of the condition fails return False else return True
# checking if dict1 contains all the keys of dict2
for key in dict2.keys():
if key not in dict1.keys():
return False
dict1_values = []
# checking if the values of dict2 is in any corresponding value list in dict1
for value in dict1.values():
dict1_values.extend(value)
for value in dict2.values():
for ele in value:
if ele not in dict1_values:
return False
# if both the condition is satisfied return True
return True

# Checking the working of the contains_keys_and_values function for different set of values
dict1 = {}
dict2 = {}
print('1.', contains_keys_and_values(dict1, dict2))
dict1 = {'a': [4,3] , 'd' : [6,2], 'z':[], 't': [2,23]}
dict2 = {'z': [2,3,6,23], 'a':[4]}
print('2.', contains_keys_and_values(dict1, dict2))
dict1 = {'a': [6,3], 'p':[]}
dict2 = {'a': [3,6,3], 'p':[6,1]}
print('3.', contains_keys_and_values(dict1, dict2))
dict1 = {'a': [6,3], 'p': []}
dict2 = {'a': [3,6,3], 'p': ['a']}
print('4.', contains_keys_and_values(dict1, dict2))
dict1 = {'a': [6,3], 'p': ['a'], 't': ['s']}
dict2 = {'a': [3,6,3], 'p':['a'], 's': ['a']}
print('5.', contains_keys_and_values(dict1, dict2))

## Part 3 of the problem
print()
print('-'*10,'PART 3 OF THE PROBLEM','-'*10)
print()

# Function to get the previous words of the dictionary
def get_previous_words_dict(text):
# converting the text into list
text = text.split()
# creating an empty dictionary to store the unique words with value as previous words
previous_words_dict = {}
# getting the unique words from the text in the sorted order
keys = sorted(set(text))
# iterating through each key
for key in keys:
# Empty list to store the previous words
previous_words = []
# finding the prevous words for the key
for index in range(len(text)):
if key == text[index]:
if index == 0:
previous_words.append('')
else:
previous_words.append(text[index-1])
# adding the key with previous words list to the dictionary
previous_words_dict[key] = previous_words
# returning the dictinoary
return previous_words_dict

# Checking the working of get_previous_words_dict function for different values
sentence = 'a man we saw saw a saw'
previous_words_dict = get_previous_words_dict(sentence)
print_dict_in_key_order(previous_words_dict)
print()

sentence = 'my favourite painting is the painting i did of my dog in that painting in my den'
previous_words_dict = get_previous_words_dict(sentence)
print_dict_in_key_order(previous_words_dict)

## Screenshots of the Code in IDLE:

## Screenshots of the Output:


Add a comment
Know the answer?
Add Answer to:
Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti s...
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 assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    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 :) Here is the questions in this assignment: 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...

  • 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...

  • PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of...

    PYTHON Define the remove_e_synonyms() function which is passed a dictionary as a parameter. The keys of the parameter dictionary are words and the corresponding values are lists of synonyms (synonyms are words which have the same or nearly the same meaning). The function removes all the synonyms which contain the letter 'e' or 'E') from each corresponding list of synonyms. As well, the function sorts each corresponding list of synonyms. Note: the testing code makes use of the print_dict_in_key_order(a_dict) which...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • # 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...

  • python please 11 def add_item(items, word): 14 15 16 F # check if the word is...

    python please 11 def add_item(items, word): 14 15 16 F # check if the word is in the dictionary (keys of dictionary) if word in items: items [word] = items (word] + 1 # update the count else: # word is not in dictionary items[word] = 1 # add the word with count 1 return items [word] # return the current count of word after updation Create a function named build_dictionary that takes a list of words (as a parameter)...

  • I need help writing these : Given a dictionary gradeCounts = { "A": 8, "D": 3,...

    I need help writing these : Given a dictionary gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6} write the Python statement(s) to print: a. all the keys. b. all the values. c. all the key and value pairs. d. all of the key and value pairs in key order. e. the average value. f. a chart similar to the following in which each row contains a key followed by a number of asterisks equal to...

  • 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 in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all 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...

    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...

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