In: Computer Science
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
#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: