Skip to content

Instantly share code, notes, and snippets.

@hugoferreira
Last active April 10, 2020 23:24
Show Gist options
  • Save hugoferreira/97aa38473b46647706a60e3e3c7d37b6 to your computer and use it in GitHub Desktop.
Save hugoferreira/97aa38473b46647706a60e3e3c7d37b6 to your computer and use it in GitHub Desktop.
Code Golf Challenges

Challenge 1 (10/4/2020)

Implement a function that given a list of natural numbers, returns the lowest number not present in the list.

Premises

0 <= list size <= 2^16
len(unique(list)) = len(list)

Examples

minFree of 1, 2, 3, 4, 5 returns 6
minFree of an empty list returns 1
minFree of 10, 5, 9, 2, 24, 3, 1 returns 4

Solutions

(Chars) Language by Name

cenas = cenas
@andresilva
Copy link

andresilva commented Apr 10, 2020

Rust (63 chars)

let minFree=|x:Vec<u64>|(1..).filter(|i|!x.contains(i)).next();

Rust untyped (54 chars)

let minFree=|x|(1..).filter(|i|!x.contains(i)).next();

Scala (47 chars)

def minFree(x:Seq[Int])=(1 to 2<<15).diff(x)(0)

Scala untyped (38 chars)

def minFree(x)=(1 to 2<<15).diff(x)(0)

Scalaskell (33 chars)

minFree x=(1 to 2<<15).diff(x)(0)

JavaScript (51 chars)

minFree=x=>{for(i=1;;i++)if(!x.indexOf(i))return i}

@ferrolho
Copy link

For 0 <= list size <= 2^16:

Julia (41 chars)

minFree(x::Set{Int})=setdiff(1:2^16,x)[1]

Julia untyped (31 chars)

minFree(x)=setdiff(1:2^16,x)[1]

For 0 <= list size <= 2e16:

Julia (56 chars)

minFree(x::Set{Int})=for i=1:Int(2e16) ix&&return i end

Julia untyped (41 chars)

minFree(x)=for i=1:2e16 ix&&return i end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment