In an attempt to boost sales, the manager of the pizzeria you work at has devised a new pizza rewards system. Although, he isn't exactly sure what he wants the variables to be. The rewards system may change in the future. Your manager wants you to implement a function that, given a dictionary of customers, a minimum number of orders, and a minimum order value, returns a set of the customers who are eligible for a reward.
Customers in the dictionary are represented as:
{ 'customerName' : [list_of_order_values_as_integers] }
Test 1:
Make at least 5 orders of at least 20$ each and get a free pizza!
min_orders = 5 min_price = 20 customers = { 'John Doe' : [22, 30, 11, 17, 15, 52, 27, 12], 'Jane Doe' : [5, 17, 30, 33, 40, 22, 26, 10, 11, 45] }
Test 2:
Make at least 2 orders of at least 50$ each and get a free pizza!
min_orders = 2 min_price = 50 customers = { 'Joey Bonzo' : [22, 67, 53, 29], 'Jennifer Bonzo' : [51, 19] }
Test 3:
Make at least 3 orders of at least 15$ each and get a free pizza!
min_orders = 3 min_price = 15 customers = { 'Natsumi Sakamoto' : [15, 15, 14], 'Gorou Hironaka' : [15, 15, 15], 'Shinju Tanabe' : [120, 240] }
This challenge comes from kingcobra on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (9)
Elm
Explainations
This will define anything that we choose to export to the outside world importing our module. In this case, we only want to expose our
Customer
type alias &freePizza
function for our tests (see below).This will define a customer in our application. Note that I choosed to use a
name
field for the name of the customer and apurchases
field for the total record of the customer's purchases.This will define the inner logic of the challenge. Basically what it does is:
.purchases
which is a function that accepts a customer and returns a list of integers (the purchases)..purchases : Customer -> List Int
. It is equivalent to this in JavaScript:const { purchases } = customer;
.((<=) minimumPrice)
part is equivalent to(\purchase -> purchase >= minimumPrice)
This will filter out all the customer that are non-eligible to our current free pizza's policy.
Tests
Playground
Test it online here.
Javascript
Elixir:
Tests:
Here's one in TypeScript
My preferred way of passing bags of arguments to functions in JS is by destructuring an object, which gets around the whole lets-just-remember-the-second-argument-was-minOrders thing.
The hacky embedded test harness is just there to make this portable.
hmm, I think I found a bug in the codepen plugin, where the template string at the bottom is being styled like it never got closed.
Will find the repo and pop in a bug report.
My solution in js
c++, hope it's correct.
Python solution
APL - Solution 1 (using Dyalog APL)
I'm passing the argument in a suitable array-format for convenience and brevity:
Output:
APL - Solution 2
Since the task was shown with JSON-Data, I want to also show how to tackle this with JSON-Data in APL.
Testing
Feel free to experiment and Try it online! ;)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.