Working through Ch.10 for learning to program 2.0 (Chris Pine)

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=[]

while true
  some_array=gets.chomp
  if some_array==""
    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/.

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 = 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 <lists@ruby-forum.com> 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=

while true
  some_array=gets.chomp
  if some_array==""
    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/\.

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

“*This email may contain confidential and/or privileged information. If you
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”.*

Confusion is certainly understandable at this point -- recursion can be a tricky subject to understand. While it is generally true that you can write algorithms in either a looping or recursive fashion, *how* to do so isn't necessarily clear. Also, some sort algorithms lend themselves quite well to one or the other, but not both.

I haven't read Learning to Program, so I'm not sure what Chris is including in this. Sort algorithms would be a natural I'd think. If he doesn't include the algorithms, WikiPedia does describe standard algorithms pretty well.

If you'd like something to look at to get you over the hump, so to speak, let me know.

If you've gotten the iterative (non-recursive) sorting method working, maybe show that.

···

On Jul 27, 2013, at 8:26 PM, JD JD <lists@ruby-forum.com> 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=

while true
some_array=gets.chomp
if some_array==""
   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/\.

Yes, I would like something to basically get me over the hump. The idea
up to this book is that, "this chapter is just a review of everything we
learned up to this point".

However, he doesn't go over algorithms. I think his idea is that people
should come up with the answer off the basic building blocks that were
taught up to this point (aka, come up with a sort algorithm on your own
instead of being taught sort algorithms). I guess that may be true,
however it just seems like its too much of a jump in my opinion.

I haven't done the non recursive one either. I was going to try to do
recursive first, but got so frustrated that I stopped. Up to this
point, the book was going ok. Now, it basically is expecting you to
make the "wheel" that is algorithms without teaching the basics of it.

Sorry, just a bit frustrated is all because I really do want to learn
this stuff.

···

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

Quoting JD JD (lists@ruby-forum.com):

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

Just out of curiosity, do you mean this page:

http://pine.fm/LearnToProgram/?Chapter=10

? Because in this page, the words 'sorting' and 'sorted' can't be
found. I was surprised that a tutorial would ask you to write a
sorting algorithm, without describing the algorith you should use.

Anyway, if you want to learn programming, it is very useful for you to
know intimately at least one sorting algorithm An algorithm that is
certainly not fast, but has the characteristic of being easy to
understand, even visually, is Bubble sort. The Wikipedia page for it
is:

but you can find large amounts of other reference.

Carlo

···

Subject: Working through Ch.10 for learning to program 2.0 (Chris Pine)
  Date: dom 28 lug 13 10:26:34 +0900

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

The version I'm reading is 2.0, the online version appears to be a
different version.

Anyways, I think I may take a quick detour from the book and try another
source and go back to it potentially.

The point is that I'm not the only one having issues with this. I
looked at reviews and it appears others are having this issue as well.
The book was good until Ch.8/9. Once there, the book moves way to fast
with few examples of what its asking you to do.

There isn't really a great example of that sort it is asking you to do.
They put it into "words", but don't really give any simplified examples
of anything like it in hard code before asking you to do it.

···

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

So, here is how to do it with cards. Basically, you look at the first
card and compare it with the second. If its smaller, you compare it to
the next one, etc. etc. until you either find a smaller card or you
don't. If you do find a smaller card, you swap the cards. If you
don't, you leave them.

Then you move on to the second card and do the same thing. You do this
until you get to the last card (which should be the biggest at this
point).

After you do that, you have a sorted card deck and can put it in the
"new" box.

How you translate that into ruby is where I am lost :/.

···

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

I tried this and came up with a one-liner that seems to do it. It sorts the
array and prints the result.
But, if I post it, will it be useful? You will learn more by doing it
yourself.
Your choice.

Harry

This sounds like fun. I've written a one-line iterative insertion sort,
that sorts an array of any object that implements #>=

It's 71 characters, plus the name of the array variable. It's also
O(n*n)

···

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

Awesome, three completely different implementations.

Mine's:

a.inject []{|s,x|(i=s.find_index{|y|y>=x})?s[0...i]+[x]+s[i..-1]:s+[x]}

···

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

So, I am just taking a detour away from the book for a bit and trying
something else and will go back to it. I glanced at the solution for
one you all wrote out. The book didn't really go over a lot of what you
all are doing.

When I go back to the book, I'm going to attempt to do it without
recursion and then with recursion. However, I am really curious if
someone could give me a semi hint on how to do it without recursion
based off my card swap example.

My guess is it would involve a while loop that does a swap something
like this:

if a>b
X++
Y++
elsif a==b
check next letter and compare
else
swap a and b

Honestly its a bit confusing to me. I haven't thought about it much
because again I am taking a quick detour away from the book and trying
an online detour to see if it might help me. I get the idea of it, I
just have a hard time putting it into ruby code I guess.

Marcus, it sounds like you read the same book. Could you maybe give me
a hint based off what I should know from the book? It looks like many
people here are using things that weren't taught in the book. Not
saying they are wrong, but it not something I probably know from what I
read in the book. I might be wrong though. I'm still learning either
way.

···

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

So, I am just taking a detour away from the book for a bit and trying
something else and will go back to it. I glanced at the solution for
one you all wrote out. The book didn't really go over a lot of what you
all are doing.

When I go back to the book, I'm going to attempt to do it without
recursion and then with recursion. However, I am really curious if
someone could give me a semi hint on how to do it without recursion
based off my card swap example.

My guess is it would involve a while loop that does a swap something
like this:

