The integer $$(19)_{10}$$ (decimal representation) can be written as $$(10011)_2$$ in the binary numeral system. This allows us to define the clockwise rotation of an integer as the operation that puts the last digit at the front in the binary representation of the integer. If we thus perform a clockwise rotation to the binary number $$(10011)_2$$, we obtain the binary number $$(11001)_2 = (25)_{10}$$.

If a leading zero appears in a binary number after it has been rotated clockwise, it may be omitted. This way, a series of integers can be constructed by repeatedly applying clockwise rotations on the previous integer. This clockwise rotation series ends if the binary representation of the last integer contains no more zeros (because leading zeros are omitted). The clockwise rotation series of the integer 19 is then generated in the following way

$$19$$ $$\stackrel{\text{cw}}{\longrightarrow}$$ $$25$$ $$\stackrel{\text{cw}}{\longrightarrow}$$ $$28$$ $$\stackrel{\text{cw}}{\longrightarrow}$$ $$14$$ $$\stackrel{\text{cw}}{\longrightarrow}$$ $$7$$
$$10011$$ $$11001$$ $$11100$$ $$0\!\!\!\!\!-\!\!1110$$ $$0\!\!\!\!\!-\!\!111$$

The sum of the integers in the clockwise rotation series is referred to as the clockwise rotation sum. The clockwise rotation sum of the integer 19 therefore is computed as $$19 + 25 + 28 + 14 + 7 = 93$$.

The reverse operation, which puts the first digit in the binary representation of an integer at the back, is then logically referred to as the counterclockwise rotation of the integer. As such, the counterclockwise rotation of $$(10011)_2 = (19)_{10}$$ is $$(0\!\!\!\!\!\!-\!\!0\!\!\!\!\!\!-\!\!111)_2 = (7)_{10}$$. Note that in this case two leading zeros are omitted. In analogy to the clockwise rotation operations, the counterclockwise rotation series and the counterclockwise rotation sum can be defined based on the counterclockwise rotation. The counterclockwise rotation sum of 357 is 789, as can be derived from the corresponding counterclockwise rotation series

$$357$$ $$\stackrel{\text{ccw}}{\longrightarrow}$$ $$203$$ $$\stackrel{\text{ccw}}{\longrightarrow}$$ $$151$$ $$\stackrel{\text{ccw}}{\longrightarrow}$$ $$47$$ $$\stackrel{\text{ccw}}{\longrightarrow}$$ $$31$$
$$101100101$$ $$0\!\!\!\!\!-\!\!11001011$$ $$10010111$$ $$0\!\!\!\!\!-\!\!0\!\!\!\!\!-\!\!101111$$ $$0\!\!\!\!\!-\!\!11111$$

Assignment

Write three functions rotation, rotationSeries and rotationSum, that can be used to respectively compute the rotation, the rotation series and the rotation sum for a given integer $$n \in \mathbb{N}_0$$. Each of these functions has a parameter number to which the integer $$n$$ must be passed. In addition, each of the functions also has a parameter clockwise that takes a Boolean value. This Boolean indicates whether clockwise (value True, the default value) or counterclockwise (value False) rotations must be computed. Try to make optimal reuse of your own source code in implementing these three functions.

Tip: examine how the built-in Python functions bin and int can be used to convert the decimal representation of an integer into its binary representation, and vice versa.

Example

>>> rotation(19)
25
>>> rotation(25, clockwise=True)
28
>>> rotation(25, clockwise=False)
19
>>> rotation(19, clockwise=False)
7

>>> rotationSeries(19)
[19, 25, 28, 14, 7]
>>> rotationSeries(69)
[69, 98, 49, 56, 28, 14, 7]
>>> rotationSeries(205, clockwise=True)
[205, 230, 115, 121, 124, 62, 31]
>>> rotationSeries(357, clockwise=False)
[357, 203, 151, 47, 31]
>>> rotationSeries(54321, clockwise=False)
[54321, 43107, 20679, 8591, 799, 575, 127]

>>> rotationSum(19)
93
>>> rotationSum(69)
321
>>> rotationSum(205, clockwise=True)
888
>>> rotationSum(357, clockwise=False)
789
>>> rotationSum(54321, clockwise=False)
128199