Created
July 16, 2015 08:23
-
-
Save oieioi/e6e151f8c04611bbbd66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_patterns(number, operators) | |
numbers = number.split '' | |
size = numbers.size | |
p operators | |
.repeated_permutation(size - 1) | |
.map { |operators_combination| | |
numbers.map.with_index {|num, index| "#{num}#{operators_combination[index]}"} | |
} | |
.map { |item| eval item.join('').gsub(/^0/, '')} | |
end | |
def divisible_by?(number, operandos) | |
not operandos | |
.select { |operando| number % operando == 0 } | |
.empty? | |
end | |
def solve | |
raw_list = [] | |
while str = STDIN.gets#.chomp | |
break if str.empty? | |
raw_list.push str.chomp | |
end | |
one_column_primes = [2, 3, 5, 7] | |
operators = ['', '+', '-'] | |
raw_list.each { |num| | |
p get_patterns(num, operators) | |
.select {|pattern| not pattern.nil? and pattern > 0} | |
.select {|pattern| divisible_by? pattern, one_column_primes} | |
.size | |
} | |
end | |
solve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment