Checking strings against each other (Anagrams)

后端 未结 23 1674
忘了有多久
忘了有多久 2020-11-30 10:55

The assignment is to write a program that accepts two groups of words from the user and then prints a \"True\" statement if the two are anagrams (or at least if all the lett

相关标签:
23条回答
  • 2020-11-30 11:02

    Simplest shortest solution

    def anagram(word1, word2):
        return sorted(word1) == sorted(word2)
    

    check

    print(anagram("xyz","zyx"))
    >>True
    
    print(anagram("xyz","zyy"))
    >>False
    
    0 讨论(0)
  • 2020-11-30 11:07

    Just a thought:

    def check_(arg):
            mark = hash(str(set(sorted(arg))))
            return mark
    
    def ana(s1, s2):
            if check_(s1) != check_(s2):
                    pass
            elif len(s1) != len(s2):
                    pass
            else:
                 print("{0} could be anagram of  {1}".format(s1, s2))
    0 讨论(0)
  • 2020-11-30 11:07

        #An anagram is the result of rearranging the letters of a word to produce a new word. Anagrams are case insensitive
        #Examples:
        # foefet is an anagram of toffee
        # Buckethead is an anagram of DeathCubeK
    
        # The shortest my function style *************************************** 
        def is_anagram1(test, original):
            """Сhecks 'test' is anagram of 'original' strings based on:
            1. length of the both string and length of the sets made from the strings is equivalent
            2. then checks equivalents of sorted lists created from test and original strings
    
            >>> is_anagram1('Same','same')
            False
            >>> is_anagram1('toffee','foeftt')
            False
            >>> is_anagram1('foefet','toffee')
            True
            >>> is_anagram1("Buuckk",'kkkcuB')
            False
            >>> is_anagram1('Buckethead','DeathCubeK')
            True
            >>> is_anagram1('DeathCubeK','Buckethead')
            True
            """
            # check the length of the both string
            if len(test) != len(original):
                return False
    
            # check is the strings are the same
            t,o = test.lower(), original.lower()
            if t == o:
                return False
    
            # check the sorted lists
            return sorted(t) == sorted(o)
    
    
        # The final my one line code **************************************
        def is_anagram(test, original):
            """Сhecks 'test' is anagram of 'original' in one line of code
    
            >>> is_anagram('Same','same')
            False
            >>> is_anagram('toffee','foeftt')
            False
            >>> is_anagram('foefet','toffee')
            True
            >>> is_anagram("Buuckk",'kkkcuB')
            False
            >>> is_anagram('Buckethead','DeathCubeK')
            True
            >>> is_anagram('DeathCubeK','Buckethead')
            True
            """
            return False if len(test) != len(original) or test.lower() == original.lower() else sorted(test.lower()) == sorted(original.lower())
    
        if __name__ == "__main__":
            import doctest
            doctest.testmod(verbose=True)
    
    
    ### 2 items passed all tests:
    ### 6 tests in __main__.is_anagram
    ### 6 tests in __main__.is_anagram1
    ### 12 tests in 3 items.
    ### 12 passed and 0 failed.
    ### Test passed
    
    0 讨论(0)
  • 2020-11-30 11:07

    This case we check using two containers for each sorted string.

    def anagram(s1, s2):
        str1 = ''
        str2 = ''
    
        for i in s1:
          str1 += i
    
        for j in s2:
          str2 += j
    
        if str1 == str2:
          return True
    
        return False
    
    0 讨论(0)
  • 2020-11-30 11:09

    Why not just sort the strings?

    >>> sorted('anagram')
    ['a', 'a', 'a', 'g', 'm', 'n', 'r']
    >>> sorted('nagaram')
    ['a', 'a', 'a', 'g', 'm', 'n', 'r']
    >>> sorted('anagram') == sorted('nagaram')
    True
    
    0 讨论(0)
  • 2020-11-30 11:10
    >>> s1 = 'vivid'
    >>> s2 = 'dvivi'
    >>> s3 = 'vivid'
    >>> def is_anagram(s1, s2):
    ...     if s1.lower() == s2.lower():
    ...         return False
    ...     return sorted(s1.lower()) == sorted(s2.lower())
    ...
    >>> is_anagram(s1, s2)
    True
    >>> is_anagram(s1, s3)
    False
    >>> s2 = 'dvivii'
    >>> is_anagram(s1, s2)
    False
    >>> s2 = 'evivi'
    >>> is_anagram(s1, s2)
    False
    >>> 
    
    0 讨论(0)
提交回复
热议问题