DEV Community

Discussion on: Daily Challenge #74 - Free Pizza

Collapse
 
earlware profile image
EarlWare

Swift solution:

import Foundation

/*
 Free Pizza Challenge Function

 @param customerDict a list of customer names(keys, String) with associated transaction lists(values, [Int])
 @param minNum is the minimum number of transactions over the specified dollar amount to be eligible for rewards
 @param minAmt is the dollar amount the transactions need to surpass to be eligible for rewards.

 @return a list of the customer dictionary entries which are all eligible for rewards.
 */
func customersWithRewards(customerDict:[String : [Int]],
                          minNum:Int,
                          minAmt:Int) -> [String : [Int]] {

    var rewarded:[String : [Int]] = [String : [Int]]()

    for customer in customerDict {  
        // filter all transactions, leaving only ones greater than or equal to the Min Amount
        let eligibleTransactions = customer.value.filter({ $0 >= minAmt})

        // see if there are enough transactions over the given cost, and if so add the customer and their transaction list to the dictionary to return.
        if (eligibleTransactions.count >= minNum) {
            rewarded[customer.key] = customer.value
        }
    }
    return rewarded
}




// variable set 1
let min_orders1 = 5
let min_price1 = 20
let customers1 = ["John Doe" : [22, 30, 11, 17, 15, 52, 27, 12],
                  "Jane Doe" : [5, 17, 30, 33, 40, 22, 26, 10, 11, 45]]

// variable set 2
let min_orders2 = 2
let min_price2 = 50
let customers2 = ["Joey Bonzo" : [22, 67, 53, 29],
                  "Jennifer Bonzo" : [51, 19]]

// variable set 3
let min_orders3 = 3
let min_price3 = 15
let customers3 = ["Natsumi Sakamoto" : [15, 15, 14],
                  "Gorou Hironaka" : [15, 15, 15],
                  "Shinju Tanabe" : [120, 240]]


// case 1
print("Eligible Customers for case 1:")
print(customersWithRewards(customerDict: customers1, minNum: min_orders1, minAmt: min_price1))
print("\n\n")

// case 2
print("Eligible Customers for case 2:")
print(customersWithRewards(customerDict: customers2, minNum: min_orders2, minAmt: min_price2))
print("\n\n")

// case 3
print("Eligible Customers for case 3:")
print(customersWithRewards(customerDict: customers3, minNum: min_orders3, minAmt: min_price3))
print("\n\n")

output:

Eligible Customers for case 1:
["Jane Doe": [5, 17, 30, 33, 40, 22, 26, 10, 11, 45]]


Eligible Customers for case 2:
["Joey Bonzo": [22, 67, 53, 29]]


Eligible Customers for case 3:
["Gorou Hironaka": [15, 15, 15]]


Program ended with exit code: 0

I choose to loop through the customer list and filter out transactions in a separate statements just for a nice easy read.
Although I'm pretty sure that could be shortened down with a bit by calling .filter() on the customer list itself and nesting another .filter() for the transactions inside that closure... but with all the mess of nesting closures that would be needed, I just felt like readability rather than shortness of code wins the day on this one.