Assignment 2: Box Office Trivia


The goal of this assignment is to produce OCaml 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.

  • .merlin: This file contains directives for Merlin to find your source code and additional files after you have compiled.
  • This file contains list-processing problems for you to complete as part 1 of the assignment.
  • This file is mostly blank. You will write a set of functions to analyze lists of movies as part 2 of the assignment.
  • This file contains the driver for the program. Don't modify this file, 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, 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 OCaml, 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 and processed in May 2012, so it will not be up to date -- films like the The Hunger Games will have continued to rack up the dollars over the last few years, and new movies will have been released. That's okay. 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.

Part 1: Lists

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

Part 2: Box Office Analysis in OCaml

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. 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 some sample infrastructure for unit tests that you may use as a model for your own testing.

Since the data files you are working with are relatively small, you should not overly concern yourself with the efficiency of your code -- though needlessly or egregiously inefficient solutions should be avoided. Your main goals are correctness, clarity and good style. Be sure to refer to our style guide.

We suggest you implement the functions in part 2 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

Part 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.)

Handin Instructions

This problem set is to be done individually.

You must hand in these files to dropbox:

  1. -- this file contains the list processing problems
  2. -- this file contains the movie processing functions
  3. README.txt -- this file contains written answers

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

Important notes about grading:

  1. Rec: For this assignment, you may add the rec keyword to any of the let statements that define functions if you would like the function to call itself. And in the other direction, a function for which we have given you a let rec needn't call itself, for example, if you have defined a recursive auxiliary function to use instead.
  2. Modules:For this assignment, you should not use functions from the List module, nor the built-in @ operator. You should code these functions "from first principles" using recursion. Do not use functions from any module as a reference to help you re-implement these functions.
  3. Compile errors: All programs that you submit must type check and compile. Programs that do not compile will be subject to a penalty in addition to the deduction for the bug that causes the failure to compile. 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.
  4. 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 working but misnamed function.
  5. Code style: Finally, please pay attention to style. Refer to the OCaml style guide and lecture notes. Ugly code is not fun to debug, nor to grade. 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.