Created
February 20, 2013 10:52
-
-
Save mdyn/4994738 to your computer and use it in GitHub Desktop.
Ruby Academy
Homework 01: rubeque, elementary level
This file contains 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
# Solution to 'The Truth' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-truth | |
assert_equal true, true | |
# Solution to 'Reverse' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/reverse | |
assert_equal 'nocab yknuhc'.reverse, 'chunky bacon' | |
# Solution to 'Hello World' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/hello-world | |
assert_equal 'HELLO WORLD', 'hello world'.upcase | |
# Solution to 'Maximum' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/maximum | |
def maximum(arr) | |
arr.sort.last | |
end | |
assert_equal maximum([2, 42, 22, 02]), 42 | |
assert_equal maximum([-2, 0, 33, 304, 2, -2]), 304 | |
assert_equal maximum([1]), 1 | |
# Solution to 'Nil Values' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/nil-values | |
[0, '', 'chunky_bacon'].each { |v| assert_equal v.nil?, false } | |
# Solution to 'Map' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/map | |
assert_equal [1, 4, 9, 16], (1..4).map { |element| element**2 } | |
# Solution to 'FizzBuzz' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/fizzbuzz | |
def fizzbuzz(x) | |
return "FizzBuzz" if x % 15 == 0 | |
return "Fizz" if x % 3 == 0 | |
return "Buzz" if x % 5 == 0 | |
x | |
end | |
assert_equal fizzbuzz(3), "Fizz" | |
assert_equal fizzbuzz(50), "Buzz" | |
assert_equal fizzbuzz(15), "FizzBuzz" | |
assert_equal fizzbuzz(5175), "FizzBuzz" | |
# Solution to 'The Curious Case of the Missing Method' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-curious-case-of-the-missing-method | |
assert_equal [1, 4, nil, 9, 16, nil].compact.inject(0) {|sum, number| sum + number}, 30 | |
# Solution to 'Blackjack' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/blackjack | |
def twenty_one? *cards | |
cards.reduce(:+) <= 21 | |
end | |
assert_equal twenty_one?(3, 4, 5, 6, 3), true | |
assert_equal twenty_one?(3, 11, 10), false | |
assert_equal twenty_one?(10, 11), true | |
# Solution to 'The Curious Case of the Missing Method Part 2' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-curious-case-of-the-missing-method-part-2 | |
assert_equal [1, 3, 7, 4, 9, 8].find(&:even?), 4 | |
# Solution to 'Temperature Robot' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/temperature-robot | |
def temperature_bot(temp) | |
# temperature bot is American but takes Celsius temperatures | |
case temp | |
when (18..21) | |
"I like this temperature" | |
else | |
"This is uncomfortable for me" | |
end | |
end | |
assert_equal temperature_bot(18), "I like this temperature" | |
assert_equal temperature_bot(21), "I like this temperature" | |
assert_equal temperature_bot(22), "This is uncomfortable for me" | |
assert_equal temperature_bot(-3), "This is uncomfortable for me" | |
# Solution to 'Injected and Rejected' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/injected-and-rejected | |
def sum_over_50(arr) | |
arr.reject{|element| element <= 50}.reduce(0, :+) | |
end | |
assert_equal sum_over_50([29, 52, 77, 102]), 231 | |
assert_equal sum_over_50([5, 11, 50]), 0 | |
assert_equal sum_over_50([4, 8, 15, 16, 23, 42]), 0 | |
assert_equal sum_over_50([300, 22, 1, 55, 42]), 355 | |
# Solution to 'Home on the Range' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/home-on-the-range | |
assert_equal (1..100).to_a[11..94].reduce(:+), 4494 | |
# Solution to 'There's No Way This Works' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/there-quo-s-no-way-this-works | |
@name = "Dave" | |
str = "My mind is going #@name" | |
assert_equal (str == "My mind is going Dave"), true | |
# Solution to 'Array Item Removal' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/array-item-removal | |
assert_equal ([:r, :u, :b, :e, :q, :u, :e] [2..4].rotate.reverse[0..1]), [:b, :q] | |
# Solution to '&& Versus And' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/-and--and--versus-and | |
roses = "blue" && "red" | |
violets = "blue" and "red" | |
assert_equal roses, "red" | |
assert_equal violets, 'blue' | |
# Solution to 'Subtracting Out The Sugar' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/subtracting-out-the-sugar | |
assert_equal 2.+(2), 2 + 2 | |
assert_equal 40.+(2), 42 | |
# Solution to 'There's No Way This Works Version 2' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/there-quo-s-no-way-this-works-version-2 | |
str = "Hello" "World" | |
assert_equal str, "HelloWorld" | |
# Solution to 'Brackets and Searches' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/brackets-and-searches | |
assert_equal "hello world"['e'], "e" | |
assert_equal "what"['e'], nil | |
assert_equal "rubeque"['e'], "e" | |
assert_equal "E"['e'], nil | |
# Solution to 'Or Equal' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/or-equal | |
b = 8 | |
c = false | |
a ||= "rubeque" | |
b ||= "rubeque" | |
c ||= "rubeque" | |
assert_equal a, "rubeque" | |
assert_equal b, 8 | |
assert_equal c, "rubeque" | |
# Solution to 'Alternate Array Notation' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/alternate-array-notation | |
assert_equal %w(hello world), ["hello", "world"] | |
assert_equal %w{1 2 3 4}, ["1", "2", "3", "4"] | |
assert_equal %w?remembrance of things past?, ["remembrance", "of", "things", "past"] | |
# Solution to 'Set Intersection' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/set-intersection | |
assert_equal ([ 1, 1, 3, 5 ] & [ 1, 2, 3 ]), [ 1, 3 ] | |
# Solution to 'Getters and Setters' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/getters-and-setters | |
class Character | |
attr_accessor :name, :quote | |
end | |
thorin = Character.new | |
thorin.name = "Thorin Oakenshield" | |
thorin.quote = "Some courage and some wisdom, blended in measure. If more of us valued food | |
and cheer and song above hoarded gold, it would be a merrier world" | |
stephen = Character.new | |
stephen.name = "Stephen Dedalus" | |
assert_equal thorin.name, "Thorin Oakenshield" | |
assert_equal stephen.name, "Stephen Dedalus" | |
# Solution to 'Queue Continuum' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/queue-continuum | |
class Queue | |
def initialize(arr) | |
@arr = arr | |
end | |
def pop(*idx) | |
@arr.shift *idx | |
end | |
def push(elem) | |
@arr.push(*elem) == @arr | |
end | |
def to_a | |
@arr | |
end | |
end | |
queue = Queue.new([5, 6, 7, 8]) | |
assert_equal queue.pop, 5 | |
assert_equal queue.pop, 6 | |
assert_equal queue.push([4, 2]), true | |
assert_equal queue.pop(2), [7, 8] | |
assert_equal queue.to_a, [4, 2] | |
# Solution to 'Ternary Operator' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/ternary-operator | |
a = "Miles O'Brien" | |
b = "Barack Obama" | |
assert_equal ((a =~ /[ ]\w'/) ? "Irish" : "Not Irish"), "Irish" | |
assert_equal ((b =~ /[ ]\w'/) ? "Irish" : "Not Irish"), "Not Irish" | |
# Solution to 'The Limits of Probability' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-limits-of-probability | |
random_values = (0..1000000).inject(0.0) do |sum, _| | |
sum += rand(14) + rand(14) | |
end | |
assert_equal (random_values/1000000.0).round, 13 | |
# Solution to 'Caution Case' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/caution-case | |
def caution_case(obj) | |
case obj | |
when obj | |
true | |
else | |
false | |
end | |
end | |
assert_equal caution_case( 1 ), true | |
assert_equal caution_case( [1, 2] ), true | |
assert_equal caution_case( {1=>2} ), true | |
assert_equal caution_case( (1..2) ), false | |
# Solution to 'The Curious Case of the Missing Method Part 3' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-curious-case-of-the-missing-method-part-3 | |
a1 = [1, 2, 3] | |
a2 = [2, 3, 4] | |
b1 = ["durham", "bartow", "zwolle"] | |
b2 = ["nc", "fl", "nl"] | |
assert_equal a1.zip(a2), [[1, 2], [2, 3], [3, 4]] | |
assert_equal [10, 11, 12].zip(a1, a2), [[10, 1, 2], [11, 2, 3], [12, 3, 4]] | |
assert_equal b1.zip(b2), [["durham", "nc"], ["bartow", "fl"], ["zwolle", "nl"]] | |
# Solution to 'Baby Got Stacks' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/baby-got-stacks | |
class Stack | |
def initialize(stack) | |
@stack = stack | |
end | |
def pop(*idx) | |
result = @stack.pop(*idx) | |
return result.reverse if result.is_a? Array | |
result | |
end | |
def push(items) | |
@stack.push(*items) == @stack | |
end | |
def to_a | |
@stack | |
end | |
def to_a | |
@stack | |
end | |
end | |
stack = Stack.new([5, 6, 7, 8]) | |
assert_equal stack.pop, 8 | |
assert_equal stack.pop, 7 | |
assert_equal stack.push([4, 2]), true | |
assert_equal stack.pop(3), [2, 4, 6] | |
assert_equal stack.to_a, [5] | |
# Solution to 'No Limit' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/no-limit | |
assert_equal ["1", "2", "3"], "1,2,3".split(',', -1) | |
assert_equal ["", "", "1", "2", "3"], ",,1,2,3".split(',', -1) | |
assert_equal ["1", "2", "3", "", ""], "1,2,3,,".split(',', -1) | |
# Solution to 'Defined? Or not?' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/defined-ques--or-not-ques- | |
if false | |
w = :whatever | |
end | |
assert_equal defined?(w) != nil, true | |
# Solution to 'The Curious Case of the Missing Method Part 4' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-curious-case-of-the-missing-method-part-4 | |
class A | |
end | |
class B < A | |
end | |
assert_equal B.ancestors[1], A | |
# Solution to 'Default Encoding' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/default-encoding | |
assert_equal "".encoding, Encoding::US_ASCII | |
assert_equal "ascii compatible string".encoding, Encoding::US_ASCII | |
# Solution to 'The Curious Case of the Missing Method Part 5' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/the-curious-case-of-the-missing-method-part-5 | |
trilogy = [["Sympathy for Mr Vengeance", "Ryu", "Cha Yeong-mi"], ["Oldboy", "Oh Dae-su", "Kang Hye-jeong"], | |
["Sympathy for Lady Vengeance", "Lee Geum-ja"]] | |
assert_equal trilogy.assoc("Sympathy for Lady Vengeance"), ["Sympathy for Lady Vengeance", "Lee Geum-ja"] | |
assert_equal trilogy.rassoc("Ryu"), ["Sympathy for Mr Vengeance", "Ryu", "Cha Yeong-mi"] | |
assert_equal trilogy.rassoc("Lee Geum-ja"), ["Sympathy for Lady Vengeance", "Lee Geum-ja"] | |
# Solution to 'Phanaeng Curry' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/phanaeng-curry | |
exponential = -> x, y { y ** x } | |
squared = exponential.curry[2] | |
assert_equal squared.(3) == 9, true | |
# Solution to 'Shotgun Assignment' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/shotgun-assignment | |
a, b, c = ["eat", "chunky", "bacon"] | |
assert_equal a, "eat" | |
assert_equal b, "chunky" | |
assert_equal c, "bacon" | |
# Solution to 'Bigger element' on rubeque.com | |
# by mdyn | |
# http://rubeque.com/problems/bigger-element | |
def first_even(items) | |
items.detect(&:even?) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment