loading...

Daily Challenge #76 - Bingo! (or not...)

thepracticaldev profile image dev.to staff ・1 min read

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!

Discussion

pic
Editor guide
Collapse
juyn profile image
Xavier Dubois 🇫🇷

PHP Version

/**  
 * Bingo Challenge
 *
 * @param array $input Array of 10 number  
 *
 * @return string  
 */
 function bingo(array $input = []): string  
{  
    // BINGO  
    $winSequence = [2, 9, 14, 7, 15];  

    return ($winSequence === array_intersect($winSequence, $input)) ? 'WIN': 'LOSE';  
}
Collapse
aminnairi profile image
Amin

Why adding public before the function keyword?

Collapse
juyn profile image
Xavier Dubois 🇫🇷

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

Thread Thread
aminnairi profile image
Amin

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!

Collapse
thejessleigh profile image
jess unrein

Python with typing indicators

def bingo(numbers: list) -> str:
  bingo_set = set([2, 9, 14, 7, 15])
  if bingo_set.issubset(set(numbers)):
    return "WIN"
  else:
    return "LOSE"

assert bingo([21,13,2,7,5,14,7,15,9,10]) == "WIN"
assert bingo([1,2,3,4,5,6,7,8,9,10]) == "LOSE
Collapse
not_jffrydsr profile image
@nobody

good ol' Clojure 😏

(ns dailyChallenge.seventySix)

(defn- bingo! [board]
 "don't do a thang if it ain't got that swang. ¯\_(ツ)_/¯"
 (let [BINGO #{2 9 14 7 15}
       reduc_board (set board) 
       contains-all? #(and (doseq [p %1] 
                       (contains? %2 p)))]

  (if (contains-all? BINGO redc_board) "WIN" "LOSE")))

das tests (⌐■_■)

(deftest fair-game? 
  (is (= "WIN" (bingo! [21 13 2 7 5 14 7 15 9 10]))
  (is (= "LOSE" (bingo!(vec (range 1 10)))) ;;just gettin' shwifty with the lists

(run-tests 'dailyChallenge.seventySix)
Collapse
spetastian profile image
Sebastian

JavaScript version:

const winSequence = [2, 9, 14, 7, 15];
function bingo(input = []){
 return [...new Set(input)].filter(value => winSequence.includes(value)).length === winSequence.length ? "WIN" : "LOSE";
}

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:

const inputSet = new Set(input);
const uniqueValuesArray = [...inputSet]

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".

Collapse
aminnairi profile image
Amin

Elm

module Bingo exposing (bingo)


belongsTo : List Int -> Int -> Bool
belongsTo integerList integer =
    List.member integer integerList


bingo : List Int -> Bool
bingo input =
    List.all (belongsTo input) [ 2, 9, 14, 7, 15 ]

Eplainations

module Bingo exposing (bingo)

Here we say that we only want the function bingo to be exposed to the outside world.

belongsTo : List Int -> Int -> Bool
belongsTo integerList integer =
    List.member integer integerList

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.

bingo : List Int -> Bool
bingo integerList =
    List.all (belongsTo integerList) [ 2, 9, 14, 7, 15 ]

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

module BingoTest exposing (suite)

import Bingo exposing (bingo)
import Expect exposing (equal)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "Basic tests"
        [ test "It should return true when passing a valid array" <|
            \_ ->
                equal True <| bingo [ 21, 13, 2, 7, 5, 14, 7, 15, 9, 10 ]
        , test "It should return false when passing an invalid array" <|
            \_ ->
                equal False <| bingo [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
        ]

Try it online.

Collapse
vinniew1rus profile image
Vinnie

JS:

const bingo = (nums) => {
    const win = [2, 9, 14, 7, 15];
    return  nums.reduce((acc, num) => {
        if(win.includes(num)) acc.push(num);
        return acc;
    }, []).length > win.length ? 'WIN' : 'LOSE';
}
Collapse
kvharish profile image
K.V.Harish

My solution in js

const bingo = (arr) => [2, 9, 14, 7, 15].every((value) => arr.includes(value)) ? 'WIN' : 'LOSE';
Collapse
andreasjakof profile image
Andreas Jakof

C#

public string Bingo(byte[] numbers)
{
    List<byte> bingo = new List<byte>{2,7,9,14,15};
    return bingo.All(x => numbers.Contains(x)) ? "WIN" : "LOSE";
}
Collapse
wlloa profile image
Wlloa

Whit Python


win_condition = set([2,9,14,7,15])
def isBingo(input):
    if win_condition.issubset(set(input)):
        return "WIN"
    return "LOSE"


`

Collapse
karthicktamil17 profile image
karthick rajan

Solved with Purescript

bingo :: Set Int -> Boolean
bingo input =
subset (fromFoldable [ 2, 9, 14, 7, 15 ]) (fromFoldable input)

Collapse
earlware profile image
EarlWare

Swift Version:

import Foundation

func printAlphaToNumericMapping() {
    // create a range of UInt8s that map to the uppercase letters A through Z
    let upppercaseUTF8 = UInt8(ascii:"A")...UInt8(ascii:"Z")
    // then convert to a string
    let uppercase = String(data: Data(upppercaseUTF8), encoding: .utf8)

    //  now print each letter and the corresponding indexed value according to the bingo challenge.
    var index:Int = 1
    for letter in uppercase! {
        print(letter, "  ", index)
        index+=1
    }
}


/*
 Bingo challenge function

 @param Array of 10 Ints clamped to 1 ... 26

 @return String
*/

func bingo(numberList:[Int]) -> String {
    // create a immutable set with the numbers that correspond to BINGO
    let bingoSet:Set<Int> = [2, 9, 14, 7, 15]

    // If our winning set is part of the list of numbers passed in, we've won! Otherwise its a loss.
    if bingoSet.isSubset(of: numberList) {
        return "WIN"
    } else {
        return "LOSE"
    }
}



// uncomment to see listed letter to number mappings(ie:  G   7  or   O   15)
// printAlphaToNumericMapping()

let test1 = [21,13,2,7,5,14,7,15,9,10]
let test2 = [1,2,3,4,5,6,7,8,9,10]

print(bingo(numberList: test1))
print(bingo(numberList: test2))

//  not exactly test suite, but you get the idea
print("All tests passed:  ", (bingo(numberList: test1) == "WIN" && bingo(numberList: test2) == "LOSE"))

outputs the following:

WIN
LOSE
All tests passed:   true
Program ended with exit code: 0

I added a utility function to print out letters and corresponding numbers so I didn't have to sit there: B 1...2! I 1...2.......8...9! etc etc etc to create the bingo number set. But I definitely don't feel like my solution to it is very smooth... If anyone has any pointers on how to make an array of letters without having to do all the conversions to int, and reconverting the list back into a string, I'd love to see just how far I've over complicated this!