-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexe_143_anagrams.py
65 lines (50 loc) · 1.71 KB
/
exe_143_anagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Two words are anagrams if they contain all of the same letters, but in a different order.
For example:
“evil” and “live” are anagrams because each contains one “e”, one “i”, one “l”, and one “v”.
Create a program that reads two strings from the user,
determines whether or not they are anagrams, and
reports the result.
"""
# START Definition of the FUNCTION
def checkAnagrams(word_1, word_2):
# Dictionary of occurrences of strings characters
chars_occurrences = {}
# Dictionary population (with chars of the FIRST string)
for char in word_1:
if char in chars_occurrences:
chars_occurrences[char] += 1
else:
chars_occurrences[char] = 1
# Emptying dictionary (with chars of the SECOND string)
for char in word_2:
if char in chars_occurrences:
chars_occurrences[char] -= 1
if chars_occurrences[char] == 0:
del chars_occurrences[char]
else:
return False
if len(chars_occurrences) > 0:
return False
else: # EMPTY DICTIONARY (the two words are ANAGRAMS)
return True
# END Definition of FUNCTION
# START MAIN PROGRAM
def main():
# Acquisition of DATA entered by the USER
word_1 = input("Enter the FIRST word: ")
word_2 = input("Enter the SECOND word: ")
# STRING TESTING
# word_1 = "evil"
# word_2 = "live"
# String evaluation (anagrams or not)
anagrams = checkAnagrams(word_1, word_2)
# Displaying the RESULTS
if anagrams:
result = "ARE"
else:
result = "ARE NOT"
print("The two words \"{}\" and \"{}\" {} ANAGRAMS.".format(
word_1, word_2, result))
if __name__ == "__main__":
main()