Skip to main content
added 387 characters in body
Source Link
manatwork
  • 20.6k
  • 5
  • 51
  • 81

Sed, 40 characters

(39 characters code + 1 character command line option.)

s/[^][]+//g
:;s/]\[//;t
s/]//g
s/\[/1/g

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;s/]//g;s/\[/1/g' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111

Sed, 33 characters

(32 characters code + 1 character command line option.)

If trailing spaces are allowed in the output.

s/[^][]+//g
:;s/]\[//;t
y/[]/1 /

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;y/[]/1 /' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111      

Sed, 40 characters

(39 characters code + 1 character command line option.)

s/[^][]+//g
:;s/]\[//;t
s/]//g
s/\[/1/g

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;s/]//g;s/\[/1/g' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111

Sed, 40 characters

(39 characters code + 1 character command line option.)

s/[^][]+//g
:;s/]\[//;t
s/]//g
s/\[/1/g

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;s/]//g;s/\[/1/g' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111

Sed, 33 characters

(32 characters code + 1 character command line option.)

If trailing spaces are allowed in the output.

s/[^][]+//g
:;s/]\[//;t
y/[]/1 /

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;y/[]/1 /' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111      
Source Link
manatwork
  • 20.6k
  • 5
  • 51
  • 81

Sed, 40 characters

(39 characters code + 1 character command line option.)

s/[^][]+//g
:;s/]\[//;t
s/]//g
s/\[/1/g

Input: string, output: unary number.

Sample run:

bash-4.3$ sed -r 's/[^][]+//g;:;s/]\[//;t;s/]//g;s/\[/1/g' <<< '[1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14]'
111111