Skip to content

Instantly share code, notes, and snippets.

@ptn
Last active December 11, 2015 18:38
Show Gist options
  • Save ptn/4642959 to your computer and use it in GitHub Desktop.
Save ptn/4642959 to your computer and use it in GitHub Desktop.
Bit Zeppelin Hack Party - Code Kata #2
# -*- coding:utf-8 -*-
def main(path)
accumulator = 0
read_ascii(path) { |char| accumulator += with_sign(to_num(char)) }
accumulator
end
def read_ascii(path)
File.open(path) do |f|
while (char = f.read(1))
yield char
end
end
end
def with_sign(num)
if plus_sign? num
num.abs
elsif minus_sign? num
num * -1
else
0
end
end
def to_num(char)
char.ord
end
def plus_sign?(num)
(divisible_by? num, 2) || (divisible_by? num, 5)
end
def minus_sign?(num)
(divisible_by? num, 3) || (divisible_by? num, 7)
end
def divisible_by?(subject, target)
subject % target == 0
end
def validate_args
if ARGV.length < 1
puts "Falta: ruta al archivo"
exit 1
end
if ARGV.length > 1
puts "Ãnico argumento válido: ruta al archivo"
exit 1
end
end
validate_args
path = ARGV[0]
puts main(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment