For this game of BINGO, you will receive a single array of 10 numbers from 1 to 26 as an input. Duplicate numbers within the array are possible.
Each number corresponds to their alphabetical order letter (e.g. 1 = A. 2 = B, etc). Write a function where you will win the game if your numbers can spell "BINGO". They do not need to be in the right order in the input array). Otherwise you will lose. Your outputs should be "WIN" or "LOSE" respectively.
Test Arrays:
bingo([21,13,2,7,5,14,7,15,9,10])
bingo([1,2,3,4,5,6,7,8,9,10]
Good luck!
This challenge comes from julesnuggy on CodeWars. 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!
Top comments (14)
PHP Version
Why adding
public
before thefunction
keyword?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
good ol' Clojure ๐
das tests (โโ _โ )
JavaScript version:
Explanation:
The
Set
object lets you storeunique
values 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".
JS:
Elm
Eplainations
Here we say that we only want the function
bingo
to be exposed to the outside world.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 theintegerList
thanks to theList.member
function.Finally, we define our main function
bingo
which takes a list of integers. TheList.all
takes a function applied to each one of ourBINGO
list (which is just the integer representation of that word). If there is noBINGO
integer in theintegerList
, it will return false.List.all
expects all applied functions to returnTrue
. If only one of them returnFalse
(meaning, one of theBINGO
integer has not been found in theintegerList
), it will return false.Tests
Try it online.
C#
My solution in js
Whit Python
`
Solved with Purescript
bingo :: Set Int -> Boolean
bingo input =
subset (fromFoldable [ 2, 9, 14, 7, 15 ]) (fromFoldable input)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.