Binary to unary conversion

From Esolang
Jump to navigation Jump to search

A binary to unary conversion program is a program that can convert a given number in binary form to a unary form. It is mainly used to showcase Markov algorithm (wikipedia). Its steps are as follows:

  1. Replace all instances of 1 with 0* (* can be any character)
  2. Replace all instances of *0 with 0**
  3. Remove all instances of 0

For example, here is the conversion of 1001 to *********:

  1. 1001:
    1. 1001 -> 0*000*
  2. 0*000*:
    1. 0*000* -> 00**00*
    2. 00**00* -> 00*0**0*
    3. 00*0**0* -> 000***0***
    4. 000***0*** -> 000**0*****
    5. 000**0***** -> 000*0*******
    6. 000*0******* -> 0000*********
  3. 0000********* -> *********


Examples in programming languages

Python 3.0

def binarytounary(bstring):
 bstring = bstring.replace("1","0*")
 while "*0" in bstring:
   bstring=bstring.replace("*0","0**")
 return bstring.replace("0","")

///

/1/0*//*0/0**//0//100010

Retina

1
0*
+`\*0
0**
0
(an extra line must be present)

Try it online.

Stringle

a $

#a
.a 1 b b "0*"
.a !1 b b "0"
a :a
#a

#b
t .b .:b
t "*0" b "0**" ::b
t "*0" b c b
t "*0" c ""
c c .b
b :b
#b

#c
.c "*" d d "*"
c :c
#c

$ d
$ "decimal: " #d

Sed

s/1/0*/g;:x;s/\*0/0**/g;tx;s/0//g