word= 'even'
dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
i want to know if word is entirely composed of letters in dict2
my approach:
step 1 : convert word to dictionary(dict1)
step2:
for k in dict1.keys():
if k in dict2:
if dict1[k] != dict2[k]:
return False
return True
return False
return True
by adding a print statement i can see that this simply ends too early
e.g. as soon as the first IF condition is met the loop exits
i think this is easy but google and python doc didn't return any good
hints so i'm trying here.
Thanks Baba
set(word) <= set(dict2.keys())
Just go through each letter of word checking for its existence in
dict2. Return False if one misses, and True if you get through the
whole word:
def ...():
for c in word:
if c not in dict2:
return False #if any character is not in dict
return True # otherwise
If you know of generator expressions, and remember that True and False
are 1 and 0 respectively, then this works
def ...():
return sum(c in dict2 for c in word) == len(word)
Gary Herron
--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
ok but how do we address the fact that letter e needs to have the
value 2 in the dictionary if it was to be True? in my example this
condition is not met so the check would return False. Word is not
entirely composed of letters in dict2, one of the letter is not in
dict2 i.e. the 2nd e
So finding a matching key seems to be the easy part, checking if the
number of ocurrences of letter in 'word' == letter.value seems to be
the tricky part
Do the numbers in dict2 represent the maximum number of times that the
letter can be used?
If yes, then build a similar dict for the word with the number of times
that each letter occurs in the word and then check for every pair in
the dict whether the key (ie, letter) occurs in dict2 and that the
value (number of occurrences) isn't too many.
Just compare the two dictionaries
dict1 == dict2
Or, if you want to allow dict2 to contain higher but not lower values
all(v <= dict2.get(k, 0) for k, v in dict1.iteritems())
Peter
Hi MRAB
Thanks for the hint. In my case i need to do the opposite: the number
of times that each letter ocurs in the word needs to be smaller or
equal to the number of times it apears in dict2. That way i am
guaranteed that word is entirely made up of elements of dict2.
Your hint pointed me in the right direction.
for k in word.keys():
if k not in hand:
return False
elif k in hand:
if word[k] > hand[k]:
return False
return True
Baba
Untested:
all(word[k] <= hand.get(k,0) for k in word)
Huh??? I answered the problem as it was stated in the email -- it said
nothing about *counting* the occurrences of letters. In order to not
waste our (voluntary) time, perhaps you should carefully re-state the
problem you'd liked solved. Then we'll see what we can come up with.
> So finding a matching key seems to be the easy part, checking if the
> number of ocurrences of letter in 'word' == letter.value seems to be
> the tricky part
>
>
for k in word.keys():
if k not in hand:
return False
else:
if word[k] > hand[k]:
return False
return True
This can be shortened still further.
>level: beginner
>
>word= 'even'
>dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
>
>i want to know if word is entirely composed of letters in dict2
>
>my approach:
>step 1 : convert word to dictionary(dict1)
>
>step2:
>for k in dict1.keys():
> if k in dict2:
> if dict1[k] != dict2[k]:
> return False
> return True
> return False
> return True
>
Assign letters to their corresponding primes with the
following function -
def alphaprime(c) :
if c == 'a' return 2
elif c == 'b' return 3
elif c == 'c' return 5
...
elif c == 'y' return 97
elif c == 'z' return 101
else : return 0
Using above calculate a composite for the letters in the
dictionary. Of course begin with 1 and multiply by the
associated prime for each letter and repeat for each
recurrence of that letter. Call this dictionarycomposite.
Do the same for the word, parsing sequentially and
multiplying by the prime for each letter. Call this
wordcomposite.
Now if
dictionarycomposite % wordcomposite == 0
the word can be spelled with the letters in the dictionary.
I used this in a word game : works fast.
The Fundamental Theorum of Arithmetic may apply.
Dave WB3DWE