if a>b
X++
Y++
elsif a==b
check next letter and compare
else
swap a and b

Honestly its a bit confusing to me. I haven't thought about it much
because again I am taking a quick detour away from the book and trying
an online detour to see if it might help me. I get the idea of it, I
just have a hard time putting it into ruby code I guess.

Marcus, it sounds like you read the same book. Could you maybe give me
a hint based off what I should know from the book? It looks like many
people here are using things that weren't taught in the book. Not
saying they are wrong, but it not something I probably know from what I
read in the book. I might be wrong though. I'm still learning either
way.

···

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

Thanks for the input everyone. Matthew, thanks for taking the time to
write this. This is actually something I think I needed to read. It
helps me actually think about how to possibly solve these issues.

Also, Marcus, I will try to keep my code clean. Also, I will try not to
peak at your solution until I go back to the book and try again at it.
Just curious, is that solution a selection sort solution or insertion
sort? Also, the one described in the book, is that selection or
insertion (the one he writes out in plain words)?

I guess my issue with the book is it took a massive jump after Ch. 8.
Ch. 9 I had to look in the back of the book and type out was written.
Reading the code afterwords, I can understand what is going on. It may
just be the Roman numerals, but it took a massive jump in Ch. 9 and this
one.

Is there any book that might not take such a massive jump that might be
good to read as well? I really want to learn how to do ruby. It just
seems either things are too "basic" or they take "to big of jumps".
This book was great until Ch. 9. It provided good learning material and
realistic but good problems. Do you have any ideas or this?

···

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

Sorry, I didn't realize that. I read that, but didn't know if that was
what the code was below that writing (mainly because I didn't look at
the code yet for reason obvious).

Marcus, do you have any good website/book/etc. that would be helpful
from going from complete "novice" to "pretty good"? I know everyone
says this book is good, but I think it jumps too fast after ch. 9.

Is there one that is really good at that doesn't jump so fast without
giving plenty of examples beforehand (not answers of course, but
examples are helpful)? Anything?

···

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

However, he doesn't go over algorithms.

That's not true. He essentially describes "selection sort"
right on page 75:

  "We'll take our list of words, find the 'smallest' word [...]
   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 [...ample omissions...] until your
   still-unsorted list is empty."

That's the algorithm!

You might also want to search for selection sort in the internet,
it's one of the easiest sorting algorithms you could conceive.

I haven't done the non recursive one either. I was going to try to do
recursive first, but got so frustrated that I stopped.

You really should start with the iterative version.

Regards,
Marcus

···

Am 28.07.2013 11:15, schrieb JD JD:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Could be different in the new version… the online one is v1.

···

On Jul 28, 2013, at 5:26 AM, "Carlo E. Prelz" <fluido@fluido.as> wrote:

Quoting JD JD (lists@ruby-forum.com):

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

Just out of curiosity, do you mean this page:

Blocks and Procs - Learn to Program

? Because in this page, the words 'sorting' and 'sorted' can't be
found. I was surprised that a tutorial would ask you to write a
sorting algorithm, without describing the algorith you should use.

But that's the whole point of programming, isn't it?
Taking a problem and splitting/translating it into small pieces
that a computer can understand.

Maybe you should take an unsorted deck of cards and try sorting it
using the selection sort algorithm (search for the smallest card,
put it on the "sorted" stack, move all other cards to the "unsorted"
stack, and so forth) and write it down in plain English. On paper,
step by step, so that an arbitrary, not too bright person could
follow your instructions and get a sorted deck of cards.

Once you have done that, programming the same thing with an array
of e.g. numbers should be easy.

Granted, it's a big step, but figuring it out by yourself is so much
more valuable for your skills as a programmer than just copying the
code from a book or web page. In the future, you probably won't ever
need to write your own sort method or even develop your own
sort algorithm, but you will have to solve problems all the time.

Regards,
Marcus

···

Am 29.07.2013 00:00, schrieb JD JD:

The version I'm reading is 2.0, the online version appears to be a
different version.

Anyways, I think I may take a quick detour from the book and try another
source and go back to it potentially.

The point is that I'm not the only one having issues with this. I
looked at reviews and it appears others are having this issue as well.
The book was good until Ch.8/9. Once there, the book moves way to fast
with few examples of what its asking you to do.

There isn't really a great example of that sort it is asking you to do.
They put it into "words", but don't really give any simplified examples
of anything like it in hard code before asking you to do it.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

I'd *love* to see the one-liner :slight_smile: Whisper it to me at tamouse.lists@gmail.com, pls?

···

On Jul 29, 2013, at 9:43 AM, Harry Kakueki <list.push@gmail.com> wrote:

I tried this and came up with a one-liner that seems to do it. It sorts the array and prints the result.
But, if I post it, will it be useful? You will learn more by doing it yourself.
Your choice.

Just to make sure: you do not use Array#sort?

Regards,
Marcus

···

Am 29.07.2013 16:43, schrieb Harry Kakueki:

I tried this and came up with a one-liner that seems to do it. It sorts
the array and prints the result.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

I have not heard from the OP for almost 24 hours, so I will post an idea.

a = [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9]

#one-line version
p [].tap{|b| (0...a.size).each{|c| a.each{|d| b << d if a.select{|e|
e<d}.size==c}}}

OP: This uses Array#size, Array#each, and #select. I have not read the book
you are using but I guess you have studied these methods.
I hope it is readable. If there are any problems, you can fix it.

#Expanded version
b = []
(0...a.size).each do |c|
  a.each do |d|
    if a.select{|e| e<d}.size==c
      b << d
    end
  end
end
p b

Harry