You're saying goodbye to your friend when they say to you, "See you next happy year!" You smile and wave, saying you'll see them then. But wait, when's the next happy year anyway?
Given a year, write a function that will return the closest year you'll see your friend, the next year with all unique digits.
Years will always be represented as positive integers. It is not necessary for the year passed through the function to be a Happy one.
Examples:
nextHappyYear(7712) ==> 7801
nextHappyYear(1001) ==> 1023
nextHappyYear(2018) ==> 2019
This challenge by MrZizoScream on CodeWars was used for inspiration. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (13)
My ugly answer. But I thought I would at least give this a go in python.
Here is my solution using PHP:
And here is my unit tests:
Hope you like it :)
APL (I'm using Dyalog APL)
Code:
Tests:
Simple JS solution:
And a PHP solution:
Great, thanks! I blanked out on this for some reason :)
quick python solution
Rust:
This was a strange exercise to do in Rust. Feels dirty to use conversion to string :(
Elixir (now with doctests!):
And a test file (just for the sake of completeness):
Javascript:
Results:
Clever usage of Set!
Elm
Tests
Edit
Just realized there is a native Set module in Elm by looking at the comments made in JavaScript which is awesome, but I'll keep this as is for the record. I'm edgy anyway!
Edit2
Turns out, after an interesting talk with the folks at the Elm Slack, and some experimentations, there is no way to to
Set
that keeps the order of theList
due to the Haskell implementation behind, preventing to have an easy solution. So this means either use an extra library (which I tend to avoid since I wanted to show a native Elm solution) or use a combination just like I did!Using a regex in Perl: