Created
September 25, 2015 17:56
-
-
Save matiasa18/1915ae7f6d6e86181e61 to your computer and use it in GitHub Desktop.
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
require "minitest/autorun" | |
# Solution for | |
# 1.Given an array of n integers where _n > 1_, `nums`, return an array `output` such that `output[i]` | |
# is equal to the product of all the elements of nums except `nums[i]`. | |
# | |
# Solve it **without division** and leave a comment with the time complexity of your algorithm. | |
# | |
# For example, given `[1,2,3,4]`, return `[24,12,8,6]`. | |
def problem_1 (nums) | |
return [] unless nums.is_a?(Array) && nums.length > 1 | |
output = [] | |
nums.each_with_index do |first_val, i| | |
# We initialize at the given index | |
# Then we select the elements from that array that don't have the same index than the index we are currently sitting on | |
output[i] = 1 | |
nums.each_with_index.select { |n, x| x != i}.each do |second_val, x| | |
output[i] *= second_val | |
end | |
end | |
output | |
end | |
# Solution for | |
# 2. Given a specific number (let's call it `n`) and an array of numbers | |
# (let's call it `numbers`) write some code that returns a Boolean, indicating | |
# if there is a pair of numbers in `numbers` that together add up to `n`. | |
# | |
# e.g.: n = 12, numbers = [1,2,3,4,5,6,7,8,9] -> true (because 5 + 7 == 12) | |
# e.g.2.: n = 20, numbers = [1,2,3,4,5,6] -> false | |
def problem_2 (n, numbers) | |
return false unless numbers.is_a?(Array) && numbers.length >= 2 | |
numbers.each_with_index do |first_num, i| | |
# We will select all the numbers from numbers but the one we are standing on right now (i) | |
numbers.each_with_index.select { |n, x| x != i}.each do |second_num, x| | |
return true if first_num + second_num == n | |
end | |
end | |
false | |
end | |
# Solution for | |
# 3. We say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; | |
# for example, the 5-digit number, 15234, is 1 through 5 pandigital. | |
# | |
# The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, | |
# and product is 1 through 9 pandigital. Find the sum of all products whose multiplicand/multiplier/product | |
# identity can be written as a 1 through 9 pandigital. | |
def problem_3 () | |
end | |
class TestFirstSolution < Minitest::Test | |
def test_2_number_array_works | |
assert_equal problem_1([1, 2]), [2, 1] | |
end | |
def test_4_number_array_works | |
assert_equal problem_1([1, 2, 3, 4]), [24, 12, 8, 6] | |
end | |
def test_empty_array_returns_empty | |
assert_equal problem_1([]), [] | |
end | |
def test_1_number_array_returns_empty | |
assert_equal problem_1([1]), [] | |
end | |
end | |
class TestSecondSolution < Minitest::Test | |
def test_12_numbers | |
assert_equal problem_2(12, [1, 2, 3, 4, 5, 6, 7, 8, 9]), true | |
end | |
def test_should_return_true | |
assert_equal problem_2(2, [1, 1]), true | |
end | |
def test_should_return_false | |
assert_equal problem_2(3, [1, 1]), false | |
end | |
def test_0_and_0_equals_0 | |
assert_equal problem_2(0, [0, 0]), true | |
end | |
def test_negative_numbers | |
assert_equal problem_2(-2, [-1, -1]), true | |
end | |
def test_numbers_length_less_than_2_return_false | |
assert_equal problem_2(24, [1]), false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment