importFoundation/*
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.
*/funccustomersWithRewards(customerDict:[String:[Int]],minNum:Int,minAmt:Int)->[String:[Int]]{varrewarded:[String:[Int]]=[String:[Int]]()forcustomerincustomerDict{// filter all transactions, leaving only ones greater than or equal to the Min AmountleteligibleTransactions=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}}returnrewarded}// variable set 1letmin_orders1=5letmin_price1=20letcustomers1=["John Doe":[22,30,11,17,15,52,27,12],"Jane Doe":[5,17,30,33,40,22,26,10,11,45]]// variable set 2letmin_orders2=2letmin_price2=50letcustomers2=["Joey Bonzo":[22,67,53,29],"Jennifer Bonzo":[51,19]]// variable set 3letmin_orders3=3letmin_price3=15letcustomers3=["Natsumi Sakamoto":[15,15,14],"Gorou Hironaka":[15,15,15],"Shinju Tanabe":[120,240]]// case 1print("Eligible Customers for case 1:")print(customersWithRewards(customerDict:customers1,minNum:min_orders1,minAmt:min_price1))print("\n\n")// case 2print("Eligible Customers for case 2:")print(customersWithRewards(customerDict:customers2,minNum:min_orders2,minAmt:min_price2))print("\n\n")// case 3print("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.
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 solution:
output:
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.