# Another Chris Pine Tutorial Question

**URL:** <https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317>\
**Category:** Ruby\
**Created:** [August 30, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317 "2008-08-30T23:00:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [August 30, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/1 "2008-08-30T23:00:23Z")

</div>

Let’s write a program which asks us to type in as many words as we  
want (one word per line, continuing until we just press Enter on an  
empty line), and which then repeats the words back to us in  
alphabetical order. OK?

So… first we’ll—uh… um… hmmm… Well, we could—er… um…

You know, I don’t think we can do it. We need a way to store an  
unknown amount of words, and how to keep track of them all together,  
so they don’t get mixed up with other variables. We need to put them  
in some sort of a list. We need arrays.  
puts “Let’s make a shopping list!”

shoplist = []

while shoplist.last != ‘’

shoplist.push(gets.downcase.chomp)

end

shoplist.pop

puts shoplist.sort

The program asks for one word per line but there is no way one can do  
that utilizing the methods the tutorial has expounded upon up to this  
point so I settled for what I got here.

However, it asks you to then do this:

• Try writing the above program without using the sort method. A large  
part of programming is solving problems, so get all the practice you  
can!

Could somebody point me in the right direction, or give me an example  
of how that would be possible? I’m racking my brain but I can not  
think of a way to do it without using methods and tools that you are  
not yet supposed to use at this point in the tutorial (i.e. regex)

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [August 30, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/2 "2008-08-30T23:46:02Z")

</div>

Have you already been introduced to String#\<=\> [  
[class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M000778) ] ? You could use  
it as  
the basis for comparing each of the input strings to all others that  
you’ve  
collected so far. How’s that for a push in one direction?

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [August 31, 2008, 12:54am UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/3 "2008-08-31T00:54:30Z")

</div>

danielj wrote:

> in some sort of a list. We need arrays.  
> shoplist.pop  
> • Try writing the above program without using the sort method. A large  
> part of programming is solving problems, so get all the practice you  
> can!
> 
> Could somebody point me in the right direction, or give me an example  
> of how that would be possible? I’m racking my brain but I can not  
> think of a way to do it without using methods and tools that you are  
> not yet supposed to use at this point in the tutorial (i.e. regex)

There’s not really any point to limiting yourself. Whatever you come up  
with will essentially be a crude or simplified sorting algorithm. If  
you really didn’t want to use the sort method, you could do something  
like this:

words = []  
while true  
word = gets.chomp  
break if word == ‘’  
words \<\< word  
end

while words.size \> 0  
word = words.min{|a,b| a.downcase \<=\> b.downcase }  
puts word  
words -= [word]  
end

However, what you’ve ended up doing is creating something a lot like a  
selection sort only instead of swapping with the front of the list  
you’re printing it and removing it from the list. In effect, it’s just  
a really inefficient version of sort.

Maybe you’ve taken this as a personal challenge, but I’ve come to except  
some (most?) texts have at least some little quirks like this. They’re  
best ignored while you move on to more interesting things. If you want  
a challenge, there’s a huge backlog of RubyQuiz problems that are much  
more interesting.

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [August 31, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/4 "2008-08-31T14:05:34Z")

</div>

On 30.08.2008 22:55, danielj wrote:

> However, it asks you to then do this:
> 
> • Try writing the above program without using the sort method. A large  
> part of programming is solving problems, so get all the practice you  
> can!
> 
> Could somebody point me in the right direction, or give me an example  
> of how that would be possible? I’m racking my brain but I can not  
> think of a way to do it without using methods and tools that you are  
> not yet supposed to use at this point in the tutorial (i.e. regex)

The trick I assume Chris is targeting at is to insert words at the  
correct position. That way your list is always sorted and you do not  
need an explicit sort operation.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [August 31, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/5 "2008-08-31T21:26:27Z")

</div>

On 2008-08-31 13:00:33 +0100, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> The trick I assume Chris is targeting at is to insert words at the  
> correct position. That way your list is always sorted and you do not  
> need an explicit sort operation.

That’s a good idea. Or I suppose you could implement your own sorting  
method and call that instead.

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [September 1, 2008, 9:34am UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/6 "2008-09-01T09:34:43Z")

</div>

On Aug 31, 11:21 pm, timr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > shoplist.push(gets.downcase.chomp)
> 
> > However, it asks you to then do this:
> 
> > • Try writing the above program without using the sort method. A large  
> > part of programming is solving problems, so get all the practice you  
> > can!
> 
> > Could somebody point me in the right direction, or give me an example  
> > of how that would be possible? I’m racking my brain but I can not  
> > think of a way to do it without using methods and tools that you are  
> > not yet supposed to use at this point in the tutorial (i.e. regex)

SORRY. AFTER FIXING THE COMMENTS TO BE MORE READABLE–

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [September 1, 2008, 9:05am UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/7 "2008-09-01T09:05:58Z")

</div>

On Aug 30, 1:55 pm, danielj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in some sort of a list. We need arrays.  
> shoplist.pop  
> part of programming is solving problems, so get all the practice you  
> can!
> 
> Could somebody point me in the right direction, or give me an example  
> of how that would be possible? I’m racking my brain but I can not  
> think of a way to do it without using methods and tools that you are  
> not yet supposed to use at this point in the tutorial (i.e. regex)

words = []  
loop do #loop allows one to do an indefinite amount  
of something–must set an exit  
#inside of the loop.  
incoming = gets  
if incoming == “\n” then #if a blank line is entered, we are done.  
Sort and puts the list.  
puts words.sort  
exit  
else  
words \<\< incoming #if data is entered, add it to the array of  
words.  
end  
end

#=\>  
alphabet soup  
bananas  
coca cola  
hot dogs  
kool aid  
lettuce  
milk  
potatoes  
spinach  
sugar

Tim Rand

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [September 1, 2008, 9:35am UTC](https://www.ruby-forum.com/t/another-chris-pine-tutorial-question/147317/8 "2008-09-01T09:35:18Z")

</div>

words = []  
loop do #loop allows one to do an indefinite amount  
# of something–must set an exit  
#inside of the loop.  
incoming = gets  
if incoming == “\n” then #if a blank line is entered, we are done.  
#Sort and puts the list.  
puts words.sort  
exit  
else  
words \<\< incoming #if data is entered, add it to the array of  
words.  
end  
end

#=\>  
alphabet soup  
bananas  
coca cola  
hot dogs  
kool aid  
lettuce  
milk  
potatoes  
spinach  
sugar

Tim Rand
