Skip to main content
Use backticks for -3 and add a wordy explanation
Source Link
mercator
  • 369
  • 1
  • 7

Python 2, 7373 70 bytes

A function that takes a string as input and returns a string representation of a Python list. Zero can be represented both by 0 and -0 (when it comes last):

lambda s:str(map`map(len,s.split('0')))`.replace('0, ','-').replace('--','0,')

Explanation

  1. split the input string s on zeroes.
  2. Take the length of each string in the resulting list (using map).

That takes us a long way. Zeroes were separators after all. And the numbers were unary, so len conveniently converts those to decimal. But now we've messed up all the non-separator uses of 0. Luckily, all non-separator uses were leading zeroes so they came after a separator-zero and gave us zero-length strings ('00'.split('0') == ['', '', '']). Those zero-length strings then also became 0 because of the len.

  1. Turn the list into a string (using "reverse quotes"), so we can fix up the mess more easily.
  2. replace each zero that precedes another number by a negative sign on that number instead. That fixes the use of 0 as a sign but it breaks the literal zeroes. Literal zeroes were also preceded by a separator, so they've now become pairs of extra dashes on the next number.
  3. replace each -- back into a 0 element in the "list".

Python 2, 73 bytes

A function that takes a string as input and returns a string representation of a Python list. Zero can be represented both by 0 and -0 (when it comes last):

lambda s:str(map(len,s.split('0'))).replace('0, ','-').replace('--','0,')

Python 2, 73 70 bytes

A function that takes a string as input and returns a string representation of a Python list. Zero can be represented both by 0 and -0 (when it comes last):

lambda s:`map(len,s.split('0'))`.replace('0, ','-').replace('--','0,')

Explanation

  1. split the input string s on zeroes.
  2. Take the length of each string in the resulting list (using map).

That takes us a long way. Zeroes were separators after all. And the numbers were unary, so len conveniently converts those to decimal. But now we've messed up all the non-separator uses of 0. Luckily, all non-separator uses were leading zeroes so they came after a separator-zero and gave us zero-length strings ('00'.split('0') == ['', '', '']). Those zero-length strings then also became 0 because of the len.

  1. Turn the list into a string (using "reverse quotes"), so we can fix up the mess more easily.
  2. replace each zero that precedes another number by a negative sign on that number instead. That fixes the use of 0 as a sign but it breaks the literal zeroes. Literal zeroes were also preceded by a separator, so they've now become pairs of extra dashes on the next number.
  3. replace each -- back into a 0 element in the "list".
Source Link
mercator
  • 369
  • 1
  • 7

Python 2, 73 bytes

A function that takes a string as input and returns a string representation of a Python list. Zero can be represented both by 0 and -0 (when it comes last):

lambda s:str(map(len,s.split('0'))).replace('0, ','-').replace('--','0,')