P
P
Pavel2019-04-02 18:54:20
Swift
Pavel, 2019-04-02 18:54:20

How to implement the method more efficiently?

The card has 4 features. You need to compare 3 cards. Each sign must either completely match the three cards, or be completely different. For example, .color must either be the same for three cards, or each card has its own

struct Card: Hashable {
    var symbolsCount = PropertyVariant.one
    var shape = PropertyVariant.one
    var color = PropertyVariant.one
    var filling =  PropertyVariant.one
    
    enum PropertyVariant: Int {
        case one = 1, two, three
    }
}

    func isSet() -> Bool {

        // let cardsForSet: [Card] = Array of 3 cards
        let comparisonValue = cardsForSet[0]
        let isPropertySet: ([Card]) -> Bool = { $0.count == 1 || $0.count == 3 }

        guard isPropertySet(cardsForSet.filter { $0.symbolsCount == comparisonValue.symbolsCount }) else {
            return false
        }
        guard isPropertySet(cardsForSet.filter { $0.shape == comparisonValue.shape }) else {
            return false
        }
        guard isPropertySet(cardsForSet.filter { $0.color == comparisonValue.color }) else {
            return false
        }
        guard isPropertySet(cardsForSet.filter { $0.filling == comparisonValue.filling }) else {
            return false
        }
        return true
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question