Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Chris Pine tutorial assistance chapter 7 sort data without use of.sort method

Reply
Thread Tools

Chris Pine tutorial assistance chapter 7 sort data without use of.sort method

 
 
jgheal@googlemail.com
Guest
Posts: n/a
 
      11-24-2008
I'm learning to program and came across 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? "

****** HOWEVER I'm not allowed to use the .sort method ********

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).

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.

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Chris Pine] Chapter 7 - Sorting Arrays without .sort Vegard Sandengen Ruby 5 02-04-2010 04:49 AM
Programming Noob Chris Pine Tutorial sorting without use ofarray.sort method whisperjim Ruby 9 08-31-2009 02:13 AM
Chris Pine tutorial assistance chapter 7 sort data without use of.sort method jgheal@googlemail.com Ruby 9 11-27-2008 07:36 PM
Another Chris Pine Tutorial Question danielj Ruby 6 09-01-2008 06:26 AM
Chris Pine Tutorial 99 Bottles of Beer Program danielj Ruby 10 08-28-2008 05:27 PM



Advertisments