CSci 1301: Takehome 2

Due: Thursday, November 22nd at 11:59pm by e-mail (no late work accepted)

Total: 35 points

This is individual work. You are not allowed to spend more than 8 hours on this test. This includes any reading that you do to help you solve the problems.

Make sure to write your name in the file(s) you submit and also name the files with your last name (and initial if needed). You may submit one or two files.

Use helper functions as needed. All your functions (including all those you use in big-bang) must have documentation with the types of parameters and results, a function description, and, whenever possible, check-expect tests.
When defining your own structures, clearly explain what all the fields mean.
Some problems provide test cases, but you need to add your own to test thoroughly.
Comments, helpful variable names, and good code style and formatting are a very important part of your grade. You don't have to look for the shortest solution, but you should eliminate unnecessary code repetition if possible (use helper functions).

Task 1 (5 points)

Write a function all-smaller? that takes two lists of numbers and returns #true if each element of the first list is smaller than the corresponding element of the second list (so the first element of the first list is smaller than the first element of the second list, etc.). If one of the lists is shorter than the other one, the extra elements aren't considered. . The function must pass the following check-expects:


(check-expect (all-smaller? (list 1 6 0) (list 2 8 1)) #true)
(check-expect (all-smaller? (list 1 6 0) (list 2 8 1 -5)) #true)
(check-expect (all-smaller? (list 1 6 0) (list 2 5 1)) #false)
(check-expect (all-smaller? (list 1 6 0) (list 2 6 1)) #false)
  

Task 2 (5 points)

Write a function drop that takes a list and a number n and returns the list without the first n elements. If a negative number is passed to it, the function produces an error. The check-expects below detail the function further:


(check-expect (drop 2 (list 1 2 3 4 5)) (list 3 4 5))
(check-expect (drop 0 (list 1 2 3 4 5)) (list 1 2 3 4 5))
(check-expect (drop 5 empty) empty)
(check-expect (drop 10 (list 1 2 3 4 5)) empty)
(check-error (drop -1 (list 1 2 3 4 5)) "cannot drop a negative number of elements")

Task 3 (25 points): structures and a world problem

Your task is to write some functions and a world in which the world state is a list of structures representing events. For simplicity the events start and end on the exact hour (so we can't have something like 10:30) and we use military time (10pm is represented as 22, midnight is 0). The structures are defined as follows:


    (define-struct event [name start end])
    ;; Interpretation:
    ;; name is a string: the name of the event
    ;; start is an integer between 0 and 23 (inclusive): the start hour of the event
    ;; end is an integer between 0 and 23 (inclusive): the end hour of the event
    ;; Example:
    (make-event "Meeting" 10 11)

No events extend past midnight.

Your task is to write several functions for lists of event structures. Some of them include world programs, and some don't. The functions that you need to write are:

  1. Given a list of events, find their total duration (in hours). Add the durations even if some events overlap.
  2. In a list of events, find and return the latest ending event. If two events end at the same latest time, either one of them can be returned. Assume that the given list is non-empty.
  3. Write a function to sort events in increasing order by the starting time.
  4. Given a list of events and another event, return #true if the given event overlaps with at least one of the events on the list, and #false otherwise. Two events overlap if the starting hour of one is smaller than the ending hours of another (say, one is 8 to 10, and the other one is 9 to 10). If the starting hour of one is exactly the starting hour of another one, they do not overlap (example: 8 to 10 and 10 to 11).
  5. Write a function that, given two lists of events, returns #true if any event on the first list overlaps with any event on the second list, and #false otherwise.
  6. Write a world program that uses a list of non-overlapping events as its states and displays them vertically as rectangles in a calendar-style, starting at 6am and ending at midnight with the name of the event. For a list
    
    	(list (make-event "Meeting" 10 11) (make-event "Lecture" 13 15) (make-event "Movie" 21 23))
        
    the result would look like this:

    The width and the height of the scene can be changed, and the schedule must adjust with it (although you may use fixed font size for the strings; it's ok if they become too big or too small when the scene dimensions are changed). The one shown above is 500 by 300.
  7. Add a functionality that when a number key is pressed, the event with that number is removed: if 1 is pressed, the first (earliest) event is removed, and so on. If the key pressed is larger than the number of shown events, nothing is removed. We assume that we cannot have more than 9 events at a time.
    Note that next time the key is pressed, the counting starts at 1 for the new world state.
  • Extra credit, 5 points (only after everything else is finished): Add the gridlines to the events list indicating the time labels in US style from 6am to 12am (midnight), starting at 6am and going by the hour. To get credit, your solution must be recursive, not just directly adding the gridlines to the image one-by-one.
  • If something is not working, please comment it out and write comments about why you think it's not working and how you would be going about fixing it if you had more time. Make sure that the code you submit runs, even if it doesn't implement all of the functionality.

    Your work will be graded based on:

    Happy programming!

    What to submit

    Submit all your files via Canvas. Also please include the total time you spent on the test in a submission comment.


    CSci 1301 course web site.