1

The assignment is to have an input: "chose a word" from a list if "cat" is given by the user, then I want every word that have: ca_, c_t, _at to be printed out (except cat) and words cant repeat so for example if "car" is in the list twice it should only be printed once. I started with the code but cant manage to finish

def build(wordList):
    dict = {}
    for i in wordList:
        for j in range(len(i)):
            bucket = i[:j] + '_' + i[j+1:]
            if bucket in dict:
                dict[bucket].append(i)
            else:
                dict[bucket] = [i]
    return dict
luk2302
  • 55,258
  • 23
  • 97
  • 137
Mo Pie
  • 41
  • 5
  • Looks good so far. Now if you take any input word, you need to generate all the possible buckets, look up the matching mappings in the mapping you just computer, put all the resulting things in a set to deduplicate and then print that set. – luk2302 Feb 04 '22 at 18:26
  • I will try my best. Thank you! – Mo Pie Feb 04 '22 at 19:03

1 Answers1

0

don't use a dict. Use a set, which enforces unique values.

def build(wordList):
    my_set = {}
    for i in wordList:
        my_set.add(i)
    return dict
  • I have never used a set... Do you have an example of how? – Mo Pie Feb 04 '22 at 18:25
  • https://stackoverflow.com/a/31972583/7361019 – kwaalaateimaa Feb 04 '22 at 18:28
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 05 '22 at 07:27