Implement a ruby method to find the next largest number with the same digits of the input number. Ex: 38276 would return 38627.
Asked
Active
Viewed 661 times
-6
-
1Instead of asking _us_ to implement such method, tell us what's keeping _you_ from doing so. – Stefan Jul 01 '19 at 07:02
-
3This might help: [Given a number, find the next higher number which has the exact same set of digits as the original number](https://stackoverflow.com/q/9368205/477037) – Stefan Jul 01 '19 at 07:46
-
2You do this by writing a program which does what you want. If you have a problem with your program, carefully read the documentation of all the methods, classes, modules, and libraries you are using, write tests for your programs, trace the execution with pen and paper, single-step it in a debugger, then sleep on it, start again from the beginning, sleep on it again, and *then and only then* narrow your problem down to a concise, focused, simple, short, reproducible [mre] and ask a specific, focused, narrow question on [so]. – Jörg W Mittag Jul 01 '19 at 08:26
-
3Brimasen, your question has been reopened. If it is a homework problem please edit to describe efforts you have made to solve it yourself, as required by SO rules. Regardless, you should restate the question to tell us what help you need and to respond to comments. If you do not do so I think it is unlikely that readers will devote their time to helping you. – Cary Swoveland Jul 01 '19 at 13:16
2 Answers
2
It wouldn't be the fastest one, but does the job!
number = 38276
options = number.to_s.chars.permutation.map{|s| s.join.to_i}.uniq.sort
options[options.index(number) + 1] #=> 38627

Babar Al-Amin
- 3,939
- 1
- 16
- 20
0
Another way to get the next number with same digits :
def next_number_with_same_digits(number)
number.to_s.chars.permutation.map { |e| e.join.to_i }.select { |n| n > number }.min
end
If you want to get the last number with same digits :
def last_number_with_same_digits(number)
number.to_s.chars.permutation.map { |e| e.join.to_i }.select { |n| n < number }.max
end

user11659763
- 183
- 8
-
1The `uniq` call is what makes the difference. The `index &&` addition is superfluous. – Stefan Jul 01 '19 at 13:02
-
Yep ^^ I added the `index &&` to prevent an unwanted exception (I could have used rescue or even raised something if index was nil). – user11659763 Jul 01 '19 at 13:11
-
1It won't be `nil` – the permutations of all digits will always generate the number itself. – Stefan Jul 01 '19 at 13:14
-
Something like this : def next_larger_number_with_same_digits(arg) arg.to_s.chars.permutation.map { |i| i.join.to_i }.reject { |i| i <= arg }.first end ? – user11659763 Jul 01 '19 at 13:37
-
No attribution required. If you have two solutions with a strong preference for one you can strengthen your answer by giving only the better solution. Think of it as writing an article or book. You circulate a draft for comments. Based on those comments you may revise your text, without providing a running commentary on your edits. – Cary Swoveland Jul 01 '19 at 20:40
-
A few points about the code you added: 1) `<=` in `reject { |n| n <= number }` should be `>=` or, maybe clearer, write `select { |n| n < number }` (notice I've named the variable `n`, for "number", as `i` is often used for "index"); 2) `sort.first` can be replaced by `max`; and 3) if the array produced by `select { |n| n < number }` is empty, `[].max #=> nil`, which is what you want. – Cary Swoveland Jul 01 '19 at 20:47
-
Ok, I'll try to edit the answer to give the best solution but I have two questions first. With select n < number followed by max I'm getting the last number with the same digit. Did I understood the question wrong ? And for the |i| used as index I agree with you, should I use e (element) or s (string) for the map ? – user11659763 Jul 02 '19 at 06:19