0

I'm trying to write a function with two input, word, and a dictionary. I already wrote a very inefficient version. The keys of the dictionary are letters with positive integers as keys. The function returns True if all letters in the word are in the dictionary and subtract one from the value every letter in the word from the dictionary or False if any of the letters in the word is not in the dictionary and will not change the value of the dictionary even if some of the letters are present. Here is my version

def isWordIn(word, store):
"""Input: word and store are strings and list respectively
   Output: True or False
""" 
    for letter in word:
        if letter not in store.keys():
            return False

        for letter in word:
            if letter in store.keys():
                store[letter] -= 1
        return True
MCube
  • 37
  • 6
  • this may be a good starting point, to avoid multiple lookups in the store: https://stackoverflow.com/questions/5192753/how-to-get-the-number-of-occurrences-of-each-character-using-python – Ch4ni Oct 13 '18 at 19:07

1 Answers1

0

find(str, beg=0, end=len(string)): Determines whether str occurs in a string and outputs the index of the location. You can limit the search by specifying a beginning index using beg or a ending index using

store.find(word) can be used in your situation without the need to create a seperate function. Remember, this will return the index of the location.

Official Python Docs: https://docs.python.org/3/library/stdtypes.html?highlight=find#str.find

ByWaleed
  • 395
  • 4
  • 18