Created
January 14, 2016 14:27
-
-
Save mindplace/cd274e7bb12410d49ac8 to your computer and use it in GitHub Desktop.
Block problems to work on block passing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ### Bubble Sort | |
# | |
# http://en.wikipedia.org/wiki/bubble_sort | |
# | |
# Implement Bubble sort in a method, `Array#bubble_sort!`. Your method should | |
# modify the array so that it is in sorted order. | |
# | |
# > Bubble sort, sometimes incorrectly referred to as sinking sort, is a | |
# > simple sorting algorithm that works by repeatedly stepping through | |
# > the list to be sorted, comparing each pair of adjacent items and | |
# > swapping them if they are in the wrong order. The pass through the | |
# > list is repeated until no swaps are needed, which indicates that the | |
# > list is sorted. The algorithm gets its name from the way smaller | |
# > elements "bubble" to the top of the list. Because it only uses | |
# > comparisons to operate on elements, it is a comparison | |
# > sort. Although the algorithm is simple, most other algorithms are | |
# > more efficient for sorting large lists. | |
# | |
# Hint: Ruby has parallel assignment for easily swapping values: | |
# http://rubyquicktips.com/post/384502538/easily-swap-two-variables-values | |
# | |
# After writing `bubble_sort!`, write a `bubble_sort` that does the same | |
# but doesn't modify the original. Do this in two lines using `dup`. | |
# | |
# Finally, modify your `Array#bubble_sort!` method so that, instead of | |
# using `>` and `<` to compare elements, it takes a block to perform the | |
# comparison: | |
# | |
# ```ruby | |
# [1, 3, 5].bubble_sort! { |num1, num2| num1 <=> num2 } #sort ascending | |
# [1, 3, 5].bubble_sort! { |num1, num2| num2 <=> num1 } #sort descending | |
# ``` | |
# | |
# #### `#<=>` (the **spaceship** method) compares objects. `x.<=>(y)` returns | |
# `-1` if `x` is less than `y`. If `x` and `y` are equal, it returns `0`. If | |
# greater, `1`. For future reference, you can define `<=>` on your own classes. | |
# | |
# http://stackoverflow.com/questions/827649/what-is-the-ruby-spaceship-operator | |
class Array | |
def bubble_sort! | |
end | |
def bubble_sort(&prc) | |
end | |
end | |
# ### Doubler | |
# Write a `doubler` method that takes an array of integers and returns an | |
# array with the original elements multiplied by two. | |
def doubler(array) | |
end | |
# ### My Each | |
# Extend the Array class to include a method named `my_each` that takes a | |
# block, calls the block on every element of the array, and then returns | |
# the original array. Do not use Enumerable's `each` method. I want to be | |
# able to write: | |
# | |
# ```ruby | |
# # calls my_each twice on the array, printing all the numbers twice. | |
# return_value = [1, 2, 3].my_each do |num| | |
# puts num | |
# end.my_each do |num| | |
# puts num | |
# end | |
# # => 1 | |
# 2 | |
# 3 | |
# 1 | |
# 2 | |
# 3 | |
# | |
# p return_value # => [1, 2, 3] | |
# ``` | |
class Array | |
def my_each(&prc) | |
end | |
end | |
# ### My Enumerable Methods | |
# * Implement new `Array` methods `my_map` and `my_select`. Do | |
# it by monkey-patching the `Array` class. Don't use any of the | |
# original versions when writing these. Use your `my_each` method to | |
# define the others. Remember that `each`/`map`/`select` do not modify | |
# the original array. | |
# * Implement a `my_inject` method. Your version shouldn't take an | |
# optional starting argument; just use the first element. Ruby's | |
# `inject` is fancy (you can write `[1, 2, 3].inject(:+)` to shorten | |
# up `[1, 2, 3].inject { |sum, num| sum + num }`), but do the block | |
# (and not the symbol) version. Again, use your `my_each` to define | |
# `my_inject`. Again, do not modify the original array. | |
class Array | |
def my_map(&prc) | |
end | |
def my_select(&prc) | |
end | |
def my_inject(&blk) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment