16
1
Unary numbers typically only represent nonnegative integers, but we can extend them to represent all integers as follows:
- A positive integer N is represented as N
1
's:5 -> 11111
- A negative integer -N is represented as a
0
followed by N1
's:-5 -> 011111
- Zero is represented as
0
We can then represent a list of these numbers unambiguously if we use 0
as the separator:
3,-2,0,1
111,011,0,1
111 0 011 0 0 0 1
11100110001
Your task: take a string representing such a list of signed unary numbers, and translate it into a list of decimal numbers.
Details
You may assume that the input is a complete list of signed unary numbers. In particular, your program will not have to handle 1) empty input or 2) input that ends with a separator.
You may assume that the magnitude of each number will not exceed 127. For languages with maximum sizes of strings or lists, you may assume that the input and output will fit in your language's data structures, but your algorithm should theoretically work for a list of any size.
Your program or function may perform I/O in any of the standard ways. Input may be a string or a list of characters, single-character strings, integers, or booleans. You may use any two characters to represent 1
and 0
; if you don't use 1
and 0
, please specify which characters you're using.
Output must be decimal numbers in any reasonable list format (in particular, there must be some kind of a separator between numbers). Negative numbers should be indicated with a minus sign, although if your language has a different format for negative integers I will also accept that. Zero may be represented in the output as 0
or -0
.
Test cases
1 -> 1
0 -> 0 (or -0, and similarly for the other test cases)
011 -> -2
1101 -> 2,1
1100 -> 2,0
11001 -> 2,-1
110001 -> 2,0,1
11100110001 -> 3,-2,0,1
00000001 -> 0,0,0,-1
01111011111111001111111111111110111111111111111100111111111111111111111110111111111111111111111111111111111111111111 -> -4,8,-15,16,-23,42
01111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 -> -127
2Nitpick: Since this contains
'0's
, it isn't technically unary. Good challenge though! – James – 2017-12-16T07:16:56.3434@DJMcMayhem Nitpick to the nitpick: I technically never said it was unary. It is an extension of unary which I am calling "signed unary." ;) – DLosc – 2017-12-16T07:23:16.370
@DJMcMayhem IMO, the challenge is specifically that the separator (
0
) and the negative sign prefix (0
) are the same, although it's still unambiguous, since you can't have negative signs in the middle of a number (is182--693-1
a number? no, and neither is1111011000101111
for the exact same reason). – Erik the Outgolfer – 2017-12-16T09:35:30.987Is it okay if the outputted list is in reverse order of the input? – James – 2017-12-16T09:49:43.783
well technically decimal isn't decimal either since it uses the ' - ' symbol – Unlambder – 2017-12-16T10:45:04.917
@DJMcMayhem No, it should be the same list and therefore in the same order. – DLosc – 2017-12-16T18:17:34.363
Doesn't input of
0
end in a separator? – Giuseppe – 2017-12-16T20:26:08.593@Giuseppe No, for the same reason that input of
011
doesn't begin with a separator:0
is not considered a separator when it's part of a number. – DLosc – 2017-12-16T21:23:48.320