From: Richard Wilson Date: 2013-07-28T11:46:51+09:00 Subject: Re: Working through Ch.10 for learning to program 2.0 (Chris Pine) --047d7bf16084565b0704e2894802 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Just curious, does that book go into algorithms? I can't help you (I'm new myself to Ruby). I did some recursion in scheme, you want to make sure the second argument of your function is optional, and has a default value. def recursive_sort(unsorted_array, sorted_array =3D Array.new) I completely forget everything to do with algorithms though. I've been spoiled by standard libraries and if that book touches on them I'd probably pick it up. On Sat, Jul 27, 2013 at 6:26 PM, JD JD wrote: > So, I have been working through this book, and have been doing ok up > until ch. 10. > > I'm in the part of the chapter where it asks us to generate our own > sorting method (once with recurssion and once without). > > They gave a shell example of code. However, I'm completely lost. This > is what I came up with so far, however I really have no idea what to do > at this point. Could someone possibly point me in the right direction? > Recurrsion also is very confusing. I get the idea of it with > factorials, but beyond that and it gets a little confusing. Here is > what I have so far, and I would appriciate any help: > -------------------------------------- > puts "Enter some words" > some_array=3D[] > > while true > some_array=3Dgets.chomp > if some_array=3D=3D"" > break > end > > sort some_array > end > #This is where I would print out after this puts the solution > puts "Here is the sorted list" > > #Wrapper method suggested in the book > def sort some_array > recursive_sort some_array, [] > end > > #Actual method used to make this happen, and also where I'm confused > def recursive_sort unsorted_array, sorted_array > #Probably doing this wrong. Basically, I want to > #either push the smallest word into sorted_array > #or go down the list and find the smallest, then add it and > start > #Over. However, I could be thinking about this completely wrong > #Could someone point me in the right direction with this? > if unsorted_array[0]>unsorted_array[1] > sorted_array.push unsorted_array[1] > else > recursive_sort > end > > -- > Posted via http://www.ruby-forum.com/. > > --=20 *Richard Wilson* Sechelt Innovations Cell - (604) 842 5318 Skype - r.crawfordwilson Email - r.crawfordwilson@gmail.com =93*This email may contain confidential and/or privileged information. If y= ou are not the intended recipient or have received this email in error, please notify the sender immediately and destroy this email. Any unauthorized copying, disclosure or distribution of the information contained on this email is prohibited=94.* --047d7bf16084565b0704e2894802 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable
Just curious, does that book go into algorithms? I can'= ;t help you (I'm new myself to Ruby). I did some recursion in scheme, y= ou want to make sure the second argument of your function is optional, and = has a default value.=A0

def recursive_sort(unsorted_array, sorted_array =3D Array.new)

=
I completely forget everything to do with algorithms though. I&#= 39;ve been spoiled by standard libraries and if that book touches on them I= 'd probably pick it up.


On Sat,= Jul 27, 2013 at 6:26 PM, JD JD <lists@ruby-forum.com> wr= ote:
So, I have been working through this book, a= nd have been doing ok up
until ch. 10.

I'm in the part of the chapter where it asks us to generate our own
sorting method (once with recurssion and once without).

They gave a shell example of code. =A0However, I'm completely lost. =A0= This
is what I came up with so far, however I really have no idea what to do
at this point. =A0Could someone possibly point me in the right direction? Recurrsion also is very confusing. =A0I get the idea of it with
factorials, but beyond that and it gets a little confusing. =A0Here is
what I have so far, and I would appriciate any help:
--------------------------------------
puts "Enter some words"
some_array=3D[]

while true
=A0 some_array=3Dgets.chomp
=A0 if some_array=3D=3D""
=A0 =A0 break
=A0 end

=A0 sort some_array
end
#This is where I would print out after this puts the solution
puts "Here is the sorted list"

#Wrapper method suggested in the book
def sort some_array
=A0 recursive_sort some_array, []
end

#Actual method used to make this happen, and also where I'm confused def recursive_sort unsorted_array, sorted_array
=A0 =A0 =A0 =A0 #Probably doing this wrong. =A0Basically, I want to
=A0 =A0 =A0 =A0 #either push the smallest word into sorted_array
=A0 =A0 =A0 =A0 #or go down the list and find the smallest, then add it and=
=A0 =A0 =A0 =A0 start
=A0 =A0 =A0 =A0 #Over. =A0However, I could be thinking about this completel= y wrong
=A0 =A0 =A0 =A0 #Could someone point me in the right direction with this? =A0 if unsorted_array[0]>unsorted_array[1]
=A0 =A0 sorted_array.push unsorted_array[1]
=A0 else
=A0 =A0 recursive_sort
end

--
Posted via http://= www.ruby-forum.com/.




--
Richard= Wilson
Sechelt Innovations
Cell - (604) 842 5318
Skype - r.crawfordwilson
Email - r.crawfordwilson@gmail.com

=93This email may contain confidential and/or privileged information. If = you are not the intended recipient or have received this email in error, pl= ease notify the sender immediately and destroy this email. Any unauthorized= copying, disclosure or distribution of the information contained on this e= mail is prohibited=94.
--047d7bf16084565b0704e2894802--