Binary to unary conversion
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 algorithms. Its steps are as follows:
- Replace all instances of
1with0*(*can be any character) - Replace all instances of
*0with0** - Remove all instances of
0
For example, here is the conversion of 1001 to *********:
1001:1001->0*000*
0*000*:0*000*->00**00*00**00*->00*0**0*00*0**0*->000***0***000***0***->000**0*****000**0*****->000*0*******000*0*******->0000*********
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)
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