UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.