Re: OT: Need help with numeric bases
From: | Peter Clark <peter-clark@...> |
Date: | Tuesday, February 25, 2003, 4:27 |
On Monday 24 February 2003 07:24 pm, Tristan wrote:
> And this is why I like Python coders. You don't get the non-human
> readable option.
Well, if you don't like line noise, it looks something like this:
---
#! /usr/bin/env python
from sys import argv
def ash(n):
n = int(n) - 1
if n >= 0:
return str(ash(n / 8)) + str((n % 8) + 1)
def dec(n):
acc = 0
for digit in n:
acc = acc * 8 + int(digit)
return acc
a = ash(argv[1])[4:]
print a
print dec(a)
---
One problem with translating this code: Python returns "None" on empty
returns, which is what happens at the last level of recursion in ash(). Thus,
the full return to ash(517) is "None785", hence the [4:] when printing the
answer.
I know there is some way around this, but I haven't played much with
recursion for a while and I started this late in the evening, so my brain
isn't up to speed.
Thanks to Monsieur Teoh for the "perl of wisdom."
Next up: floating numbers!
:Peter
--
Oh what a tangled web they weave who try a new word to conceive!
Replies