I just wrote a small Ruby class to solve the first of the challenges in the G CodeJam. I am not strong at this sort thing so very sure it can be simplified massively! The program is correct for both the small and large inputs.
Problem
Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N. Then she starts naming N, 2 ×ばつ N, 3 ×ばつ N, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep.
Bleatrix must start with N and must always name (i + 1) ×ばつ N directly after i ×ばつ N. For example, suppose that Bleatrix picks N = 1692. She would count as follows:
N = 1692. Now she has seen the digits 1, 2, 6, and 9. 2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9. 3N = 5076. Now she has seen all ten digits, and falls asleep. What is the last number that she will name before falling asleep? If she will count forever, print INSOMNIA instead.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a single integer N, the number Bleatrix has chosen.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the last number that Bleatrix will name before falling asleep, according to the rules described in the statement.
class CountingSheep
def solve(input)
return "INSOMNIA" if input == 0
current = input
seen_numbers = []
loop do
seen_numbers.concat(current.to_s.split(//))
break if seen_numbers.uniq.size == 10
current += input
end
current
end
def get_inputs(data_size)
array = []
path = "~/Desktop/counting_sheep/A-#{data_size}-practice.in"
File.open(File.expand_path(path), 'rb') do |f|
f.each_line do |line|
array << line.strip
end
end
array.shift
array
end
#'small' or 'large' data_size input
def output(data_size: "small")
count = 1
get_inputs(data_size).each do |input|
output = solve(input.to_i)
puts "Case ##{count}: #{output}"
count += 1
end
end
end
-
\$\begingroup\$ Habit I guess. You are of course right - it's definitely not needed. \$\endgroup\$Ben Hawker– Ben Hawker2016年04月14日 02:36:09 +00:00Commented Apr 14, 2016 at 2:36
1 Answer 1
In no particular order:
I'd prefer
.zero?
rather than== 0
You could use a hash instead of an array and remove the need to call
uniq
. That method quickly gets pretty expensive, especially since you're calling it on an ever-growing arrayI'd like the
solve
method to return either an integer ornil
, instead of mixing string and number return types.
As for getting digits from a number, I'd suggest using #divmod
. Coverting to a string and splitting always irks me a little, even if it is a pretty straightforward solution. But like the return type from solve
, I see no reason to involve strings when trying to get numbers from a number.
Basically, you can do something like this:
require 'set'
def unique_digits(number)
digits = Set.new
remainder = number
while remainder > 0
remainder, digit = remainder.divmod(10)
digits << digit
end
digits
end
This is using a Set
(part of the stdlib) since it guarantees uniqueness - no need to call uniq
on an array.
As for the core of the task:
def count_sheep(n)
return nil if n.zero?
digits = Set.new
current = 0
while digits.size < 10
current += n
digits += unique_digits(current)
end
current
end
All you need then is the method to run it. I'd suggest something like:
def read_input(io)
cases = io.gets.strip.to_i
cases.times do |i|
n = io.gets.strip.to_i
puts "Case ##{i+1}: #{count_sheep(n) || "INSOMNIA"}"
end
end
Where io
is a file handle or stdin. That way you can run your script in two ways; piping things to stdin, or reading from a file. All it needs is an IO
object. E.g.:
read_input(ARGV[0] ? File.open(ARGV[0]) : STDIN)
should let run either $ ruby sheep.rb <file>
or $ cat <file> | ruby sheep.rb
In all:
require 'set'
def unique_digits(number)
digits = Set.new
remainder = number
while remainder > 0
remainder, digit = remainder.divmod(10)
digits << digit
end
digits
end
def count_sheep(n)
return nil if n.zero?
digits = Set.new
current = 0
while digits.size < 10
current += n
digits += unique_digits(current)
end
current
end
def read_input(io)
cases = io.gets.strip.to_i
cases.times do |i|
n = io.gets.strip.to_i
puts "Case ##{i+1}: #{count_sheep(n) || "INSOMNIA"}"
end
end
read_input(ARGV[0] ? File.open(ARGV[0]) : STDIN)