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)

Original; to delete

You should begin by writing a helper function that has an extra input, the numbers already seen for the current line.

    ;; aoc-help : (Listof (Listof Number))
    ;;            (Listof Number)
    ;;            (listof (Listof Number))
    ;;             -> (Listof (Listof Number))
    (define (aoc-help nums current-group final-answer) empty)
  1. What is the result of (append (list 10 20) (list 30 40 50))?

  2. When read in from a file, what will the following data look like in Racket?

    20
    30
    50
    
    80
    
  3. Suppose that you have already seen the numbers 20 and 30 as part of the first group, what data from the previous part remains? (In Racket terms, like your answer to the above.)

  4. Suppose that you have already seen the numbers 20 and 30 as part of the first group and your final answer is currently empty. Write a check expect showing a call to aoc-help and the result.

  5. Suppose that a call to your helper function has:

    • the nums list is empty,
    • the current-group input is (list 80), and
    • the final-anwer input is (list (list 20 30 50)). Write a check-expect.
  6. When your helper function has seen (list 80 130 210) and encounters (list) as the next item, what is it supposed to do next?

  7. When your helper function has seen (list 340 550) and encounters (list 790), what is it supposed to do next?

  8. Write the helper function aoc-help.

  9. Write the function aoc-table.

Last modified April 26, 2024: Miscellaneous projects. (fd2412a)