From: JB Eriksson Date: 2005-12-08T16:32:05+09:00 Subject: Re: is this a good way to find anagrams? ------=_Part_12356_13524078.1134027118090 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline umm... what's the dictionary for? also, I think there's an easier way to do this. with a bit of spacing and stuff, I wrote up a 10 line solution. ;) I also did the splitting string into array thing. Then there's two instance methods for Array, named "sort" and "=3D=3D" respectively. An anagram is when two words contain the exact same characters, right? so i= f the two splitted strings are the same when sorted, they should be anagrams. On 12/8/05, travis laduke wrote: > > 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 > > ------=_Part_12356_13524078.1134027118090--