#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Need a anagram function


    Hi Friends

    I need a function that takes two strings and returns True if they are anagrams.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by chetansaip
    Hi Friends

    I need a function that takes two strings and returns True if they are anagrams.

    Thanks
    Code:
    #!/usr/bin/python                                                                                                           
    
    def is_anagram(str1, str2):
        return sorted(list(str1.lower().replace(' ', ''))) == sorted(list(str2.lower().replace(' ', '')))
    
    if __name__ == '__main__':
        print is_anagram('George Bush', 'He bugs Gore')
    You're welcome!

    Comments on this post

    • b49P23TIvg agrees : Finally! Anagram tests on sorted lists. With spaces removed! Great anagram.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Giving ERROR


    Hi b49P23TIvg

    I'm using Python 3 and this code giving me error:

    SyntaxError: invalid syntax
    >>>

    And a red highlight on "if"
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by chetansaip
    Hi b49P23TIvg

    I'm using Python 3 and this code giving me error:

    SyntaxError: invalid syntax
    >>>

    And a red highlight on "if"
    There is no "if" statement in the code above. Did you post this to the wrong thread?
    http://www.catb.org/esr/faqs/smart-questions.html#code
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Want the user to input anagram values in CMD


    Hi

    I want to write script like this:

    Code:
    x = input("Give me Word1:")
    str1 = str(x)
    y = input("Give me Word2:")
    str2 = str(y)
    
    def is_anagram(str1, str2):
    return sorted(list(str1.lower().replace(' ', ''))) == sorted(list(str2.lower().replace(' ', '')))
    
    if __name__ == '__main__': print (is_anagram(str1, str2))

    can u please look into this and modify this with out errors, i'm using python 3
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by chetansaip
    Hi

    I want to write script like this:

    Code:
    x = input("Give me Word1:")
    str1 = str(x)
    y = input("Give me Word2:")
    str2 = str(y)
    
    def is_anagram(str1, str2):
    return sorted(list(str1.lower().replace(' ', ''))) == sorted(list(str2.lower().replace(' ', '')))
    
    if __name__ == '__main__': print (is_anagram(str1, str2))

    can u please look into this and modify this with out errors, i'm using python 3
    Code:
    def is_anagram(str1, str2):
        return sorted(list(str1.lower().replace(' ', ''))) == sorted(list(str2.lower().replace(' ', '')))
    
    if __name__ == '__main__':
        x = input("Give me Word1:")
        str1 = str(x)
        y = input("Give me Word2:")
        str2 = str(y)
    
        print (is_anagram(str1, str2))
    http://www.catb.org/esr/faqs/smart-questions.html#code
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Thanks So much


    Thanks so much Partoj. I got the answer.

    If there is any chance, I have got 2 more questions in this forum, can u please look into them:

    http://forums.devshed.com/python-programming-11/need-answer-for-a-python-interview-question-943290.html

    http://forums.devshed.com/python-programming-11/new-to-python-need-help-943145.html

IMN logo majestic logo threadwatch logo seochat tools logo