Sunday, July 19, 2009

Hint #4 for my_sort

Hi,

Here's the 4th hint.

To check to see if a given element in an array is a lower value than the previous element, you'd do this:

a = [3, 2, 1]

puts a[2] < a[1] # true

With these hints, it should be possible to write a method that iterates backwards over an array and swaps each element with its previous element if the element is less than the previous element. If you do that iteration enough times, you will eventually have a sorted array. This is called a bubble sort in computer science.

Glenn

Hint #3 for my_sort

Hi,

Here's the 3rd hint.

You can iterate backwards over an array with the following code:

def reverse_each(arr)
(arr.size - 1).downto(0) { |i| print arr[i] }
end

reverse_each([3, 2, 1]) # 123

One question about the code above: why did I write (arr.size - 1)?

Look for hint #4 later today.

Glenn

Friday, July 17, 2009

Hint #2 for the my_sort method

Here's the second hint:

Use the self object in your method.

The third hint will be tomorrow.


Glenn

Thursday, July 16, 2009

Hint #1 for the my_sort method assignment

Hi All,

Here's the first hint for the my_sort assignment.

To swap 2 elements in an array -- say the 3rd element and the 2nd element -- you'd do this:

a = [1, 2, 3]

a[2], a[1] = a[1], a[2]

puts a.inspect # [1, 3, 2]

Look for the 2nd hint tomorrow or Saturday.

Glenn

Wednesday, July 15, 2009

Assignment for the July 20th class

Hi All,

The assignment for the July 20th class is to write a method to sort an array of numbers without using the array's out-of-the-box sort method.

The code should look like this:

class Array
   def my_sort

     ... your code goes here ...

   end
end

Look those some hints over the next few days.

Glenn

Saturday, July 11, 2009

Hint #4

Hi,

Here's the 4th and last hint.

So far, we have:

h = Hash.new
a = [1, 2, 2, 3, 3, 1]

a.each { |e| h[e] = 0 }
a.each { |e| h[e] += 1 }

That gives us a hash with a key for each element in the original array, and value for each time that key appeared in the array.

To complete the assignment you need to do 5 things:

1) Find out what the maximum times an element appeared in the array using the values method on the hash, which returns an array.

2) Find out the highest value with the max method on that array.

3) Remove all of the key-value pairs in the hash that do not have a value equal to the highest value.

4) Use the keys method on the hash that's left.

5) So far, none of the code that I've shown has been in a method definition. So the last step would be to take what we have and write a method definition around it.

There's a couple of things that I've shown here that could be made more efficient and elegant. Take a minute to look at the code to see if you can come up with what they are.

See you on Monday!

Glenn

Hint #3 for the mode assignment

Hi,

Here is the 3rd hint.

We've already created a hash, and used the each method to start the hash out with a key for each unique element of the hash, and we've already set the value for each key in the hash to be equal to 0.

h = Hash.new

a = [1, 2, 2, 3, 3, 1]
a.each { |e| h[e] = 0 }

puts h.inspect # {1 => 0, 2 => 0, 3 => 0}

Now, we are ready to count how many times each element in the array actually occurs.

a.each { |e| h[e] += 1 }

puts h.inspect # {1 => 2, 2 => 2, 3 => 2}

We are half way there. We have a hash that already has the information about which element or elements occur the most. Remember that the goal of the method is to return an array. How can we return the desired array from the hash? I will post the answer later today. But here's a clue: hashes have methods that allow you to get information from them. Take a look at the methods in the Hash object to see if there are any that will help us. You can see what methods are in hash objects by creating an empty hash and calling the methods method on it.

Glenn