Skip to content

Instantly share code, notes, and snippets.

Created October 26, 2015 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/868600af5bc470f06a2b to your computer and use it in GitHub Desktop.
Save anonymous/868600af5bc470f06a2b to your computer and use it in GitHub Desktop.
require 'set'
def possible?(n)
if n.to_s == n.to_s.chars.sort.reverse.join then false else true end
end
def same_digits?(a1, a2)
if Set.new(a1.to_s.chars) == Set.new(a2.to_s.chars) then true else false end
end
# puts possible?(4321) # false
# puts possible?(4231) # true
def next_bigger(n)
res = nil
if possible?(n) then
n.upto(Float::INFINITY) { |num| if same_digits?(n, num) && possible?(num)
then num
else nil end }
end
end
puts next_bigger(4231) # should be 4321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment