# Integer encoding challenge

**URL:** <https://www.ruby-forum.com/t/integer-encoding-challenge/98938>\
**Category:** Ruby\
**Created:** [July 19, 2006, 4:00am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938 "2006-07-19T04:00:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 19, 2006, 4:00am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938/1 "2006-07-19T04:00:48Z")

</div>

Hey all,

I need to come up with a way to encode an integer as a string so that  
it will sort correctly lexicographically. This is pretty easy when you  
have a fixed integer range but how do you do it with Ruby’s BigNums?  
Any ideas?

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 19, 2006, 4:19am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938/2 "2006-07-19T04:19:49Z")

</div>

“David B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hey all,
> 
> I need to come up with a way to encode an integer as a string so that  
> it will sort correctly lexicographically. This is pretty easy when you  
> have a fixed integer range but how do you do it with Ruby’s BigNums?  
> Any ideas?
> 
> Cheers,  
> Dave

def encode\_integer(num)  
“x”\*num  
end

Do I win something?

YS.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 19, 2006, 5:11am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938/3 "2006-07-19T05:11:33Z")

</div>

On 7/19/06, Yohanes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Dave
> 
> def encode\_integer(num)  
> “x”\*num  
> end
> 
> Do I win something?
> 
> YS.

Nice idea 😛  
But how about negative numbers.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 19, 2006, 5:24am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938/4 "2006-07-19T05:24:19Z")

</div>

“David B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > Cheers,  
> > > Nice idea 😛  
> > > But how about negative numbers.

Same technique, but put them in different namespace.

YS.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 19, 2006, 7:27am UTC](https://www.ruby-forum.com/t/integer-encoding-challenge/98938/5 "2006-07-19T07:27:36Z")

</div>

On 7/19/06, Yohanes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > Any ideas?  
> > > > YS.
> > 
> > Nice idea 😛  
> > But how about negative numbers.
> 
> Same technique, but put them in different namespace.
> 
> YS.

I need to be able to compare positive integers with negative so I  
would need to add a bit to this technique, (not to mention a couple of  
terabytes of memory to handle even moderately large numbers :))

def encode\_integer(int)  
if (int \> 0)  
“z” \* int  
else  
“x” \* -int + “y”  
end  
end

Anyway, here is a better solution using only only digits 0-9. I’ll  
extend it myself to use the full ascii alphabet;

def encode\_int(int)  
if (int \> 0)  
int\_str = int.to\_s  
(“3%04d” % int\_str.size) + int\_str  
elsif (int \< 0)  
int\_str = (-int).to\_s  
(“1%04d” % (9999 - int\_str.size)) + (10 \*\* (int\_str.size + 1) +  
int).to\_s  
else  
“2”  
end  
end

Cheers,  
Dave
