Course / The terminal and the language
Tidy the username
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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// TODOcomment and areturnline you will change. RawandFileare variables outside the functions. The tests give them different values, so read them each time the functions run.Usernamedoes 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-.IsCSVis true only when the file name ends with.csv, ignoring upper and lower case..CSVand.Csvcount.- Use the
stringspackage. You will needimport "strings"at the top of the file. - Do not change the lines
var Raw string,var File stringor thefunclines.
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
- The
stringspackage 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. - For
Username, trim first, then lower-case, then replace each space with a hyphen. ForIsCSV, lower-case the file name first so.CSVand.csvlook the same, then ask whether it ends with.csv. - Username:
return strings.ReplaceAll(strings.ToLower(strings.TrimSpace(Raw)), " ", "-"). IsCSV:return strings.HasSuffix(strings.ToLower(File), ".csv"). Addimport "strings"under the package line.