DEV Community

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

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!