19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
defmoduleDay71do@doc"""
Finds the next happy year after `year`.
iex> Day71.next_happy_year(7712)
7801
iex> Day71.next_happy_year(1001)
1023
iex> Day71.next_happy_year(2018)
2019
"""@specnext_happy_year(pos_integer)::pos_integerdefnext_happy_year(year)do(year+1)|>Stream.iterate(&(&1+1))|>Enum.find(&happy?/1)end@doc"""
Checks if a year is a happy year.
A year is happy if all its digits are unique.
iex> Day71.happy?(2019)
true
iex> Day71.happy?(2021)
false
"""@spechappy?(pos_integer)::booleandefhappy?(year)dodigits=Integer.digits(year)Enum.uniq(digits)==digitsendend
And a test file (just for the sake of completeness):
moduleNextHappyYearTestexposing(suite)importExpectexposing(equal)importNextHappyYearexposing(nextHappyYear)importTestexposing(Test,describe,test)suite:Testsuite=describe"Next happy year"[test"It should return 7712 when passing 7801"<|\_->equal7801<|nextHappyYear7712,test"It should return 1001 when passing 1023"<|\_->equal1023<|nextHappyYear1001,test"It should return 2019 when passing 2018"<|\_->equal2019<|nextHappyYear2018]
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 the List 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!
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Javascript:
Results:
Clever usage of Set!
Elixir (now with doctests!):
And a test file (just for the sake of completeness):
Using a regex in Perl:
quick python solution
My ugly answer. But I thought I would at least give this a go in python.
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
Setthat keeps the order of theListdue 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!Rust:
This was a strange exercise to do in Rust. Feels dirty to use conversion to string :(
Here is my solution using PHP:
And here is my unit tests:
Hope you like it :)
Simple JS solution:
And a PHP solution:
APL (I'm using Dyalog APL)
Code:
{4=+/⎕d∊⍕⍵:⍵⋄∇⍵+1}Tests:
{4=+/⎕d∊⍕⍵:⍵⋄∇⍵+1}7712 7801 {4=+/⎕d∊⍕⍵:⍵⋄∇⍵+1}1001 1023 {4=+/⎕d∊⍕⍵:⍵⋄∇⍵+1}2019 2019Great, thanks! I blanked out on this for some reason :)