# One for me as a noob hard task

**URL:** <https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687>\
**Category:** Ruby\
**Created:** [November 19, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687 "2007-11-19T20:10:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/1 "2007-11-19T20:10:05Z")

</div>

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  
without using the sort method. It says I shall do it at  
[Arrays and Iterators - Learn to Program](http://pine.fm/LearnToProgram/?Chapter=07) but I’m stuck.

If I use the sort method, I do it like this:

words = []  
while (word = gets.chomp) != “”  
words \<\< word  
end  
puts  
puts words.sort

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/2 "2007-11-19T20:24:42Z")

</div>

Joakim Olsson wrote:

> and which then repeats the words back to us in alphabetical order  
> without using the sort method. It says I shall do it at  
> [Arrays and Iterators - Learn to Program](http://pine.fm/LearnToProgram/?Chapter=07) but I’m stuck.

> **[Sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms)**
>
> In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require input data to be in sorted lists. Sorting is also often useful for canonicalizing data and for producing human-readable output.
> Formally, the output of ...

Pick one (bubble sort is quite easy) and then implement it in ruby.

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/3 "2007-11-19T20:35:17Z")

</div>

On Nov 19, 2007 8:22 PM, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Joakim Olsson wrote:
> 
> > and which then repeats the words back to us in alphabetical order  
> > without using the sort method. It says I shall do it at  
> > [Arrays and Iterators - Learn to Program](http://pine.fm/LearnToProgram/?Chapter=07) but I’m stuck.
> 
> [Sorting algorithm - Wikipedia](http://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms)  
> Pick one (bubble sort is quite easy) and then implement it in ruby.

Or for the beginning, before you try to implement any of those  
algorithms, you may try this:  
Just image how YOU would sort a pile of cards with those words on  
them. What would you do?

Then try to implement it in ruby. The pile is your array. You may (or  
not) need another pile, and some basic operations:  
remove a card from the stack, and insert it in some other place, and  
maybe more. You don’t have to make it fast,  
or efficient. Just try to make it work anyhow.

If you’re stuck, try with small arrays - the smallest one is empty 😉  
It’s easy to sort an empty array, right? Then try one word.  
Still easy. Two words? Three? Four?

After you have done this, look at the “properly designed” algorithms  
and try to implement some of them.

Finally you can more-or-less forget about this all and use the sort  
method 😉 It’s always good to know what’s  
going on under the hood.

Good luck!

Jano

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/4 "2007-11-19T20:49:29Z")

</div>

> Good luck!
> 
> Jano

Thanks for the cool answer! However, I must say that I can’t solve the  
problem yet. I guess I should use the pop method and the \<-thingy but I  
can’t solve it.

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/5 "2007-11-19T21:18:16Z")

</div>

Now a person wrote this to me:

sorted=[]; words.length.times do sorted\<\<words.delete(words.min) end;  
sorted

How can I use that in the code? It would be really nice with an example  
which I can read and then understand.

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/6 "2007-11-19T22:03:38Z")

</div>

words = []  
sorted=[]  
while (word = gets.chomp) != “”  
words \<\< word  
end  
words.length.times do  
sorted \<\< words.delete(words.min)  
end  
puts  
puts sorted

That worked it out! Thank you, Sebastian and everybody else!

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [November 19, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/one-for-me-as-a-noob-hard-task/120687/7 "2007-11-19T21:29:51Z")

</div>

Joakim Olsson wrote:

> Now a person wrote this to me:

That person was me.

> sorted=[]; words.length.times do sorted\<\<words.delete(words.min) end;  
> sorted
> 
> How can I use that in the code?

It returns the sorted array. You can puts it or do whatever you want  
with it.

> It would be really nice with an example  
> which I can read and then understand.

words=gets("").split("\n")  
sorted=[];

# At this point sorted is an emtpy array and words is an unsorted array

# of the words that the user entered.

words.length.times do # Do the following as many times  
# as there are items in words.  
sorted \<\< words.delete(words.min)

# Take the smallest item in words (words.min),

# delete it from words (words.delete) and then

# put it at the end of sorted (sorted \<\<)

end

puts sorted # Print the sorted array to the screen

HTH,  
Sebastian
