BThe code may stay short, but every letter after the first is still compared.
CEach of the 1,000 new letters is compared, not just one.
DThat would be every letter compared with every other. Each is compared once, with the run's letter.
pen and paper
5question 5about 5 minutes
Run the method by hand on the text xxyyyyz.
Each comparison, and whether the letters match.
The code.
The text read back from the code.
a hint
Write a run only when the next letter differs, or at the end.
a worked answer
The second x matches x. The first y differs, so write 2x. The next three y's match. z differs, so write 4y. At the end, write 1z. That is 6 comparisons, and the code 2x4y1z. Read back, 2x4y1z gives xxyyyyz.
letter
held against
same
comparisons so far
x
-
-
0
x
x
yes
1
y
x
no
2
y
y
yes
3
y
y
yes
4
y
y
yes
5
z
y
no
6
xxyyyyz: the first x starts a run. Each of the other 6 letters is compared once.
One text of 1,000 letters goes x, y, z, x, y, z and so on. Another is 1,000 a's.
How many comparisons does each take?
How many characters does each code have?
a hint
Use questions 1 and 2 of the learn page.
a worked answer
By the learn page's question 1, each makes 999 comparisons, one for each letter after the first. By the learn page's question 2, the first has no letter twice in a row, so its code has 2,000 characters, twice its length. The second is one run, 1000a, 5 characters.
Question 2: how many characters, , does the method write for a text of letters with no letter twice in a row? And why does no text of letters write more?6 of 6 shown
1the move: every run is one letter
the line:
why it is allowed: The first letter starts a run. Each other letter differs from the one before, so it starts a new run, ended by the next letter or the end. xyzx has 4 runs.
2the move: count them
the line:
why it is allowed: By line 1, runs, and each writes 1, then its letter, 2 characters. For xyzx, 1x1y1z1x, 8.
guess it, then
3the move: a longer run
the line:
run
L
writes
characters
b
1
1b
2
aaa
3
3a
2
zzzzzzzzzz
10
10z
3
A run of 1 writes twice its letters. Longer runs write fewer.
why it is allowed: The method writes each run as its length, then its letter. aaa writes 3a, 2 characters; ten z's write 10z, 3, as 10 has two digits.
guess it, then
4the move: put the digits' limit into line 3
the line:
L
digits
characters
twice L
2
1
2
4
9
1
2
18
10
2
3
20
100
3
4
200
From 2 letters up, a run writes at most its letters, and far under twice them.
why it is allowed: 2 to 9 have 1 digit, and 1 is at most less 1. 10 to 99 have 2 digits and are at least 10; 100 to 999 have 3 and are at least 100. Each extra digit makes the smallest 10 times bigger but adds only 1 digit. So from 2 up, the number has at most less 1 digits; line 3 adds 1 letter, so at most .
guess it, then
5the move: each run at most twice its letters
the line:
run
L
writes
twice L
aa
2
2
4
b
1
2
2
ccc
3
2
6
total
6
6
12
aabccc, each run writes at most twice its letters, so the code, 6 characters, is at most twice the text, 12.
why it is allowed: A run of 1 writes 2 by line 3. From 2 up, at most by line 4, under .
guess it, then
6the move: add up the runs
the line:
why it is allowed: By line 5, and the runs' letters add up to , so the code has at most characters. Exactly only when every run is 1 letter, as by line 5 a longer run writes at most , under .
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand