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
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.
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.
– Richard Wilson
Sechelt Innovations
Cell - (604) 842 5318
Skype - r.crawfordwilson
Email - [email protected]
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.
? 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.
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.
? 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.
Could be different in the new version the online one is v1.
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.
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.
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 :/.
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.
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.
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.
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.
I’d love to see the one-liner Whisper it to me at [email protected], pls?
– Richard Wilson
Sechelt Innovations
Cell - (604) 842 5318
Skype - r.crawfordwilson
Email - [email protected]
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.
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