ひるあんどんブログ

色々なことに手を出してみるブログ

Verify anagrams

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. Two words are anagrams to each other if we can get one from another by rearranging the letters. Anagrams are case-insensitive and don't take account whitespaces. For example: "Gram Ring Mop" and "Programming" are anagrams. But "Hello" and "Ole Oh" are not.

You are given two words or phrase. Try to verify are they anagrams or not.

Input: Two arguments as strings.

Output: Are they anagrams or not as boolean (True or False)

Example:

verify_anagrams("Programming", "Gram Ring Mop") == True

verify_anagrams("Hello", "Ole Oh") == False

verify_anagrams("Kyoto", "Tokyo") == True


How it is used: Anagramming can be a fun way to train your brain, but they have and another application. Psychologists use anagram-oriented tests, often called "anagram solution tasks", to assess the implicit memory. Anagrams are connected to pseudonyms, by the fact that they may conceal or reveal, or operate somewhere in between like a mask that can establish identity. In addition to this, multiple anagramming is a technique sometimes used to solve some kinds of cryptograms.

Precondition: 0 < |first_word| < 100;
0 < |second_word| < 100;
Words contain only ASCII latin letters and whitespaces.


要はアナグラムになっているかどうかを調べて、ブールで返してくださいという問題。


こうやって考えた。

まず大文字、小文字の別は比較の邪魔でしかないのでlower()で小文字にする。そして、ソートする。
次に空白が邪魔なので、これを取り除く作業をする。

できた文字列がそれぞれ等しいならば、アナグラムであるし、そうでなければアナグラムではない。

def verify_anagrams(first_word, second_word):
    first_word = first_word.lower()
    second_word = second_word.lower()
    
    first_word = list(first_word)
    #first_word = first_word.sort()
    first_word.sort()
    first_word = "".join(first_word)
    print(first_word)
    first_word = first_word.strip()
    
    second_word = list(second_word)
    #second_word = second_word.sort()
    second_word.sort()
    second_word = "".join(second_word)
    second_word = second_word.strip()
    
    if first_word == second_word:
        return True
        
    return False

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert isinstance(verify_anagrams("a", 'z'), bool), "Boolean!"
    assert verify_anagrams("Programming", "Gram Ring Mop") == True, "Gram of code"
    assert verify_anagrams("Hello", "Ole Oh") == False, "Hello! Ole Oh!"
    assert verify_anagrams("Kyoto", "Tokyo") == True, "The global warming crisis of 3002"

ちなみに一回しんだ。list.sort()でソートしたものが返ってくるんだから、これをそのまま変数に入れればいいや。
と思ったらNoneが返ってきて死んだでこざる。

list.sort()はインプレースにソートしてくれてNoneを返すんだね。

list.sorted()とは違うんだよなぁ…。
知りませんでした。

単純な昇順のソートはとても簡単です: sorted() 関数を呼ぶだけです。そうすれば、新たにソートされたリストが返されます:
>>>

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

リストの list.sort() メソッドを呼びだしても同じことができます。この方法はリストをインプレースに変更します(そして sorted との混乱を避けるため None を返します)。多くの場合、こちらの方法は sorted() と比べると不便です- ただし、元々のリストが不要な場合には、わずかですがより効率的です。
>>>

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

違いは他にもあります、 list.sort() メソッドはリストにのみ定義されています。一方 sorted() 関数は任意のイテラブルを受け付けます。
>>>

>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]


ちゃんとチュートリアルに書いてあるし…。しかし、pythonは親切というか生真面目な言語だと感じるなぁ。

さて、一位の人は。


def verify_anagrams(a,b):

    return sorted(a.lower().replace(' ','')) == sorted(b.lower().replace(' ',''))


あっ、replace関数あるじゃん…。
はるかにわかりやすい…。


文字列の変換。空白文字も文字なんだから変換できるじゃん。まわりくどいことする必要なかった。

まーた一つ賢くなってしまいましたなぁ。