compare dictionaries
pdlemper at earthlink.net
pdlemper at earthlink.net
Wed Sep 8 01:09:59 EDT 2010
On Tue, 7 Sep 2010 12:46:36 -0700 (PDT), Baba <raoulbia at gmail.com>
wrote:
>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
More information about the Python-list
mailing list