Eyeballing the binary values printed as ovals and sticks is not so easy...
To help with that, you must write a function (or a program) that prints numbers in custom binary representation.
So I want to take a number, say 3 (00000011
) and output the bits in user-defined format, for example with space separated pairs:
00 00 00 11
or, say, in reversed order and with some decorators, e.g.:
11_00_00_00
Furthermore, there must be possibilty to show '0' and '1' as custom characters to distinguish them better, e.g.:
XX oo oo oo
So the challenge is to write the code which does that all whithin the following specification.
Specification
The function takes input like this: f(A, mask, zeros, ones)
Parameters:
A -- input number -- any (unsigned) integer in range 0-255.
mask -- a string parameter which defines the construction of the output.
zeros -- a string of the same length, defines 'zero' glyphs for each output slot.
ones -- a string of the same length, defines 'one' glyphs for each output slot.
Rules for the output construction:
Look at this image with example closely to understand how the output is generated:
So only the single digits in the mask are parsed and replaced by corresponding bits of A, other characters are left as is. Further, if the value of the taken bit is 1 then it shows up in the final output as "X" and if it is 0 then it shows up as "o". In the above example all four taken bits are "1" so we see "X" in all slots.
If the input number was 128, then, logically, the output would be X foo bar ooo
. Characters in parameters "zeros" and "ones": any printable ASCII chars, assume they are always char-aligned with the mask.
Notes:
- Bits are 0-indexed: 0th bit is the MSB.
- Assume that digits 8,9 are not allowed in the mask string.
- Input Strings include any printable ASCII chars.
- 'Zeros' and 'ones' are char-aligned with the mask.
- For special characters/modifiers in your language: we can assume they will not appear in the input string.
For the clarity, see more examples.
Input -> Output examples
Output all 8 bits in common order with a space delimiter, in common oval-and-stick notation:
mask = "0123 4567"
zeros = "0000 0000"
ones = "1111 1111"
A=1 -> 0000 0001
Output in reversed order, in dash-and-glyph notation:
mask = "| 7654 3210 |"
zeros= " ---- ---- "
ones = " ssss ssss "
A=1 -> | s--- ---- |
A=3 -> | ss-- ---- |
A=128-> | ---- ---s |
Diverse notations in one output, e.g. for packed data:
mask = "0 | 123 4567"
zeros= " --- ----"
ones = "X kkk ssss"
A= 15 -> | --- ssss
A= 16 -> | --k ----
A= 32 -> | -k- ----
A= 128 -> X | --- ----
A= 255 -> X | kkk ssss
Repeating patterns:
mask = "| 7 66 555 4444 |"
zeros= " . .. ... .... "
ones = " 0 00 000 0000 "
A= 0 -> | . .. ... .... |
A= 1 -> | 0 .. ... .... |
A= 2 -> | . 00 ... .... |
A= 3 -> | 0 00 ... .... |
A= 4 -> | . .. 000 .... |
Update
The rules have been slightly simplified - the program must print one number only (not array/list of numbers as it was proposed initially).
A
does, since it is the same in all of the test cases \$\endgroup\$