GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-01-01 20:28:24

leif902
Member
From: Georgia, USA
Registered: 2007-10-09
Posts: 24

Unary Encoding

I can see little purpose for this script, it's simple enough, but I'm sure someone can find a use for it.

Expand// unary_encode(num)
// Returns the unary encoded version of a number as a string.
// ex. unary_encode(5) returns "11110"

var num, unum;
num = argument0;
unum = "";
repeat( num - 1 ) {
     unum += "1";
}
unum += "0";
return ( unum );

(You could of course optimize the repeat loop, but I didn't see a need for it as it will execute fast enough)


Leif902

Offline

#2 2008-01-02 00:31:24

Yourself
Member
Registered: 2007-10-09
Posts: 48

Re: Unary Encoding

Unary?  Why does it end in 0?  What's the point of a character that doesn't depend on the value?  Of course, I also consider this script to be completely pointless and probably doesn't need to be on the site.

Expandreturn (argument0 - 1) * "1" + "0";

Much shorter.

Last edited by Yourself (2008-01-02 14:04:12)

Offline

#3 2008-01-02 00:35:05

leif902
Member
From: Georgia, USA
Registered: 2007-10-09
Posts: 24

Re: Unary Encoding

Ah neat, I didn't realize you could use multiplication with strings. I've never found a use for this script, but I have heard of it being used in various algorithms for discrete probability distribution and geometric distribution. Look up "Unary coding" on Google or Wikipedia and I'm sure you'll find some use for it.

EDIT: Sure enough, Wikipedia has an article on everything... http://en.wikipedia.org/wiki/Unary_coding
EDIT 2: Even if some of them are rather bad...

Last edited by leif902 (2008-01-02 00:36:30)


Leif902

Offline

Board footer

Powered by FluxBB