-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_anagram.py
61 lines (48 loc) · 1.67 KB
/
is_anagram.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
'''
Estimated time
10-15 minutes
Level of difficulty
Easy
Objectives
improving the student's skills in operating with strings;
converting strings into lists, and vice versa.
Scenario
An anagram is a new word formed by rearranging the letters of a word, using all the original letters exactly once. For example, the phrases "rail safety" and "fairy tales" are anagrams, while "I am" and "You are" are not.
Your task is to write a program which:
asks the user for two separate texts;
checks whether, the entered texts are anagrams and prints the result.
Note:
assume that two empty strings are not anagrams;
treat upper- and lower-case letters as equal;
spaces are not taken into account during the check - treat them as non-existent
Test your code using the data we've provided.
'''
try:
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
# this is what we're going to do with both strings:
# - remove spaces
str1 = str1.replace(" ", "")
str2 = str2.replace(" ", "")
# - change all letters to upper case
str1 = str1.upper()
str2 = str2.upper()
# - convert string into list
str1 = list(str1)
str2 = list(str2)
# - sort the list
str1.sort()
str2.sort()
# - join list's elements into string
print(str1)
str1 = "".join(str1)
str2 = "".join(str2)
# and finally, compare both strings
# Anagrams must have the same letters used the same amount of time
if str1 == str2:
print("Anagrams")
else:
print("Not anagrams")
# Let's do it!
except BaseException as e :
print("Note: ", e)