You are creating Nile.com, which you hope will be the next big thing in online bookstores. You know that you can save money by anticipating what books people will buy; you will pass these savings on to your users by offering a discount if they buy books that Nile recommends.
To do this, you offer an incentive for people to upload their lists of recommended books. From their lists, you can establish suggested pairs. A pair of books is a suggested pair if both books appear on one person’s recommendation list. Of course, some suggested pairs are more popular than others. Also, any given book is paired with some books much more frequently than with others.
You need to organize the list of recommended books to support two tasks:
When someone buys a book, you want to be able to suggest a second book to accompany it. Specifically, you should provide the book it is most frequently paired with, along with a count of how frequent this is. Because there may be more than one book with that count, you should return a list of books (even if there is only one) as well as the count. This will be in the form of a Recommendation, as described below.
Sometimes, an indecisive user asks for a book recommendation. Nile offers not one recommendation at a time, but pairs of them! Wow! To support this, you must be able to identify the most popular pairs of suggested books. Return the most popular pair, with a count of how often it occurs. Again, because there may be multiple pairs with the same count, you should return a list of pairs (even if there is only one).
A list of recommended books is represented as a File:
| file(name :: String, content :: String)
file("vty.txt", "Crime and Punishment\nHeaps are Lame\nLord of the Flies")
An output recommendation is represented as a Recommendation:
| recommendation(count :: Number, names :: List<String>)
f1=file("alist.txt","1984\nAnimal Farm\nHigurashi\nLife of Pi")
f2=file("blist.txt","Animal Farm\nHigurashi\nLife of Pi")
f3=file("clist.txt","1984\nHeart of Darkness")
recommend("1925",[list: f1,f2,f3]) is
recommend("1984",[list: f1,f2,f3]) is
recommendation(1,[list: "Animal Farm","Higurashi","Life of Pi","Heart of Darkness"])
popular-pairs([list: f1,f2,f3]) is
recommendation(2,[list: "Animal Farm+Higurashi","Animal Farm+Life of Pi","Higurashi+Life of Pi"])
fun recommend(title :: String, book-records :: List<File>) -> Recommendation
recommend takes a book title and a list of Files and produces a Recommendation. The Recommendation’s names is a list of the titles of the books that are most often paired with the input book. The Recommendation’s count is the number of times the books in the names list are each paired with the input book.
fun popular-pairs(records :: List<File>) -> Recommendation
popular-pairs takes a list of Files and produces a Recommendation. The Recommendation’s names is a list of Strings each of which represent a pair of books. Each of these Strings contains two titles separated by a "+", for example "book1+book2". Each most popular pair of books should appear exactly once and order is irrelevant. The Recommendation’s "count" is the number of times each pair occurred together in a file.
For this assignment, you will need to write your own version of any built-in functions that you choose to use.Indeed, this is good practice for you: you should be able to write such functions without much difficulty by this point in the course. In particular, for strings, you may only use string-to-code-points and string-from-code-points. Built-in string functions can be recreated by operating on lists of code points. For lists, you may not use any built-in functions such as length, member, get, split-at, etc. You should be able to write your own versions of these functions using link and empty. However, you do not need to extensively test any functions you create that are the equivalent of built-in functions; you can also keep your purpose statement concise (tell us which built-in its emulating).
You are, however, welcome to use operators on Strings (+, <=, >, ==, etc.).
You can hand in your code by sharing a copy of your work on code.pyret.org, and then submitting the link to your shared copy using the handin script. With your code open, click the share button at the top of the page, and click on “Share a New Copy” to generate a snapshot of your code at the current moment. Choose the shared copy you would like to submit and copy the url of the shared copy. In your terminal, run the command:
cs019-cpo-handin nile <shared copy url>
where <shared copy url> is enclosed in quotation marks. This will send us the link to your code. If you submit more than one shared copy in this manner, only your most recent copy will be graded.