CHALLENGE DESCRIPTION
In this challenge you will be dealing with zero based numbers. A zero based number is set in
the following form: "flag" "sequence of zeroes" "flag" "sequence of zeroes" etc. Separated by
a single space.
00 0 0 00 00 0
Your goal is to convert these numbers to integers. In order to do that you need to perform
the following steps:
1. Convert a zero based number into a binary form using the following rules: a) flag "0" means
that the following sequence of zeros should be appended to a binary string. b) flag "00" means
that the following sequence of zeroes should be transformed into a sequence of ones and
appended to a binary string.
00 0 0 00 00 0 --> 1001
2. Convert the obtained binary string into an integer.
1001 --> 9
INPUT SAMPLE
00 0 0 00 00 0
00 0
00 0 0 000 00 0000000 0 000
0 000000000 00 00
OUTPUT SAMPLE
9
1
9208
3
My Code
#!/usr/bin/env ruby -w
def juggling_with_zeros(args)
res = ''
args.each_slice(2) do |a, b|
if a == '0'
res << '0' * b.size
else
res << '1' * b.size
end
end
res.to_i(2)
end
ARGF.each_line do |line|
puts juggling_with_zeros(line.chomp.split)
end