4 Replies - 2578 Views - Last Post: 21 March 2013 - 02:44 AM Rate Topic: -----

#1 reetpook  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-February 13

Check if Two Strings are Anagrams of Each Other

Posted 17 March 2013 - 12:46 PM

hey

could someone help me writing an algorithm for this. i dont even know where to begin.


Two (or more) words are anagrams if and only if the letters in one can be rearranged to form the other.
For example, the words canter, nectar, recant and trance are all anagrams; they each consist of the same
letters, just in a different order. For this program, you'll write a program that reads a list of words; then
produce a list of how many anagrams each word has, and the largest list of words that are anagrams of
each other.


this is an example our professor gave in class.
>>> ================================ RESTART ================================
>>>
Name of input file? shortwordlist.txt
There were 5 words with 0 anagrams.
There were 4 words with 1 anagrams.
There were 6 words with 2 anagrams.
There were 4 words with 3 anagrams.
The highest anagram count found was 3
The following words have 3 anagrams:
cater
crate
react
trace
>>>

This post has been edited by macosxnerd101: 17 March 2013 - 03:47 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Check if Two Strings are Anagrams of Each Other

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Check if Two Strings are Anagrams of Each Other

Posted 17 March 2013 - 03:46 PM

Please use a descriptive title. "Please help" is not a descriptive title, nor is "Algorithm for this."

Big hint- sort the letters. If the two ordered lists are equal, then the words are anagrams.
Was This Post Helpful? 2
  • +
  • -

#3 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: Check if Two Strings are Anagrams of Each Other

Posted 20 March 2013 - 10:39 PM

Something like this perhaps:

input1 = "words"
input2 = "sdrow"

assert sorted(list(input1), key=str.lower) == sorted(list(input2), key=str.lower)



Simply turns the strings into a list that can be sorted and then uses string.lower to ignore case then uses the natural alphabetical order to sort them, then just looks if they are the same. should work fine.
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is online

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: Check if Two Strings are Anagrams of Each Other

Posted 21 March 2013 - 01:44 AM

There is no need to turn the strings into lists before using sorted. Sorted automatically returns a sorted list if the argument is a string. Also, using assert for this strikes me as a little strange. Assert is used for checking if something is the value you expect; and then raises an Assertionerror exception if the case is otherwise. Not really appropriate in this situation in my opinion.

def are_anagrams(one,two): return sorted(one.lower())==sorted(two.lower())


-Mek
Was This Post Helpful? 1
  • +
  • -

#5 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: Check if Two Strings are Anagrams of Each Other

Posted 21 March 2013 - 02:44 AM

I wasn't 100% sure on the sorted on a string but i figured i would be safe rather then wrong.

I know its may be an annoying habit but when i do code snippets for forums i tend to write them like tests to easily show it works. My bad, just my weird style.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1