From: Michael Fellinger Date: 2005-12-08T17:11:09+09:00 Subject: Re: is this a good way to find anagrams? --nextPart2139708.tvCfTPD2iy Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hey travis, you gave me a good time figuring this one out... after some thought i came = up=20 with something that takes roughly 1 second to search 9,6k words for a=20 anagram. of course this will go up when you add more words (a task you still can thi= nk=20 about) all in all, 5 LoC... but i'm sure there are even better ways... word =3D "easy".split(//).sort anagrams =3D IO.readlines('/usr/share/dict/british-english').partition {|l| l.strip! (l.size =3D=3D word.size && l.split(//).sort =3D=3D word) }[0] anagrams.each{|x| puts x} ##### gives you: manveru@lambda:~/ruby$ time ruby anagramma.rb ayes easy yeas real 0m1.097s user 0m0.936s sys 0m0.105s Am Donnerstag, 8. Dezember 2005 06:52 schrieb travis laduke: > it seems to me, with computers these days, this should finish > instantly, not take like 20 seconds. > also, please help me make my ruby more ruby-like. i'm new to ruby, > not that i know any other language. > > > ##these are here from when i was first testing > secret_word =3D "spine" > rack =3D "spine" > > secret_word =3D secret_word.split(//) > rack =3D rack.split(//) > test_rack =3D rack.to_s > > ##are there enough of the right letters in the rack to spell the word > from the dictionary? > ##i think i'm going to use funny spanish names for my methods instead > of descriptive names. > def rodolfo(secret_word, rack) > secret_word.each do |x| > rack =3D rack.to_s > if rack.include? x > rack =3D rack.sub(x, '') > ##p rack, x > else > ##puts x, ' rack don\'t work' > break > end > end > end > > > puts "reading dictionary" > dict =3D IO.readlines('/usr/share/dict/words') > > while rack > puts "enter rack" > rack =3D gets.chomp.split(//) > > dict.each do |secret_word| > > if rodolfo(secret_word.chomp.split(//), rack) > puts secret_word.to_s > end > end > end --nextPart2139708.tvCfTPD2iy Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQBDl+qRMdQeL6eBxhIRAkCdAKDT1Zzg/c7Ihy87ACnbWCsRy+p52QCfWl5M xD+ZNzI3TItJawMxk2l0k4Y= =c3rI -----END PGP SIGNATURE----- --nextPart2139708.tvCfTPD2iy--