Created
February 26, 2015 14:24
-
-
Save SolomonHD/b1937d34bbd4ab9d175c to your computer and use it in GitHub Desktop.
26FEB15 New Ruby
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
require 'minitest/autorun' | |
require 'minitest/pride' | |
# Write a method which accepts an array and returns a sum of the elements in the | |
# array. Specifying a second parameter (e.g. 3) will allow you to sum all the | |
# items starting from index 3. Specifying three parameters will allow you to sum | |
# between two indices (e.g. everything between 3 and 6, inclusive). | |
# WRITE YOUR CODE HERE. Name your method `subsum`. | |
def subsum(array, x = -1, y = -1) | |
return nil if array == nil | |
if x == -1 || y == -1 | |
sum = 0 | |
array.each do |a| | |
sum += a | |
end | |
sum | |
end | |
elsif x != -1 | |
z = 0 | |
x.times do | |
sum += array[z] | |
z += 1 | |
end | |
end | |
sum | |
end | |
class OptionalParametersChallenge < MiniTest::Test | |
def test_sum | |
assert_equal 10, subsum([1, 2, 3, 4]) | |
assert_equal 0, subsum([1, -2, -3, 4]) | |
end | |
def test_two_parameters | |
assert_equal 7, subsum([1, 2, 3, 4], 2) | |
assert_equal 6, subsum([9, 8, 7, 6], 3) | |
end | |
def test_three_parameters | |
assert_equal 24, subsum([9, 8, 7, 6], 0, 2) | |
assert_equal 17, subsum([9, 8, 7, 6], 0, 1) | |
end | |
def test_nil | |
assert_equal nil, subsum(nil) | |
end | |
def test_bad_second_parameter | |
assert_equal nil, subsum([9, 8, 7, 6], 4) | |
end | |
def test_bad_third_parameter | |
assert_equal nil, subsum([9, 8, 7, 6], 2, 1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment