From: Hugh Sasse Date: 2008-11-25T23:37:26+09:00 Subject: Re: Chris Pine tutorial assistance chapter 7 sort data without use of .sort method On Tue, 25 Nov 2008, jgheal@googlemail.com wrote: > I'm learning to progam and came accross Chris Pine's Ruby Tutorial. > > I've got to the Array and Iterators chapter ( http://pine.fm/LearnToProgram/?Chapter=07 > ). > > and the following assignment " Let's write a program which asks us to > type in as many words as we want (one word per line, continuing until > we just press Enter on an empty line), and which then repeats the > words back to us in alphabetical order. OK? " That's all fairly clear.... > > ****** HOWEVER I'm not allowed to use the .sort method ******** That's also clear, you are writing a sort program to teach you about programming, rather than to teach you to explore the library. > > The covered chapters (1 ~ 7) have not covered method creation, > Classes, Blocks or Procs, so the challenge is to solve the assignment > without using them ( which is dandy since I don't know what they are > and I want to solve this assignment before I move forward to discover > what they are). OK > > This is what I've come up with so far, however when run.... It's not > working - surprise surprise. > > Could someone initially give me an idea of what is going wrong rather > than rectify my code. This is rather difficult. Why? You have told us what the outcome of the program should be. You have shown us a program that doesn't work. There could be several things wrong, including the mental model you have built. It would be a big help to us in helping you if you told us how you intended the program to work. It may be that you have misunderstood something, so expressed your idea poorly in code. Which is OK of course, that's the point of learning. It may be that the algorithm you have chosen to implement doesn't work, so even if the program implements it correctly, you won't get the answer you want.. So the next thing to learn about programming is that comments are to express intention, to give people a bird's eye view of the code so they can see what the programmer was trying to do, when that is not immediately obvious from the code, or when the code could be wrong, expressing another idea. Add comments to this to explain your intentions about each major section and then we can tell you if your code expresses that intention > > puts 'Type in as many words as you want and once you\'ve had enough, > just \'enter\' a clear line.' > > words_array = [] > > loop do > input = gets.downcase.chomp > break if input.empty? > words_array << input > > end > > wordCheck = words_array.last > rotateCounter = 0 > sortedWords_array = [] > until words_array.length == 0 > while rotateCounter <= words_array.length > if words_array[0] >= wordCheck > > words_array.push words_array.shift > rotateCounter = rotateCounter + 1 > > else wordCheck = words_array[0] > rotateCounter = 0 > end > > end > sortedWords_array.push wordCheck > > until wordCheck = words_array.last > words_array.push words_array.shift > end > words_array.pop > rotateCounter = 0 > wordCheck = words_array.last > end > puts sortedWords_array > > # Any help greatly appreciated > Hugh