Lesson 8 of 34 · Fundamentals
Boss: The Ember Sentinel's Rune Code
The Ember Sentinel blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
At the heart of the Sharpforge Citadel burns the Great Forge, and before it stands the Ember Sentinel — a towering guardian of iron and flame. It speaks only in rune code, a compact script carved into its armour. Decode its language and encode your own, and the Sentinel will let you pass into the Citadel proper.
Battle plan
Rune code is run-length encoding: every run of repeated characters is written as the character followed by how many times it repeats.
| Text | Rune code |
|---|---|
aaabcc |
a3b1c2 |
Hello |
H1e1l2o1 |
zzzzzzzzzzzz (12 × z) |
z12 |
Upper and lower case letters are different characters, and the encoded text never contains digits as characters — every digit belongs to a count, and a count can have several digits.
This fight uses everything from Chapter 1: indexing into strings, char comparisons, loops, if/else and a little arithmetic.
Turning a digit character into its number is a classic trick: characters are stored as numbers, and '0' to '9' are consecutive, so '7' - '0' is 7. To read a multi-digit number one character at a time, multiply what you have so far by 10 and add the new digit: reading '1' then '2' gives 1, then 1 * 10 + 2 = 12. char.IsDigit(c) tells you whether a character is a digit.
Assignment
Complete three methods in Program:
Compress(text)returns the rune code oftext. An empty string compresses to"".Expand(code)reverses it:Expand("a3b1c2")returns"aaabcc"andExpand("x12")returns twelvexs. An empty code expands to"". You can assume the code is valid.Shorter(text)returns the compressed text if it is strictly shorter than the original, otherwise the original:Shorter("aaaaaab")is"a6b1"butShorter("abc")is"abc".
Keep learning
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .