TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Card exercise

Forums > Swift

Hello guys. I’m having some issues with an exercise and I’m stuck, to be honest I don’t know where to begin. Ok so the task is this: I need to write a code that takes a certain number of cards “N” and shows how many decks I can get, deck is defined as a set of 32 unique cards.

My first thoughts were sorting out enums for suits and values and I know how to create a deck but I don’t think that is necessary here. Any help would be appreciated and thank you!

2      

its just a division problem, ignoring any left overs i.e. 31 cards isnt going to work. And int will discard this automatically.

func getNumberOfDecks(fromCards cards:Int, deckSize:Int) -> Int {
    return cards / deckSize
}

let decks = getNumberOfDecks(fromCards: 100, deckSize: 32)

If this is for a course please add in error tests, at least check for 0 on the decksize.

2      

It really is a simple division. I got confused as the excercise said "32unique cards", also a simple 0...X range could have been used. I got it now, thank you for your reply!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.