Course outline
Course / The terminal and the language

Tidy the username

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/09-04-tidy-the-username
cd 09-04-tidy-the-username
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

Use the strings package to tidy a typed username in Username, and to tell in IsCSV whether a file name ends in .csv, whatever the capital letters.

Example

Raw = "  Ada Lovelace "
fmt.Println(Username()) // ada-lovelace

File = "REPORT.CSV"
fmt.Println(IsCSV()) // true

File = "csv.txt"
fmt.Println(IsCSV()) // false

The spaces at the ends are removed, the letters become lower case, and the space in the middle becomes a hyphen. csv.txt has csv in it, but it does not end with .csv.

Rules

  • Edit tidy.go. Each function has a // TODO comment and a return line you will change.
  • Raw and File are variables outside the functions. The tests give them different values, so read them each time the functions run.
  • Username does three things, in this order: remove spaces, tabs and new lines from both ends, make everything lower case, then turn every remaining space into one -.
  • IsCSV is true only when the file name ends with .csv, ignoring upper and lower case. .CSV and .Csv count.
  • Use the strings package. You will need import "strings" at the top of the file.
  • Do not change the lines var Raw string, var File string or the func lines.

What you'll practice

Choosing the right tool from the strings package: TrimSpace, ToLower, ReplaceAll and HasSuffix, and combining them.

Run it

gonomicon fetch week-1/09-04-tidy-the-username
cd 09-04-tidy-the-username
gonomicon test
gonomicon submit

Hints

  1. The strings package has a tool for each job: removing spaces from the ends, making lower case, swapping one piece for another, and checking how a string ends.
  2. For Username, trim first, then lower-case, then replace each space with a hyphen. For IsCSV, lower-case the file name first so .CSV and .csv look the same, then ask whether it ends with .csv.
  3. Username: return strings.ReplaceAll(strings.ToLower(strings.TrimSpace(Raw)), " ", "-"). IsCSV: return strings.HasSuffix(strings.ToLower(File), ".csv"). Add import "strings" under the package line.

Something wrong with this lesson? Report a problem