Start Your Journey with Linux Command Line
| Anagram Listen = Silent |
[1] An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word binary into brainy and the word adobe into abode. The original word or phrase is known as the subject of the anagram. Any word or phrase that exactly reproduces the letters in another order is an anagram.
Now, lets see the lab question.
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:
Note:
Test your code using the data we've provided.
Sample input:
Sample output:
Anagrams
Sample input:
Sample output:
Not anagrams
Can you come up with a shorter / better solution? Please mention it in the comments 👀
References
1. Wikipedia contributors. (2022, February 1). Anagram. Wikipedia. Retrieved February 5, 2022, from https://en.wikipedia.org/wiki/Anagram
An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using every original letter exactly once. For example, "Listen" and "Silent" are anagrams, while "modern" and "norman" are not, because they use different sets of letters. LAB 2.5.1.8 asks you to write a program that reads two separate texts and decides whether they are anagrams, printing "Anagrams" or "Not anagrams" accordingly.
The lab adds a few important rules. Two empty strings are never considered anagrams, spaces must be ignored as if they did not exist, and upper- and lowercase letters should be treated as equal. Handling these rules is where most of the practical work lies, because they affect how the two inputs must be prepared before they can be compared.
The solution in the post centers on a clever trick: two strings are anagrams if and only if they contain the same letters in the same quantities. The simplest way to check that is to sort the characters of both strings and see whether the sorted versions are identical. To apply the rules, the code first removes all spaces from each string with s.replace(" ", ""), then converts everything to uppercase or lowercase with lower() so the comparison ignores case.
Once prepared, the two strings are compared using sorted(s1.lower()) == sorted(s2.lower()). The sorted() function returns a list of the characters arranged in order, and comparing those lists reveals whether both strings are built from the same multiset of letters. If they match, the program prints that the strings are anagrams; otherwise it prints that they are not. This elegant approach avoids any manual counting of individual letters.
replace(" ", "") before comparing.lower() makes the comparison case-insensitive.sorted() versions of the two strings reveals whether they share the same letters.Sorting both strings arranges their characters in the same order. If the two strings are anagrams, their sorted character lists will be identical, since the same letters appear the same number of times.
The line s.replace(" ", "") removes every space character from the string before comparison, effectively treating spaces as if they did not exist.
Because the lab treats uppercase and lowercase letters as equal, converting each string to lowercase with lower() ensures that letter case does not affect the result.
The strings contain different letters. "modern" has a d but no second a, while "norman" has two a's and a different set of letters, so their sorted character lists do not match.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.