Skip to content

Instantly share code, notes, and snippets.

Created January 11, 2016 18:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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(:+)
# vim_buffer(10000)
# => 37359001
require 'minitest/autorun'
class TestVimBuffer < Minitest::Test
def test_ten
assert_equal(46, vim_buffer(10))
def test_eleven
assert_equal(57, vim_buffer(11))
def test_ten_twelve
assert_equal(69, vim_buffer(12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment