Hi,

I am new to programming and following Chris Pines book. There is an
exercise in the book to write a method similar to .sort that will
arrange an array of string in alphabetical order. I will paste the
exercise:

OK. So we want to sort an array of words, and we know how to find out
which of two words comes first in the dictionary (using <).
What strikes me as probably the easiest way to do this is to keep two
more lists around: one will be our list of already-sorted words, and
the
other will be our list of still-unsorted words. We’ll take our list of
words,
find the “smallest” word (that is, the word that would come first in
the
dictionary), and stick it at the end of the already-sorted list. All
of the
other words go into the still-unsorted list. Then you do the same
thing
again but using the still-unsorted list instead of your original list:
find
the smallest word, move it to the sorted list, and move the rest to
the
unsorted list. Keep going until your still-unsorted list is empty.

I think I covered the first bit of the exercise but when I try to
repeat my method recursively it fails.
Would you be please so kind to take a look at my code and tell me what
am I doing wrong?

puts 'Please enter a list of words you would like to arrange'
puts 'in alphabetical order:'

list_of_words = []
input = 'empty'

while input != ''
        input = gets.chomp
        if input != ''
                list_of_words.push input
        end
end

def arrange some_array
recursive_arrange some_array, []
end

def recursive_arrange unsorted_array, sorted_array

# Initializing the array "still_unsorted_array"

        still_unsorted_array = []

# Finding the smallest word on the array "list_of_words"

        smallest = unsorted_array [0]

        unsorted_array.each do |word|
                if smallest > word
                        smallest = word
                end
        end

# Adding the smallest word from the array of "unsorted words" to the
array of "sorted_words"

        sorted_array.push smallest

# Adding the words from the array of "unsorted_array" to the array of
"still_unsorted_array"
# with the exception of the content of variable "smallest"

        bigger = 'empty'

        unsorted_array.each do |word|
                if word != smallest
                        bigger = word
                        still_unsorted_array.push bigger
                end
        end

# Clearing the array of "unsorted_array" so it can be re-used

        unsorted_array = []
# Adding the smallest word from the array of "still_unsorted words" to
the array of "sorted_words"

        smallest = still_unsorted_array [0]
        still_unsorted_array.each do |word|
                if smallest > word
                        smallest = word
                end
        end

# Adding the smallest word from the array of "still_unsorted words" to
the array of "sorted_words"

        sorted_array.push smallest

# Adding the remaining words from the array "still_unsorted_words" to
the array of
# "unsorted_words"

        still_unsorted_array.each do |word|
                if word != smallest
                        bigger = word
                        unsorted_array.push bigger
                end
        end

# This is the bit I tried to call the method recursively.

        if unsorted_array == unsorted_array = []
                puts sorted_array
                else
                        recursive_arrange unsorted_array, sorted_array
        end

puts 'unsorted array:'
        puts unsorted_array
        puts 'still unsorted array:'
        puts still_unsorted_array
        puts 'sorted array:'
        puts sorted_array

end

arrange list_of_words

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to