[Tutor] anagram solving algorithm
Max Russell
max_russell2000 at yahoo.co.uk
Thu Sep 30 10:13:25 CEST 2004
I'm going to sort the input word (i.e the anagram)
then get all words of equal length to the anagram,
sort them, then do a match. I'll return the original
words (unsorted) for all those that match.
--- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
> On Wed, 29 Sep 2004, Max Russell wrote:
>
> > Hello here is a breakdown of an algorithm I'm
> using to write my own
> > anagram solver:
>
> [text cut]
>
> > My problem here is that it how to structure
> looking at each possibe word
> > in the list: how to set up the loop for that, also
> how to do the
> > sort/inspection of each word.
>
>
> Hi Max,
>
> It sounds like you want to run a 'for' loop across
> every word in your
> wordlist.
>
>
> Here's an example of a for loop, just so you get the
> idea of what it can
> do:
>
> ###
> >>> def isEven(x):
> ... return x % 2 == 0
> ...
> >>> numbers = [3, 1, 4, 1, 5, 9, 2, 6]
> >>> for n in numbers:
> ... print "I'm looking at", n
> ... if isEven(n):
> ... print n, "is even"
> ... else:
> ... print n, "is odd"
> ...
> I'm looking at 3
> 3 is odd
> I'm looking at 1
> 1 is odd
> I'm looking at 4
> 4 is even
> I'm looking at 1
> 1 is odd
> I'm looking at 5
> 5 is odd
> I'm looking at 9
> 9 is odd
> I'm looking at 2
> 2 is even
> I'm looking at 6
> 6 is even
> ###
>
>
> For more examples of loops, take a look at a
> tutorial like Alan's:
>
>
>
http://www.freenetpages.co.uk/hp/alan.gauld/tutloops.htm
>
>
> There are two kinds of loops in Python:
>
> 1. for --- good for going across sequences.
>
> 2. while --- good for repeating something until
> some condition
> changes.
>
>
> 'for' can go across lists easily, so it's a good
> tool here. You can loop
> across a list with 'while' too, but it's not so
> idiomatic.
>
>
> Just as a last note: there's a very clever way to
> solve the anagram
> problem, and it does involve sorting. But let's see
> try to get your first
> solution working: we can talk about the clever
> approach later. *grin*
>
>
> Good luck to you!
>
>
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
More information about the Tutor
mailing list