DEV Community

loading...

Discussion on: Daily Challenge #12 - Next Larger Number

Collapse
tanguyandreani profile image
Tanguy Andreani

Ruby

Put this together on my phone sorry for formatting.

This one doesn’t generate all the permutations of all the digits.

[9999, 12, 513, 2019].each do |initial|
  next_larger = initial.to_s.reverse
  next_larger.length.times do |i|
    next_larger.length.times do |j|
      # luckily for us, ascii numbers are stored
      # in the same order as actual numbers
      if next_larger[j] > next_larger[i]
        tmp = next_larger[j]
        next_larger[j] = next_larger[i]
        next_larger[i] = tmp
      end
    end
  end
  next_larger.reverse!

  puts "#{initial}\t#{next_larger}" if initial != next_larger.to_i
end

repl.it/@daxyq/DailyChallenge12