Loren
2012-Jan-19 17:39 UTC
Could Someone please explain how to repeate my code recursivly?
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Scott Ribe
2012-Jan-19 18:22 UTC
Re: Could Someone please explain how to repeate my code recursivly?
On Jan 19, 2012, at 10:39 AM, Loren wrote:> Would you be please so kind to take a look at my code and tell me what > am I doing wrong?You''re making this far, far, far more complicated than it needs to be. Hint 1: you only need two arrays, sorted & unsorted, all the shuffling in & out of temporary arrays is unnecessary and just complicates your code--this is by far most important. Hint 2: use the appropriate functions on enumerable (parent class of array) with blocks to get rid of the loops--maybe your book hasn''t gotten to this yet, if so don''t worry about it, but it''s those functions that let you express the sorting part in about 10 lines or so of code. -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.