Course / The terminal and the language
Fill in the form letter
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-05-build-the-summary
cd 09-05-build-the-summary
gonomicon submitGoal
Fill in two functions that build text with fmt.Sprintf: Summary reports an average with two decimals, and Badge reports how many symbols and bytes a name has.
Example
Name, Total, Count = "Ada", 10, 3
fmt.Println(Summary()) // Ada: 3 items, average 3.33
fmt.Println(Badge()) // "Ada" (3 symbols, 3 bytes)
Name = "héllo"
fmt.Println(Badge()) // "héllo" (5 symbols, 6 bytes)
10 divided by 3 is 3.333..., and %.2f rounds it to 3.33. In Badge, the name is wrapped in double quotes, and the symbol count and byte count differ for héllo.
Rules
- Edit
summary.go. Each function has a// TODOcomment and areturnline you will change. Name,TotalandCountare variables outside the functions. The tests give them different values, so read them each time the functions run.Summaryreturns exactly<Name>: <Count> items, average <Total divided by Count, with 2 decimals>. The average must keep its decimals, so convert both numbers tofloat64before dividing.Countis never zero.Badgereturns exactly<Name in double quotes> (<symbols> symbols, <bytes> bytes). Use%qfor the quotes.- Build both results with
fmt.Sprintf. You will needimport "fmt"andimport "unicode/utf8"at the top of the file. - Do not change the three
varlines or thefunclines.
What you'll practice
Filling a template with Sprintf verbs (%s, %d, %.2f, %q), converting between number types before dividing, and counting symbols against bytes.
Run it
gonomicon fetch week-1/09-05-build-the-summary
cd 09-05-build-the-summary
gonomicon test
gonomicon submit
Hints
Sprintfbuilds the text from a template.%sfills in a string,%da whole number,%.2fa decimal with two digits, and%qa string in double quotes.- The average needs decimals, so convert both numbers first:
float64(Total) / float64(Count). ForBadge, count symbols withutf8.RuneCountInString(Name)and bytes withlen(Name). - Summary:
fmt.Sprintf("%s: %d items, average %.2f", Name, Count, float64(Total)/float64(Count)). Badge:fmt.Sprintf("%q (%d symbols, %d bytes)", Name, utf8.RuneCountInString(Name), len(Name)).