Waste less time on Facebook — follow Brilliant.
×

Conditionals

If you're tired, then sleep. Else, if you're hungry, then eat. Else, you've got some time to expand your logical programming skills by checking out conditionals!

Concept Quizzes

Pattern Matching

     

Suppose you have a stream of numbers that come in from a highway weigh station, which measures the mass of the delivery trucks that drive on the highway. The scale automatically sends its measurements to a database every 5 minutes, which you analyze at the end of the day. Because the trucks sometimes stay on the scale for longer than five minutes, the scale sometimes sends multiple consecutive records for the same truck, called a stutter.

For example, in the list [200; 271; 305; 305; 180], the 305 entry is a stutter.

By contrast, the list [305; 200; 271; 305; 180] has no stutters. We consider the two entries of 305 to be unique measurements because they are separated by one or more events.

Take this list of weigh station measurements from a given day, write a recursive function to destutter the list. How many unique weighing events were there that day?

Assumptions

  • For the purposes of this question, any consecutive entries in the list which are equal to each other are considered stutters (not unique measurements).

Consider the following function with given a number and a list, returns true if that number is in the list, and false if not.

1
2
3
4
let rec test x l =
  match l with
  [] -> false
  | h::t -> h = x || test x t

Using the above function write another function, which given a list returns a list with all its duplicates removed.

For example given the list \([3; 2; 2; 3; 4]\), returns \([2; 3; 4]\). What will be the sum of the resulting list after the list given below passes the above function:

\[[2; 7; 11; 7; 2]\]

You run a website that sells products at a steep discount, one at a time, while supplies last. Recently, your site has been experimenting with a different variety of products than usual, and sales have taken a hit, and you need to make some changes to boost sales back to their previous level. Luckily, you keep some information on the shopping habits of your customers.

You ask your data team to find you the shopping preferences of all your "super shoppers", those accounts that are least picky in taking your deals. The customer records are stored in the form

1
[Str name, Char acct_type, Int pref_group]

The name field is obviously the customer's name, acct_type indicates the degree of spending the customer has had in the recent past. If acct_type is an uppercase letter from 'D' to 'Z', that indicates that they fit the profile of an occasional, picky shopper. If acct_type is an uppercase letter from 'A' to 'C', however, that indicates that they tend to be loose with their money on the site, and are thusly dubbed "elite shoppers".

Finally, pref_group maps them onto one of the various product profiles you've previously identified in the user base. For example, if pref_group is 5, that might mean that they tend to buy winter-time items like skis, while 7 might indicate a strict interest in basketball.

Write a pattern matching routine to harvest the pref_group of all elite shoppers in your database.

Of your elite shoppers, what is the most popular pref_group?

×

Problem Loading...

Note Loading...

Set Loading...