Following a finished project teaches you how its pieces fit. Building a smaller one yourself, with slightly different rules, shows whether you can use those pieces without a guide. This exercise is an activity log rather than a reading log. The input format is the same, title,minutes, but the valid range is 1 to 60 minutes and the input comes from a slice instead of standard input, so you can focus on the rules.
How it differs from the reading log
- The input is a slice of strings. You loop over it with
rangeinstead of a scanner, so there are no line numbers and noscanner.Err()check. - The range is 1 to 60. A ride of 0 minutes and a walk of 61 minutes are both invalid.
- Rejected lines are printed as they are, quoted with
%qafterInvalid:, instead of with a message for each rule. - The report is one line: the number of accepted activities and their total minutes.
Requirements
- Write
parseActivity(line string) (int, error). It returns the minutes for a valid line, or an error for a line with no comma, a blank title, minutes that are not a whole number, or minutes outside 1 to 60. Spaces around the title or the minutes are allowed. - Write
report(lines []string). It printsInvalid:and the quoted line for each rejected line, in input order, then one summary line in exactly the formatActivities: 3 | Minutes: 100. - Call
reportthree times frommain: once with the sample, once with an empty slice and once with the extra cases below.
No plan is given this time. Deciding the order of the checks is part of the exercise.
Your turn
Build the activity log described above. The sample is "Walk,10", "Ride,0", "Stretch,30", "Walk,61" and "Ride,60". The extra cases are "Ride", which has no comma, " ,20", whose title is blank, and "Stretch, 15 ", which has spaces around its minutes. Your program is done when it prints exactly this:
Invalid: "Ride,0"
Invalid: "Walk,61"
Activities: 3 | Minutes: 100
Activities: 0 | Minutes: 0
Invalid: "Ride"
Invalid: " ,20"
Activities: 1 | Minutes: 15package main
func main() {
// Write parseActivity and report, then call report three times.
}Hint 1: the idea
parseActivity holds every rule and returns at the first one a line breaks, the way parse did on Day 6. report never looks inside a line. It changes its count and total only when parseActivity returns a nil error.
Hint 2: a first step
Write report first, with a parseActivity that returns 0, nil for every line. The sample should print Activities: 5 | Minutes: 0. Then add the rules to parseActivity one at a time and watch the summary change.
Worked solution
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
func parseActivity(line string) (int, error) {
title, raw, found := strings.Cut(line, ",")
if !found {
return 0, errors.New("use title,minutes")
}
if strings.TrimSpace(title) == "" {
return 0, errors.New("title is required")
}
minutes, err := strconv.Atoi(strings.TrimSpace(raw))
if err != nil || minutes < 1 || minutes > 60 {
return 0, errors.New("use 1-60 whole minutes")
}
return minutes, nil
}
func report(lines []string) {
accepted := 0
total := 0
for _, line := range lines {
minutes, err := parseActivity(line)
if err != nil {
fmt.Printf("Invalid: %q\n", line)
continue
}
accepted++
total += minutes
}
fmt.Printf("Activities: %d | Minutes: %d\n", accepted, total)
}
func main() {
report([]string{"Walk,10", "Ride,0", "Stretch,30", "Walk,61", "Ride,60"})
report([]string{})
report([]string{"Ride", " ,20", "Stretch, 15 "})
}Invalid: "Ride,0"
Invalid: "Walk,61"
Activities: 3 | Minutes: 100
Activities: 0 | Minutes: 0
Invalid: "Ride"
Invalid: " ,20"
Activities: 1 | Minutes: 15report changes its counters only after a nil error, so a rejected line can never leak into the total. The empty slice runs the loop zero times and still prints a summary, because the summary is outside the loop. %q shows the leading space in " ,20", which a plain Println would hide.
Check your own work
Before you open the solution, or after, go through this list. Each item is a mistake that the sample alone would not catch.
- Boundaries: change the sample's
"Ride,60"to"Ride,61", and its"Walk,10"to"Walk,1". The first should be rejected and the second accepted. - The error comes before the range check:
"Walk,ten"is rejected, andminutesis never used whenAtoifailed. - Spaces are trimmed, not rejected:
" Walk , 20 "is accepted with 20 minutes. - Empty input: the summary still prints, with zeros.
- One place for the rules:
reportcontains no comparison with 1 or 60. If it does, a rule has leaked out ofparseActivity.
Going further
Once your version works, take it further:
- Print the error message for each rule instead of the line alone, for example
"Ride,0": use 1-60 whole minutes. - Write a table check for
parseActivitywith rows on and just outside both boundaries, then break the upper bound on purpose and confirm a row fails.
Each step is something you have already done once this week. Doing it again in a new setting, without the original in front of you, is what turns a pattern you have seen into one you can use.
Completion is saved in this browser only. It does not measure mastery.