Write some code to do the following:
- Take as its input a signed integer in either signed decimal or signed octal (explained below)
- If the number is decimal convert it to octal. If it's octal, convert it to decimal
- Output the number!
Instead of starting with a
0, negative octal numbers will start with a
Z (capital zed).
In the same way that -1 is commonly encoded in binary as all
1s, in signed octal it is encoded as all
7s. -2 is all
7s except for the final digit which is
6, and so on.
Your code must accept leading
7s (after the initial
Z in octal) but must not output leading
0s for decimal or non-negative octal, with the exception of a single
0 to indicate octal format, and must not output leading
7s in the case of negative octal.
0 is valid as an entire number in both decimal and octal and is the only number to have an identical representation in both. Decimal numbers must not have any leading zeros and must be interpreted as octal if they do (i.e.
08 should be interpreted as neither decimal nor octal). Note that
0s immediately after the
Z are significant and must not be truncated.
Z is shorthand for an infinite number of
7s. You may assume that the input will always be valid and not check for it in your code.
Input : Output -1 : Z -10: Z66 -5: Z3 10: 012 -30000: Z05320 -10000: Z54360 0 : 0 01234: 668 Z1234: -3428 Z77777: -1 00777: 511 Z6000: -1024
A table of the first few positive and negative signed octal numbers:
+ve -ve 0 0 01 Z 02 Z6 03 Z5 04 Z4 05 Z3 06 Z2 07 Z1 010 Z0 011 Z67 012 Z66
Make it small!