-
-
Save johan--/e9b9e9b953f6913eb33e to your computer and use it in GitHub Desktop.
Codility challenge
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
class Solution | |
attr_reader :array # => nil | |
def initialize(a) | |
@array = a # => [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5] | |
end | |
def peak?(left, center, right) | |
left < center && center > right # => true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false, false, true, false, true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false, true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false | |
end | |
def peaks | |
@peaks ||= (array.each_with_index.map do |val, index| # => #<Enumerator: [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]:each_with_index>, #<Enumerator: [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]:each_with_index>, #<Enumerator: [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]:each_with_index>, #<Enumerator: [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]:each_with_index>, #<Enumerator: [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]:each_with_index>, #<Enumerator: [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]:each_with_index> | |
unless index == 0 || index == array.length - 1 # => true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, true | |
index if peak?(array[index-1], array[index], array[index+1]) # => 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil, 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil, 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil | |
end # => nil, 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, nil, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil, nil, nil, 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, nil, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil, nil, nil, 1, nil, 3, nil, 5, nil, nil, nil, nil, 10, nil, nil, nil, 2, nil, nil, nil, nil, 7, nil, nil, nil, nil | |
end).select {|el| !el.nil?} # => [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [2, 7], [1, 3, 5, 10], [2, 7] | |
end | |
def can_set_flags?(nr) | |
return false if peaks.length < nr # => false, false, false, false, false, false, false, false, false, false, true, false, false, true | |
last_index = peaks[0] # => 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2 | |
sum = 1 # => 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 | |
peaks.each_with_index do |val, index| # => [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7] | |
if index != 0 && (val - last_index) >= nr # => false, true, true, true, false, true, true, true, false, false, true, true, false, false, true, true, false, true, false, true, false, true, true, true, false, true, true, true, false, false, true, true, false, false, true, true, false, true, false, true | |
last_index = val # => 3, 5, 10, 3, 5, 10, 5, 10, 5, 10, 7, 7, 3, 5, 10, 3, 5, 10, 5, 10, 5, 10, 7, 7 | |
sum += 1 # => 2, 3, 4, 2, 3, 4, 2, 3, 2, 3, 2, 2, 2, 3, 4, 2, 3, 4, 2, 3, 2, 3, 2, 2 | |
end # => nil, 2, 3, 4, nil, 2, 3, 4, nil, nil, 2, 3, nil, nil, 2, 3, nil, 2, nil, 2, nil, 2, 3, 4, nil, 2, 3, 4, nil, nil, 2, 3, nil, nil, 2, 3, nil, 2, nil, 2 | |
end # => [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [1, 3, 5, 10], [2, 7], [2, 7] | |
sum >= nr # => true, true, true, false, true, true, true, true, true, false, true, true | |
end | |
def solve | |
(1..peaks.length).lazy.map {|nr| can_set_flags?(nr)}.take_while { |can| can }.count # => 3, 2 | |
# sum = 0 # => 0, 0 | |
# (1..peaks.length).lazy.map {|nr| can_set_flags?(nr)}.each do |x| # => #<Enumerator::Lazy: #<Enumerator::Lazy: 1..4>:map>, #<Enumerator::Lazy: #<Enumerator::Lazy: 1..2>:map> | |
# x ? sum += 1 : break # => 1, 2, 3, 1, 2 | |
# end # => nil, nil | |
# sum # => 3, 2 | |
end | |
end | |
def solution(a) | |
Solution.new(a).solve | |
end | |
require "minitest/autorun" # => true | |
class TestSolution < Minitest::Test | |
attr_reader :solution, :bsolution # => nil | |
def setup | |
a = [1,5,3,4,3,4,1,2,3,4,6,2] # => [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2] | |
b = [4,5,8,5,1,4,6,8,7,2,2,5] # => [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5] | |
@solution = Solution.new(a) # => #<Solution:0x007fd6f211fca8 @array=[1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]>, #<Solution:0x007fd6f2145ca0 @array=[1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]>, #<Solution:0x007fd6f214e238 @array=[1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]>, #<Solution:0x007fd6f216d958 @array=[1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2]> | |
@bsolution = Solution.new(b) # => #<Solution:0x007fd6f211f140 @array=[4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]>, #<Solution:0x007fd6f2145610 @array=[4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]>, #<Solution:0x007fd6f214db80 @array=[4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]>, #<Solution:0x007fd6f216d138 @array=[4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]> | |
end | |
def test_peak | |
assert_equal false, solution.peak?(2,3,4) # => true | |
assert_equal true, solution.peak?(2,3,1) # => true | |
assert_equal false, solution.peak?(52,22,14) # => true | |
end | |
def test_peaks | |
assert_equal [1,3,5,10], solution.peaks # => true | |
assert_equal [2,7], bsolution.peaks # => true | |
end | |
def test_if_can_set_flags_on_peaks | |
assert solution.can_set_flags?(1) # => true | |
assert solution.can_set_flags?(2) # => true | |
assert solution.can_set_flags?(3) # => true | |
refute solution.can_set_flags?(4) # => false | |
refute solution.can_set_flags?(5) # => false | |
assert bsolution.can_set_flags?(1) # => true | |
assert bsolution.can_set_flags?(2) # => true | |
refute bsolution.can_set_flags?(3) # => false | |
end | |
def test_solve | |
assert_equal 3, solution.solve # => true | |
assert_equal 2, bsolution.solve # => true | |
end | |
end | |
# >> Run options: --seed 41341 | |
# >> | |
# >> # Running: | |
# >> | |
# >> .... | |
# >> | |
# >> Finished in 0.003378s, 1184.1326 runs/s, 4440.4973 assertions/s. | |
# >> | |
# >> 4 runs, 15 assertions, 0 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment