Question

In: Computer Science

Creating a Dictionary (python 3) Description Larry is an immigrant in the USA and has a...

Creating a Dictionary (python 3)

Description

Larry is an immigrant in the USA and has a hard time understanding English there. So he decides to make a software that will tell him the synonyms of the word that he types. He has asked you for help.

Remember, you will first need to choose a data structure that you will use to store the information about the words. You can use lists or dict or tuple or anything else for this purpose.

There will be two actions involved:

Action 1 = Store the following words as synonyms of each other.

Action 2 = Always followed by one word. Print ALL the synonyms of that word in alphabetical order (note: the synonyms printed should not have the query word).


Input:

The input will be one list whose first entry is an int ‘test’ and all other subsequent entries will be lists. ‘test’ will represent the number of lists that follow.

Each of the following lists will consist of a number as its zeroth index (1 or 2) denoting the action to be taken on the words.

Output:

Each action 2 will print the synonyms in the order it is called.



Sample input:

[ 7,

[1,'good','sound','first-class'],

[1,'enough', 'abundant'],

[1,'adequate', 'enough', 'ample'],

[2, 'adequate'],

[2, 'good'],

[1, 'bright', 'illuminous'],

[2, 'cs'] ]

Sample output:

adequate: abundant,ample,enough
good: first-class,sound
cs: 



Explanation:

There are three queries for Action 2 and, hence, the output will have three lines.

Line 1: Adequate has three synonyms ( [1,'enough', 'abundant'], [1,'adequate', 'enough', 'ample'],).

So the three synonyms will be printed in alphabetical order:

Enough: abundant,ample,enough

Line 2: Good has two synonyms ( [1,'good','sound','first-class'] ).

So the two synonyms will be printed in alphabetical order:

Good: first-class, sound

Line 3: there are no synonyms added for cs and hence no synonyms will be printed, but just “cs: ”.(Note space after cs: )
Note that the print order is the same as the order in which ACTION2 is called i.e. adequate, good, cs

Solutions

Expert Solution

#Test case

test = [ 7,

[1,'good','sound','first-class'],

[1,'enough', 'abundant'],

[1,'adequate', 'enough', 'ample'],

[2, 'adequate'],

[2, 'good'],

[1, 'bright', 'illuminous'],

[2, 'cs'] ]

#Function for calculating the Values

#input: values of a key,Synonym dictionary,key

def common(test_values,Syn,t):

#list for having values of keys and Here some values to the corresponding key

#also has values

#such values are stored in a

a = []

for val in test_values:

#taking values of the values of given key

a.append(list(set(Syn[val])^set(test_values)))

#taking union of all the values in the above list

b = (list(set().union(*a)))

#Remove the key as it repeats

b.remove(t)

#sort the array as it is given in question to alphabetically sort

b.sort()

#return the values

return(b)

#Function for creating the Synonyms dictionary

def makelist(test_key,values):

#Here Some keys may already exists

#So first check whether the key is present

try:

#if key exists take its values

l = Synonyms[test_key]

except KeyError:

#if that key does not exist make a new list for the values

l = []

#Append the corresponding values into the list

for x in values:

if x != 1 and x != test_key:

l.append(x)

return l

#initiialize a dictionary for Synonyms

Synonyms = dict()

#Main function to check for print or store in dicitonary

for i in range(1,test[0]+1):

if test[i][0] == 1:

#Make the dictionary

for j in range(1,len(test[i])):

Synonyms[str(test[i][j])] = makelist(test[i][j],test[i])

try:

#Print the dictionary for given key

if test[i][0] == 2:

out = common(Synonyms[str(test[i][1])],Synonyms,test[i][1])

#Here join(map) is used to print list as a values shown in output

print(str(test[i][1]) +' : ' + ','.join(map(str,out)))

#If the given key does'nt exists print a space ' '

except KeyError:

print(test[i][1] + ' : ')

#output:


Related Solutions

How to apply accounting perspective to solve immigrant assimilation in USA?
How to apply accounting perspective to solve immigrant assimilation in USA?
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Description of Variables/Data Dictionary: The following table is a data dictionary that describes the variables and...
Description of Variables/Data Dictionary: The following table is a data dictionary that describes the variables and their locations in this dataset (Note: Dataset is on second page of this document): Variable Name Location in Dataset Variable Description Coding UniqueID# First Column Unique number used to identify each survey responder Each responder has a unique number from 1-30 SE-MaritalStatus Second Column Marital Status of Head of Household Not Married/Married SE-Income Third Column Total Annual Household Income Amount in US Dollars SE-AgeHeadHousehold...
in Python 3, I need to search a dictionary for the matching key while ignoring case...
in Python 3, I need to search a dictionary for the matching key while ignoring case of the string and the key. then print the dictionary item in it's original case. here is the code I have so far but the compiler isn't liking that the name string is lowercase ``` dictionary = {"Dan Smith": {"street":"285 Andover Lane", "city":"Pompano Beach", "state":"FL", "zip":"33060"},"Parker Brady": {"street":"7416 Monroe Ave.", "city":"Windsor", "state":"CT", "zip":"07302"}} name ="dan smith" line= dict(filter(lambda item: name.lower() in item[0], dictionary.items())) print("Street:%5s...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to their squares. Assign the dictionary to the variable squares b. Given the list value_list, assign the number of nonduplicate values to the variable distinct_values
#Python Given a dictionary gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C":...
#Python 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 the key’s data value....
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
How can SNOMED CT help in creating a data dictionary?
How can SNOMED CT help in creating a data dictionary?
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names to addresses. A person's name (stored as a string) will be the KEY and that person's address (stored as a string) will be the VALUE. 1.2) Insert into the dictionary 'addresses' the names and addresses of two (possibly imaginary) friends of yours. 1.3) Create a second empty dictionary called 'ages'. The dictionary you just created will map names to ages. A person's name (stored...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT