From: Robert Dober Date: 2006-06-27T17:57:01+09:00 Subject: Re: Stumped:beginners question ------=_Part_60330_32057623.1151398606900 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 6/27/06, Dark Ambient wrote: > > I finally have had more success, at least for the first part of the > exercise. Thanks for all the help. I do have another question (will > they ever stop) - in my code below as in Matthew's example the > array.push doesn't take place till after the loop is exited. Why is > that, can't they be added incrementally ? I did the array.push in the > loop and it completely skipped over it, or seemed to at least. > > > def sort words > > u_words =3D [] > s_words =3D [] > > continue =3D true > while continue > puts words > userword =3D gets.chomp.downcase > u_words.push(userword) > puts 'we now have these words in our list ' > puts u_words > puts 'shall we continue?' > a =3D gets.chomp.downcase > if a =3D=3D 'yes' > continue =3D true > else > continue =3D false > end > puts 'we now have these words in our list ' > puts u_words > end > end > sort 'Please enter a word for sorting' # Always try to keep your code simple loop { puts words userword =3D gets.chomp.downcase break if userword =3D=3D "stop" u_words << userwords } # I understand that you have to implement your sorting algorithm yourself, that is # a very good exercise. mysort( u_words ) ^^^^^^^ yes, you are going to write this! Here is how to go for it http://en.wikipedia.org/wiki/Sorting_algorithms use bubble sort or insertion sort (the later is much better). Stay away from the sophisticated algorithms for now. Never forget what the ancient Greeks said: Learning is Suffering. Hang in there Robert > I have a ways to go to complete this little tiny project but I must > not use array.sort. > > Stuart > > On 6/26/06, Matthew Smillie wrote: > > On Jun 26, 2006, at 20:24, Dark Ambient wrote: > > > def sortwords > > words =3D [] > > continue =3D false > > while continue > > word =3D gets.chomp.downcase > > if word =3D=3D 'stop' > > continue =3D false > > elsif word =3D=3D 'hello' > > # or some other special case. > > puts "hello yourself" > > else > > # default behaviour > > words.push(word) > > end > > end > > return words.sort > > end > > > > sortwords # (input 'foo', 'bar', 'baz', 'stop') > > =3D> ["bar", "baz", "foo"] > > > > > > matthew smillie. > > > > > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_60330_32057623.1151398606900--