Strings and output
Str and Char, building text, interpolation and print options.
Str is a sequence of ASCII bytes indexed by 0..s.len; Char is one
byte. There is no Unicode decoding behind a subscript, so s[i] is
O(1) and is exactly the i-th character of the input.
Reading and indexing
read() into a Str reads one whitespace-separated token.
Press Run (Ctrl+Enter).
A character minus 'a' is its position in the alphabet, and
'a' + k goes back:
Press Run (Ctrl+Enter).
Building text
A var string is a buffer. push appends a character, += appends a
string, and "{...}" puts any value into text:
Press Run (Ctrl+Enter).
Integers have no .to_str(); write "{n}".
print puts a space between its arguments and ends the line. Its named
arguments cover the usual output shapes:
Press Run (Ctrl+Enter).
- A list or array prints on one line, separated by spaces.
sep:andend:change the separators;none:says how to print a missing value.yes(b)printsYESorNO;printdoes not take a bareBool.- Format specs follow Python:
:.2f,:03.
Output is buffered and flushed at the end, so printing 2·10⁵ lines is fast without any setup.
Problem: Run length
Replace each maximal run of equal letters in a word by the letter and the run's length: aaabcc becomes a3b1c2.
Hints
out.push(c) and out += "{len}" build the answer in one pass.
More like this: Mirror word. Next: Sorting and searching.
Lints for these patterns
cpl lint, the editor and the playground flag the hand-written versions of what this chapter teaches:
bool_ternary_print: Answer words written outhand_rolled_string_algo: Hand-rolled string algorithmpush_loop: Loop that only pushes