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
Sunday, July 19, 2009
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
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
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
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
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
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
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
Friday, July 10, 2009
Hint #2 for the mode homework assignment
Hi All,
This is a bit of a spoiler is you are trying to do the homework on your own. But if you need a little help, please read on:
To create a hash of an array, you can create an empty array, then add each value of the array to the hash as a key and set the value of each key to be equal to 0.
Like this:
h = Hash.new
a = [1, 2, 2, 3, 3, 1]
a.each { |e| h[e] = 0 }
Hint #3 to follow tomorrow morning.
Enjoy your weekend!
Glenn
This is a bit of a spoiler is you are trying to do the homework on your own. But if you need a little help, please read on:
To create a hash of an array, you can create an empty array, then add each value of the array to the hash as a key and set the value of each key to be equal to 0.
Like this:
h = Hash.new
a = [1, 2, 2, 3, 3, 1]
a.each { |e| h[e] = 0 }
Hint #3 to follow tomorrow morning.
Enjoy your weekend!
Glenn
Hint #1 for the mode homework assignment
Hi,
Here are a couple of hints for this week's assignment.
A hash is a good way of storing pairs of objects that are related to each other in some way. The book gives states and their abbreviations as good examples of this:
h = {'Massachusetts' => 'MA', 'New York' => 'NY'}
To get the abbreviation of Massacusetts, you'd use the following:
puts h['Massachusetts'] # MA
puts h['New York'] # NY
To add another value to the has after it's been create, do this:
h['New Jersey'] = 'NJ'
puts h['New Jersey'] # NJ
But referencing a key that isn't already in the has returns a nil object.
puts h['Utah'] # nil
You can use a hash to count the number of times each element appears in a given array. Can you think of how to do this?
Look for Hint #2 later in the day...
Glenn
Here are a couple of hints for this week's assignment.
A hash is a good way of storing pairs of objects that are related to each other in some way. The book gives states and their abbreviations as good examples of this:
h = {'Massachusetts' => 'MA', 'New York' => 'NY'}
To get the abbreviation of Massacusetts, you'd use the following:
puts h['Massachusetts'] # MA
puts h['New York'] # NY
To add another value to the has after it's been create, do this:
h['New Jersey'] = 'NJ'
puts h['New Jersey'] # NJ
But referencing a key that isn't already in the has returns a nil object.
puts h['Utah'] # nil
You can use a hash to count the number of times each element appears in a given array. Can you think of how to do this?
Look for Hint #2 later in the day...
Glenn
Tuesday, July 7, 2009
Assignment for the July 13th class
Hi All,
For next week's class, the assignment is to write a method for the Array class that returns the mode of a given array. The mode in statistics is the most commonly occurring value or values. If there is a tie, then all of the numbers in the tie are considered the mode, so the return value of the method should be an array with 1 or more methods.
To start you off, here is some code.
class Array
   def mode
     ... your code goes here ...
   end
end
[100, 2, -10, 52.7, 2, nil].mode # returns [2]
[17.1, 2, 142.3, -22, nil, 17.1, 2].mode # returns [2, 17.1] or [17.1, 2]
To solve this, you will need to use a hash. Solving it without using a hash will be very difficult.
Good luck!
Please email or post a comment if you have any questions. This is a tough assignment, so don't be discouraged if it seems hard to do!
Glenn
For next week's class, the assignment is to write a method for the Array class that returns the mode of a given array. The mode in statistics is the most commonly occurring value or values. If there is a tie, then all of the numbers in the tie are considered the mode, so the return value of the method should be an array with 1 or more methods.
To start you off, here is some code.
class Array
   def mode
     ... your code goes here ...
   end
end
[100, 2, -10, 52.7, 2, nil].mode # returns [2]
[17.1, 2, 142.3, -22, nil, 17.1, 2].mode # returns [2, 17.1] or [17.1, 2]
To solve this, you will need to use a hash. Solving it without using a hash will be very difficult.
Good luck!
Please email or post a comment if you have any questions. This is a tough assignment, so don't be discouraged if it seems hard to do!
Glenn
minimum method for the Array class
Hi,
I used the wrong method in class last night in the solution for the minimum method. I used collect the collect method on the array to get rid of the nil elements when I should have used select.
Here's the correct way to do it:
class Array
   def minimum
     self.select { |e| e != nil }.sort.first
   end
end
puts [1, 2, -10, nil, 52.7].minimum # -10
You can also get rid of the nil elements using the compact method:
class Array
   def minimum
     self.compact.sort.first
   end
end
puts [1, 2, -10, nil, 52.7].minimum # -10
I used the wrong method in class last night in the solution for the minimum method. I used collect the collect method on the array to get rid of the nil elements when I should have used select.
Here's the correct way to do it:
class Array
   def minimum
     self.select { |e| e != nil }.sort.first
   end
end
puts [1, 2, -10, nil, 52.7].minimum # -10
You can also get rid of the nil elements using the compact method:
class Array
   def minimum
     self.compact.sort.first
   end
end
puts [1, 2, -10, nil, 52.7].minimum # -10
Subscribe to:
Comments (Atom)