Created
October 26, 2015 23:38
-
-
Save anonymous/868600af5bc470f06a2b 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
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