importFoundationfuncprintAlphaToNumericMapping(){// create a range of UInt8s that map to the uppercase letters A through ZletupppercaseUTF8=UInt8(ascii:"A")...UInt8(ascii:"Z")// then convert to a stringletuppercase=String(data:Data(upppercaseUTF8),encoding:.utf8)// now print each letter and the corresponding indexed value according to the bingo challenge.varindex:Int=1forletterinuppercase!{print(letter,"",index)index+=1}}/*
Bingo challenge function
@param Array of 10 Ints clamped to 1 ... 26
@return String
*/funcbingo(numberList:[Int])->String{// create a immutable set with the numbers that correspond to BINGOletbingoSet: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.ifbingoSet.isSubset(of:numberList){return"WIN"}else{return"LOSE"}}// uncomment to see listed letter to number mappings(ie: G 7 or O 15)// printAlphaToNumericMapping()lettest1=[21,13,2,7,5,14,7,15,9,10]lettest2=[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 ideaprint("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!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Swift Version:
outputs the following:
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!