Created
April 9, 2017 10:04
-
-
Save preetpalS/68a23e76fd7cdd9bbb78b45aa18108d4 to your computer and use it in GitHub Desktop.
tidy numbers
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
#!/usr/bin/env ruby | |
require 'set' | |
def is_tidy(n) | |
narr = n.to_s.split('') | |
snarr = narr.sort | |
narr.join('') == snarr.join('') | |
end | |
def find_previous_tidy_number(n) | |
(n.downto 1).each do |x| | |
return x if is_tidy(x) | |
end | |
end | |
def process(line) | |
find_previous_tidy_number(Integer(line)) | |
end | |
ARGF.each_with_index do |line, idx| | |
next if idx.zero? | |
puts "Case ##{idx}: #{process(line.strip)}" | |
end | |
# Usage: ./problemB.rb < input.txt > output.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment