Course outline
Course / The terminal and the language

Cut the code

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-03-cut-the-code
cd 09-03-cut-the-code
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 three functions that cut pieces out of the product code in Code: its first three bytes, its last character, and the code with its first and last bytes removed.

Example

For Code = "GO-7421":

Code = "GO-7421"
fmt.Println(Prefix())      // GO-
fmt.Println(LastChar())    // 1
fmt.Println(WithoutEnds()) // O-742

Prefix is positions 0, 1 and 2. LastChar is the byte at position len(Code)-1, turned into a string. WithoutEnds starts at position 1 and stops just before the last position.

Rules

  • Edit cut.go. Each function has a // TODO comment and a return line you will change.
  • Code is a variable outside the functions. The tests give it different values, so read Code each time the functions run.
  • Use indexing and slicing (Code[...]) to cut the pieces. The codes are plain ASCII and always at least three bytes long.
  • LastChar must return a string, not a number. Remember that indexing gives a byte, which prints as a number until you convert it.
  • Do not change the line var Code string or the func lines. Do not add imports.

What you'll practice

Zero-based positions, len(s)-1 for the last position, and s[start:end] where the end is not included.

Run it

gonomicon fetch week-1/09-03-cut-the-code
cd 09-03-cut-the-code
gonomicon test
gonomicon submit

Hints

  1. Positions start at 0, and s[start:end] stops just before end. The first three bytes are positions 0, 1 and 2.
  2. Prefix is Code[:3]. The last position is len(Code)-1. Dropping the first and last byte means starting at 1 and ending at len(Code)-1.
  3. Prefix: return Code[:3]. LastChar: return string(Code[len(Code)-1]). WithoutEnds: return Code[1 : len(Code)-1].

Something wrong with this lesson? Report a problem