Course outline
Course / The terminal and the language

Count the charms

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-02-count-the-symbols
cd 09-02-count-the-symbols
gonomicon submit

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

Goal

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 // TODO comment and a return line you will change.
  • Word is a variable outside the functions. The tests give it different values, so read Word each time the functions run.
  • Count symbols with utf8.RuneCountInString. You will need import "unicode/utf8" at the top of the file.
  • ExtraBytes is the number of bytes minus the number of symbols. It is never negative.
  • Do not change the line var Word string or the func lines.

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

  1. Two numbers are in play: how many bytes (beads) and how many symbols (charms). len gives one, and utf8.RuneCountInString gives the other.
  2. RuneCount should return utf8.RuneCountInString(Word). Add import "unicode/utf8" under the package line. ExtraBytes is the bytes minus the symbols.
  3. RuneCount: return utf8.RuneCountInString(Word). ExtraBytes: return len(Word) - utf8.RuneCountInString(Word).

Something wrong with this lesson? Report a problem