9
1
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).
Can we also submit a program that accepts the four inputs? Some languages don't do functions too well. – Stephen – 2017-05-27T01:14:19.447
@StephenS I suppose yes, but I am not sure I'll understand those :/ – Mikhail V – 2017-05-27T01:17:14.387
2The last two arguments seem entirely irrelevant in most languages, why not just require 1 and 0? The first argument being a list doesn't seem to do much besides make regular languages add a simple loop/recursion while making it much harder for more esoteric languages to compete. – FryAmTheEggman – 2017-05-27T01:21:21.497
@MikhailV if you want to understand all of the submissions on your challenges, good luck :/ most of them have TryItOnline links so you can see them works, but they most golfing languages look like nonsense until you know what they do – Stephen – 2017-05-27T01:22:23.957
@FryAmTheEggman I also don't understand what
A
does, since it is the same in all of the test cases – Stephen – 2017-05-27T01:22:51.057@MikhailV have at least 3 different inputs for
A
so we know what it's supposed to do, "A - a list (or an array) of unsigned integers in range 0-255." does not tell me anything about what to do withA
– Stephen – 2017-05-27T01:29:42.677@FryAmTheEggman "why not just require 1 and 0?" custom representation is the main point here. – Mikhail V – 2017-05-27T01:31:06.737
@user202729 Examples are not a specification. This question could use a lot more clarity in the actual specification. I begin to understand the task when reading the examples, but I still think the specification is unclear. – Post Rock Garf Hunter – 2017-05-27T22:43:06.720