Jake Worth

Note: any code contained in this post is more than a year old. Please use at your own risk.

A few weeks back I tackled the following programming challenge:

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?

My solution is below, and here:


I like that this solution is tested. I like that it allows me to override the range minimum if I want, although I don't remember why that was a feature.

I don't like that it uses to_s and that it's basically brute force. For problems like these brute force will work most of the time, but it's not elegant.

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))

Thanks again Josh Branchaud for posting these challenges to the Hashrocket team.

Mar 6, 2016

← Previous Post Next Post →
Hi! I'm Jake Worth, a developer at Hashrocket, based in Chicago. I co-organize Vim Chicago. Read my blog, learn about my work, follow me on Twitter and Github, get in touch.