Skip to content

Instantly share code, notes, and snippets.

@jwworth
Created January 11, 2016 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwworth/3a2fb8f23734c1dc871a to your computer and use it in GitHub Desktop.
Save jwworth/3a2fb8f23734c1dc871a to your computer and use it in GitHub Desktop.
Vim Buffer
# 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