Course / The terminal and the language
Cut the code
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-03-cut-the-code
cd 09-03-cut-the-code
gonomicon submitGoal
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// TODOcomment and areturnline you will change. Codeis a variable outside the functions. The tests give it different values, so readCodeeach 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. LastCharmust 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 stringor thefunclines. 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
- Positions start at 0, and
s[start:end]stops just beforeend. The first three bytes are positions 0, 1 and 2. - Prefix is
Code[:3]. The last position islen(Code)-1. Dropping the first and last byte means starting at 1 and ending atlen(Code)-1. - Prefix:
return Code[:3]. LastChar:return string(Code[len(Code)-1]). WithoutEnds:return Code[1 : len(Code)-1].