나는 이렇게 학습한다/Algorithm & SQL

Anagram Detection

daco2020 2022. 7. 21. 17:48
반응형

An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).

Note: anagrams are case insensitive

Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.

Examples

  • "foefet" is an anagram of "toffee"
  • "Buckethead" is an anagram of "DeathCubeK"

 

Solution:

def is_anagram(test, original):
    if len(test) < len(original):
        test, original = original, test
    arr = [i for i in test.lower()]
    [arr.remove(i) for i in original.lower() if i in arr]
    return not arr

Subtract a smaller string from a larger string to see if they match.

 

 

Best Practice:

def is_anagram(test, original):
    return sorted(original.lower()) == sorted(test.lower())

Sorting and comparing makes it easier to check.

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

What is between?  (0) 2022.07.24
Replace With Alphabet Position  (0) 2022.07.22
Sum of all the multiples of 3 or 5  (0) 2022.07.20
SQL Basics: Simple JOIN with COUNT  (0) 2022.07.19
SQL Basics: Simple HAVING  (0) 2022.07.19