Assignment 2: Box Office Trivia


The goal of this assignment is to produce O'Caml code that will help you answer a set of questions about the top-grossing films in movie history. To get started, download the tarball located here. Unzip and untar the code and data by typing the following commands at a unix prompt.

$ tar xfz a2.tgz

You should find that the tarball contains the following items.

  • This file contains some warm-up exercises you must do.
  • This file is mostly blank. You will write a set of functions to analyze lists of movies. After the warm-up, this is the file you will focus on primarily.
  • This file contains the driver for the program. Don't modify this file (unless you are trying to improve your Karma), but you can take a look to see how it's organized. It uses the module Arg to parse command line arguments in a nice way.
  • This utility file contains a set of useful routines for parsing and printing movie files. Don't modify it (unless you are trying to improve your Karma), but feel free to take a look to see how it's organized. If you'd like to parse and print other simple text files using O'Caml, this code could be a starting point.
  • README.txt: Report your answers to the movie trivia questions in this file. Also, explain any unusual design decisions or problems you had here. Suggestions for improving this assignment are also welcome.
  • Makefile: Type make all to compile your code and create an executable you can run to help you answer the key boxoffice trivia questions.
  • data: This directory contains the data you need to answer the questions asked on this assignment. The data was downloaded in May 2012, so it will be slightly out of date --- films like the The Hunger Games will have continued to rack up the dollars over the last few months. That's ok. In order to make it easy to verify your answers, you are required to use the data we provide as opposed to more recent data that you download from other sites. There is one file for each movie rating category (G, PG, PG-13, R) and one file for all-time inflation-adjusted gross movies. There are also a couple of small files you can use for testing. You will find that the data in the files comes in an arbitrary order.

Task 1: Warm-up on Lists

Examine the contents of the file and answer the questions found therein. This file must type check and compile when you hand it in.

Task 2: Box Office Analysis in O'Caml

The file is missing a bunch of functions that you need to code. See the file for details. Your goal should be to focus first on this file independently of any of the other files. Use the O'Caml top-level environment to fully debug all functions you create first before proceeding with the second part of the assignment. You will use these functions to query just a few data files. However, we will test your functions thoroughly when grading them (and you should test them thoroughly when writing them). They should operate correctly on all possible inputs. At the bottom of the file there is one bit of sample data data4 and one unit test test1. You must create many more such tests to debug your code.

Since the data files you are working with are relatively small, you should not overly concern yourself with the efficiency of your code. However, none of your functions should be more than quadratic in complexity. Your main goals are correctness, clarity and good style. Be sure to refer to our style guide.

For this assignment, you should not use functions from the List module. You should code these functions "from first principles" using recursion. Do not use functions from O'Caml's List module to help you implement these functions.

We suggest you implement them in the following order.

  • take -- return only the first n elements of a movie list
  • drop -- return everything but the first n elements of a movie list
  • average -- return the average gross of all movies
  • decade -- return all movies produced in that decade
  • sort -- a polymorphic (selection) sort function
  • sort_by_gross -- sort by gross revenue
  • sort_by_year -- sort by year produced
  • by_studio -- return total gross from all movies produced per studio
  • sort_by_studio -- sort list of studio-gross revenue pairs by gross revenue

Task 3: Answering the Box Office Trivia Questions

Scripting is a kind of functional programming: Scripts take data files (often representing lists) as inputs and produce new data files as outputs. Like functions in a functional program, scripts often compose: you can pipe the output on stdout of one script into the input on stdin of another script.

When you have finished coding and thoroughly testing the functions in, compile the entire application by typing "make" at a shell prompt in your code directory. To find out what you can do with your script, type the following at a shell prompt.

./boxoffice -help

You should see a list of options you can use. As a simple sanity check to make sure things are working properly, type the following:

./boxoffice -echo < data/trial1.txt

The above command should send the contents of the trial1.txt data file out on to standard output. We also included that test as a part of your makefile so you can also type the following to check your setup.

make check
Another command you might try is this one:
./boxoffice -take 1 < data/G.txt
What does it do? Recall that the pipe operator (vertical bar) allows you to send the output of one command in to the input of another command. With that in mind, what does the following do?
./boxoffice -sort-gross < data/G.txt | ./boxoffice -take 1

Now, take a look inside the makefile. You will see the clause for compiling boxoffice at the top. At the bottom, you'll see the clause for "topG". If you type:

make topG
you'll see the same thing. Feel free to add your own commands to the file.

To Do: Use your script to answer the questions about boxoffice trivia posed in the README.txt file. In addition to reporting the answers to the questions, report the scripting commands you used to find the answers. Try to make the script do as much work as you can. If possible, use a series of calls to your script to produce only the data you need to answer the question and no more. (This may not be possible.)

Karma (Optional)

Recall, you are not required to do Karma questions. They will have little if any impact on your grade. They are mostly for fun and a little extra challenge. Be sure to do the other questions first and ensure they are correct. Be doubly sure not to screw up your code or answers as you do the Karma questions.

  • In, write a function that takes a list and generates a list of lists. The list of lists should contain all permutations of the original list. Such a function could be useful for debugging in certain circumstances. In other situations, finding all permutations of a list can be useful if you need to search for an answer to a problem.
  • In, write a function to scramble a list. For extra Karma, do the scrambling in less than O(N^2) time. One could do this by converting from a list to an array and then using the algorithm on arrays given in COS 226 and then converting back to a list. To do that you'd have to learn how to use O'Caml arrays, which you could do -- or you could wait until later in this course. Alternatively, for fun, and for just as much Karma, think of an interesting way to do the scrambling using only lists in worst or average case N log N time.
  • What one thing would you do to generalize the script so that it is more flexible or useful or could answer more questions easily? Implement your suggestion and explain briefly. However, do not do so in a way that changes your answers to previous questions. We will judge your answers to other parts of the assignment independently of whether or not you have implemented this Karma question. You made add functionality (and extra options to the script) but you may not change existing functionality.

Handin Instructions

This problem set is to be done individually.

You must hand in these files to dropbox:

  1. -- this file contains the warm-up exercise
  2. -- this file contains the bulk of your solution
  3. README.txt -- this file contains written answers
  4. -- this file does not need to be modified at all unless you did the Karma question
  5. -- this file does not need to be modified at all unless you did the Karma question

Be sure to include your name and netid at the top of every file.

Please make sure you submit your solutions, not the blank stubs you downloaded.

Important notes about grading:

  1. Compile errors: All programs that you submit must type check and compile. Programs that do not compile will likely receive an automatic zero. If you are having trouble getting your assignment to compile, please visit office hours. If you run out of time, it is better to comment out the parts that do not compile and hand in a file that compiles, rather than handing in a more complete file that does not compile.
  2. Missing functions: We will be using an automatic grading script, so it is crucial that you name your functions and order their arguments according to the problem set instructions, and that you place the functions in the correct files. Otherwise you may not receive credit for a function properly written.
  3. Code style: Finally, please pay attention to style. Refer to the O'Caml style guide and lecture notes. Ugly code that is functionally correct may still lose points. Take the extra time to think through the problems and find the most elegant solutions before coding them up. Good programming style is also required on all the subsequent assignments.