Skip to content

Instantly share code, notes, and snippets.

@preetpalS
Created April 9, 2017 10:04
Show Gist options
  • Save preetpalS/68a23e76fd7cdd9bbb78b45aa18108d4 to your computer and use it in GitHub Desktop.
Save preetpalS/68a23e76fd7cdd9bbb78b45aa18108d4 to your computer and use it in GitHub Desktop.
tidy numbers
#!/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