Vim Buffer
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
# Week 2 - Vim Buffer: I open up a new Vim buffer and type all the | |
# numbers 1 to 10,000, separated by spaces. Then, my cat walks on the keyboard | |
# and somehow activates a substitution command that replaces all the '0’ digits | |
# (zeros) with spaces. If I now sum up all the numbers in the buffer, as | |
# delineated by spaces, what is the total? | |
def vim_buffer(start = 1, limit) | |
(start..limit).flat_map { |num| num.to_s.gsub('0', ' ').split }.map(&:to_i).reduce(:+) | |
end | |
# vim_buffer(10000) | |
# => 37359001 | |
require 'minitest/autorun' | |
class TestVimBuffer < Minitest::Test | |
def test_ten | |
assert_equal(46, vim_buffer(10)) | |
end | |
def test_eleven | |
assert_equal(57, vim_buffer(11)) | |
end | |
def test_ten_twelve | |
assert_equal(69, vim_buffer(12)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment