Misc 02

  1. Write a function to add up all of the numbers in a list.

     ;; add-all: (Listof Number) -> Number
     (define (add-all nums) 0)
     (check-expect (add-all (list 10 20 30 54)) 114)
    
    1. Write a good sequence of three more related check-expects.
    2. Write the add-all function.
  2. Write a function to find the largest number in a list.

     ;; largest: (Listof Number) -> Number
     (define (largest nums) 0)
     (check-expect (largest (list 30 50 60 40 10)) 60)
    

You might find it helpful to write a helper function that has an extra argument, the largest number that you have seen so far, but that is not required.

  1. You get a table of numbers represented by a list of lists. Find the largest sum of any row.

     ;; big-rowsum : Table -> Number
     (define (big-rowsum num-table) 0)
    

    In the table below, the largest row sum is 7000+8000+9000 = 24000 so that should be the result of big-rowsum.

     1000  2000  3000
     4000
     5000  6000
     7000  8000  9000
     10000
    

    List format:

     (define simple-table-1 (list (list 4000)))
     (check-expect (big-rowsum simpletable) 4000)
    
     (define numtable
       (list (list 1000 2000 3000)
             (list 4000)
             (list 5000 6000)
             (list 7000 8000 9000)
             (list 10000)))
    
     (check-expect (big-rowsum numtable) 24000)
    

4. Real Application

(A bit tricky.) It’s your bad luck that the table of numbers doesn’t actually come in table format. Instead, each number is on its own line, separated by blank lines between groups of numbers. In the file, this looks like:

1000
2000
3000

4000

5000
6000

When input from the file using read-words-and-numbers/line, the data will be in what I call Flat-Table format (each line has either 0 or 1 numbers in a list):

    (list (list 1000)
          (list 2000)
          (list 3000)
          (list)
          (list 4000)
          (list)
          (list 5000)
          (list 6000))

Our goal is to write a function to convert that format to the ordinary Table format, where each inner list represents one row of the table:

(list (list 1000 2000 3000)
      (list 4000)
      (list 5000 6000))

Here are two data files:

Starter code:

    (require 2htdp/batch-io)
    (define RAW (read-words-and-numbers/line "aoc-2022-01-small.txt"))

4.1 Outline

In this section Flat-Table means a list of lists of numbers, with either 0 or 1 number in each list. Here are two examples of flat tables:

(list (list 10) (list 20)) ;; table row 1: 10 20

(list (list 30) (list) (list 40)) ;; table row 1: 30; row 2: 40

Our plan is to write two helper functions:

  1. first-table-row : Flat-Table -> (Listof Numbers). This function will take in the table and output the next row.

     (define small-table-b  (list (list 10) (list 20) (list) (list 30)))
    
     (check-expect (first-table-row small-table-b)
                   (list 10 20))
    
    1. How many rows of data are there in the table that small-table-b represents?

    2. Create a variable small-table-c that represents a table with three elements in its first row and no more rows.

    3. Make a check-expect that shows what happens when you run first-table-row on small-table-c.

    4. Create a variable small-table-d tha represents a table with three rows of data.

    5. Write a check-expect that shows what should happen when you run first-table-row on small-table-d.

    6. Write the function first-table-row.

  2. rest-table-row : Flat-Table -> Table. This function takes in a flat table and puts out a flat table with the first row removed.

     (check-expect (rest-table-row small-table-b)
                   (list (list 30)))
     (check-expect (rest-table-row (list (list 30)))
                   empty)
    
    1. Make a check-expect that shows what happens when you run rest-table-row on small-table-c.

    2. Make a check-expect that shows what happens when you run rest-table-row on small-table-d.

    3. Write function rest-table-row.

4.2 Finish

The aoc-table function takes in a flat table and puts out a table. Write the aoc-table function.

    (check-expect (aoc-table small-table-b)
                  (list (list 10 20) (list 30)))

    (define (aoc-table raw) empty)
    (check-expect (aoc-table RAW) numtable)
Last modified October 4, 2024: Planned deletion of "original" version. (0ea538e)