Explanation:
The Set object lets you store unique values of any type.
That is why new Set([1,2,1,3,3,4]) will give a set of 1,2,3,4. Since a set is iterable whe can spread it into an new array, thus creating a new array containing only the unique values of the input array.
const uniqueValues = [...new Set(input)] can be written as follows:
The unique input values are then filtered with .filter()checking if each and every value is in the winning sequence using winSequence.includes(value).
This will result in an intersection between the unique values in the input array and the winning sequence. Then if the length of that intersection is the same as the length of the winning sequence, that means a "WIN".
(nsdailyChallenge.seventySix)(defn-bingo![board]"don't do a thang if it ain't got that swang. Β―\_(γ)_/Β―"(let[BINGO#{2914715}reduc_board(setboard)contains-all?#(and(doseq[p%1](contains?%2p)))](if(contains-all?BINGOredc_board)"WIN""LOSE")))
das tests (ββ _β )
(deftestfair-game?(is(="WIN"(bingo![211327514715910]))(is(="LOSE"(bingo!(vec(range110))));;just gettin' shwifty with the lists(run-tests'dailyChallenge.seventySix)
We then define a helper function belongsTo which will not be exposed to the outside world.
It takes two parameters: the first being a list (array) of integers, and the second is an integer. The function will then return a boolean indicating if the integer is indeed part of the integerList thanks to the List.member function.
Finally, we define our main function bingo which takes a list of integers. The List.all takes a function applied to each one of our BINGO list (which is just the integer representation of that word). If there is no BINGO integer in the integerList, it will return false. List.all expects all applied functions to return True. If only one of them return False (meaning, one of the BINGO integer has not been found in the integerList), it will return false.
Tests
moduleBingoTestexposing(suite)importBingoexposing(bingo)importExpectexposing(equal)importTestexposing(Test,describe,test)suite:Testsuite=describe"Basic tests"[test"It should return true when passing a valid array"<|\_->equalTrue<|bingo[21,13,2,7,5,14,7,15,9,10],test"It should return false when passing an invalid array"<|\_->equalFalse<|bingo[1,2,3,4,5,6,7,8,9,10]]
PHP Version
Why adding
publicbefore thefunctionkeyword?Force of habit.
I never code in a procedural way, I always have a class, so I declare method visibility ...
It's for sure a Fatal Error in a procedural code.
I'll edit my snippet in consequence
Okay I though it was a new language feature haha.
I didn't found any use case of this function in PHP before but now I do thanks to your take at the challenge. Well done btw!
Python with typing indicators
JavaScript version:
Explanation:
The
Setobject lets you storeuniquevalues of any type.That is why new
Set([1,2,1,3,3,4])will give a set of1,2,3,4. Since a set is iterable whe can spread it into an new array, thus creating a new array containing only the unique values of the input array.const uniqueValues = [...new Set(input)]can be written as follows:The unique input values are then filtered with
.filter()checking if each and every value is in the winning sequence usingwinSequence.includes(value).This will result in an intersection between the unique values in the input array and the winning sequence. Then if the length of that intersection is the same as the length of the winning sequence, that means a "WIN".
good ol' Clojure π
das tests (ββ _β )
JS:
Elm
Eplainations
Here we say that we only want the function
bingoto be exposed to the outside world.We then define a helper function
belongsTowhich will not be exposed to the outside world.It takes two parameters: the first being a list (array) of integers, and the second is an integer. The function will then return a boolean indicating if the
integeris indeed part of theintegerListthanks to theList.memberfunction.Finally, we define our main function
bingowhich takes a list of integers. TheList.alltakes a function applied to each one of ourBINGOlist (which is just the integer representation of that word). If there is noBINGOinteger in theintegerList, it will return false.List.allexpects all applied functions to returnTrue. If only one of them returnFalse(meaning, one of theBINGOinteger has not been found in theintegerList), it will return false.Tests
Try it online.
My solution in js
C#
Whit Python
`
Solved with Purescript
bingo :: Set Int -> Boolean
bingo input =
subset (fromFoldable [ 2, 9, 14, 7, 15 ]) (fromFoldable input)