Check your understanding of the previous reading: rotating characters and strings with the rotation formula.

Question 1

Rotate all of the following characters by the given amount, to determine a new character. Do not put quotes around the resulting character.

A by 1:

Show worked solution

A is 65: (65 + 1 - 32) % 94 + 32 is 66, and chr(66) is B.

A by 2:

Show worked solution

A is 65: (65 + 2 - 32) % 94 + 32 is 67, and chr(67) is C.

A by 10:

Show worked solution

A is 65: (65 + 10 - 32) % 94 + 32 is 75, and chr(75) is K.

A by -1:

Show worked solution

A is 65: (65 - 1 - 32) % 94 + 32 is 64, and chr(64) is @. Rotating backwards from A gives the character just before it.

B by 2:

Show worked solution

B is 66: (66 + 2 - 32) % 94 + 32 is 68, and chr(68) is D.

B by 25:

Show worked solution

B is 66: (66 + 25 - 32) % 94 + 32 is 91, and chr(91) is [. 91 is the character right after Z (90).

= by -1:

Show worked solution

= is 61: (61 - 1 - 32) % 94 + 32 is 60, and chr(60) is <.

a by 1:

Show worked solution

a is 97: (97 + 1 - 32) % 94 + 32 is 98, and chr(98) is b.

z by 5:

Show worked solution

z is 122: (122 + 5 - 32) % 94 + 32 is 33, and chr(33) is !. 122 + 5 - 32 is 95, past the end of the wheel, so the modulo wraps it around to 1: the wheel starts again at the space (32).

z by 8:

Show worked solution

z is 122: (122 + 8 - 32) % 94 + 32 is 36, and chr(36) is $. The modulo wraps the wheel around past ~, back to the start.

2 by -30:

Show worked solution

2 is 50: (50 - 30 - 32) % 94 + 32 is 114, and chr(114) is r. 50 - 30 - 32 is -12, and in Python -12 % 94 is 82 (the result of % is never negative here), so the result is 82 + 32.

Question 2

Which of the following is the result of rotating the string Hello by 29?

Select one answer, then check.

  • Not quite. That is Hello rotated by 30, one too far, with the characters in reverse order.
  • Not quite. That is Hello rotated backwards, by -29, with the characters in reverse order.
  • Not quite. These are the right characters, but in reverse order: rotating keeps every character in its place.
  • Not quite. That is Hello rotated by 28, one short.
  • Correct. H (72) becomes 101, e; e (101) wraps around to 36, $; each l (108) wraps to 43, +; and o (111) wraps to 46, ..
  • Not quite. That is Hello rotated by 1 (Ifmmp), in reverse order.
  • Not quite. That is Hello rotated by 30, one too far.

Question 3

What is the result of rotating the string Dragons! by 10? Do not put quotation marks around your answer, just enter the characters.

Show worked solution

Rotate every character by 10: D (68) becomes 78, N, r (114) becomes 124, |, a (97) becomes 107, k, g (103) becomes 113, q, o (111) becomes 121, y, n (110) becomes 120, x, s (115) becomes 125, }, ! (33) becomes 43, +. No code goes past the end of the wheel, so nothing wraps around. Rotating adds no tildes: that is a separate step of encrypt_text.