ChecjIO: Verify Anagrams

原创 2016年08月28日 14:39:42

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.

一道水题,将一个单词更换顺序组成另一个单词,大小写不计。
def verify_anagrams(first_word, second_word):
    first_word = first_word.lower()
    second_word = second_word.lower()
    A = ''.join(sorted(list(first_word)))
    B = ''.join(sorted(list(second_word)))
    if A.strip() == B.strip():
        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"



def verify_anagrams(first_word, second_word):
    return sorted(first_word.replace(" ","").lower()) == sorted(second_word.replace(" ","").lower())
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"



版权声明:本文为博主原创文章,未经博主允许不得转载。

anagrams java实现

  • 2011年04月27日 20:34
  • 41KB
  • 下载

vc中ASSERT()和VERIFY()区别

  • 2015年06月09日 19:09
  • 29KB
  • 下载

L2TP部署中ipsec verify出现问题的解决

L2TP部署中ipsec verify出现问题的解决 标签: L2TP 2015-05-30 02:12 1604人阅读 评论(0) 收藏 举报  分类:   VPS(3...

MD5Verify.rar

  • 2010年05月12日 03:32
  • 299KB
  • 下载

qqmail_domain_verify.txt

  • 2011年05月09日 01:25
  • 14B
  • 下载

JAVA结合testng断言verify(断言失败不中断继续执行)

 原理: 1.自已构造一个断言类,把Assert.assertEquals给try catch住。 2.利用testng的监听类在测试方法运行结束后进行分析。 代码: 断言类: ...

assert,assert_valid,verify,trace用法

  • 2013年07月12日 10:31
  • 3KB
  • 下载

python用requests请求百度接口报“SSL: CERTIFICATE_VERIFY_FAILED”

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581) 今天想试用一下百度的语音识别A...

EMAIL verify 软件 v.178

  • 2009年09月01日 10:49
  • 1.24MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ChecjIO: Verify Anagrams
举报原因:
原因补充:

(最多只允许输入30个字)