Course / The terminal and the language
Count the charms
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-02-count-the-symbols
cd 09-02-count-the-symbols
gonomicon submitGoal
Fill in two functions: RuneCount returns how many symbols Word holds, and ExtraBytes returns how many more bytes than symbols it takes up.
Example
For Word = "héllo", there are 5 symbols but 6 bytes, because é takes two bytes.
Word = "héllo"
fmt.Println(RuneCount()) // 5
fmt.Println(ExtraBytes()) // 1
For plain English text such as "hello", both counts match, so ExtraBytes() is 0.
Rules
- Edit
symbols.go. Each function has a// TODOcomment and areturnline you will change. Wordis a variable outside the functions. The tests give it different values, so readWordeach time the functions run.- Count symbols with
utf8.RuneCountInString. You will needimport "unicode/utf8"at the top of the file. ExtraBytesis the number of bytes minus the number of symbols. It is never negative.- Do not change the line
var Word stringor thefunclines.
What you'll practice
Telling bytes apart from symbols: len counts bytes, and utf8.RuneCountInString counts runes.
Run it
gonomicon fetch week-1/09-02-count-the-symbols
cd 09-02-count-the-symbols
gonomicon test
gonomicon submit
Hints
- Two numbers are in play: how many bytes (beads) and how many symbols (charms).
lengives one, andutf8.RuneCountInStringgives the other. RuneCountshould returnutf8.RuneCountInString(Word). Addimport "unicode/utf8"under thepackageline.ExtraBytesis the bytes minus the symbols.- RuneCount:
return utf8.RuneCountInString(Word). ExtraBytes:return len(Word) - utf8.RuneCountInString(Word).