### Daily Challenge #12 - Next Larger Number

#### dev.to staff on July 10, 2019

Apologies on missing yesterday's challenge. We have a fun one to hop in on today. If you haven't been following the series, feel free to give thi...
[Read Full]

Read this, sat down, just typed the following into

`irb`

, seems to work. Not recommended, but I do like Ruby's standard library:You didn't handle the case when the number is the largest possible and it should return -1

The code above returns

`nil`

, which is Ruby's`null`

.My bad then, thought like was a go code :)

No worries, the snippet is a bit dense.

But what’s the point of these isolated little exercises if not pushing your language to its limits? Sure, you can turn everything into an enterprise “masterpiece”, but ugly/stupid/throwaway code is where one can learn, enjoy and explore.

Exactly! That is also part of challenge for others to read others codes.

## Perl 5

Why JSON?

Because debugging and forgetting to remove it when done.

Here's mine.

First, a function for permuting the digits (using recursion):

Next, a sorting function:

And finally, the function to calculate the next largest number itself, relying on the idea that if we sort in numerical ascending order and filter out all the items less than the requested number, then the first one remaining must be our next largest number.

Full code and tests in gist: gist.github.com/kerrishotts/a0a96d...

Ruby solutionTrying out

`Object#then`

, which was introduced recently in Ruby 2.6, just for the fun of it.Took a long drive up for vacation yesterday so I wasn't able to get this types out! I think I know what I'm gonna implement so just gotta see if I can't type it out today! Can't get oo far behind on these challenges! I owe two now !

Got it done! Was able to write out what I was thinking last night without too much hassle, pretty happy with how it came out.

Could have tried not converting to chars in the middle, but it worked out pretty nicely still I think!

I notice a lot of people did theirs differently so I thought I'd explain what I did!

One of the things I noticed that led to my solution, was the fast that the next largest number, was 1 'sort' away from the number we had. What I mean is if we imagine our number as an array of its digits, the number we wanted was 1 swap away AND would make our 'array' more sorted than it was before!

This made me realize that a modified bubble sort was exactly what I was looking for! So below I conconted something loosely based on a bubble sort. It starts at the end of the number and moves backward seeing if it can make a swap. If it does, it returns the swapped value. If we make it to the beggining of the list we know there wasn't a larger number and simply return

`None`

!I wanted to do it like this (in Erlang), but didn’t figure out how to. Nice.

I think you have a mistake:

`next_largest(351)`

should return`513`

, and you return`531`

.That’s where I gave up with the swapping approach.

This was my solution on CodeWars (JS):

I’m learning

Erlang.At first, I wanted to use digit swapping, like @coreyja , but it was harder than I initially thought (e.g.

`351`

needs two swaps, because the next larger number is`513`

, not`531`

.)To validate the swapping method, I wrote a blunt version that converts a number into an array of digits, lists all the permutations, sorts them and finds the next occurence. For that I searched for a permutation function. It uses generators and list comprehensions, and I haven’t taken the time to understand it yet, but I’ll definitely try that in future challenges.

So my current solution is the blunt one (and I removed the unit tests for the intermediate functions, that are useful for development but not interesting for the solution).

I know I am late.

This what I did in C#

## Perl 6

My solution in js

RubyPut this together on my phone sorry for formatting.

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

repl.it/@daxyq/DailyChallenge12

Ooops. Today I caught the challenge a bit late... I sketched something, hopefully I'll finish it by tomorrow before the next challenge :-/

I didn't get to this one till today too, and I still own one from a few days ago! So don't feel too bad lol