Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Defining a new base-type in Python based off Hexavigesimal

42 views
Skip to first unread message

Alec Taylor

unread,
Nov 30, 2011, 10:19:31 AM11/30/11
to comp.lang.python
Good evening,

I have defined a new numbering structure for certain mathematical advantages.

How do I implement this in Python, or would I be better off writing
this in C or C++?

Ultra concise definition: http://i42.tinypic.com/af7w4h.png
LaTeX source: http://pastebin.tlhiv.org/Kf6jPRkI

Thanks for all suggestions,

Alec Taylor

Steven D'Aprano

unread,
Nov 30, 2011, 10:38:58 AM11/30/11
to
On Thu, 01 Dec 2011 02:19:31 +1100, Alec Taylor wrote:

> Good evening,
>
> I have defined a new numbering structure for certain mathematical
> advantages.
>
> How do I implement this in Python, or would I be better off writing this
> in C or C++?
>
> Ultra concise definition: http://i42.tinypic.com/af7w4h.png

Care to translate it into English?

Then translate the English into pseudo-code. And the pseudo-code into
Python. Then, if and only if the Python version is too slow, translate it
into C. And now you are done!



--
Steven

Peter Otten

unread,
Nov 30, 2011, 11:00:55 AM11/30/11
to pytho...@python.org
Alec Taylor wrote:

> I have defined a new numbering structure for certain mathematical
> advantages.
>
> How do I implement this in Python, or would I be better off writing
> this in C or C++?
>
> Ultra concise definition: http://i42.tinypic.com/af7w4h.png
> LaTeX source: http://pastebin.tlhiv.org/Kf6jPRkI
>
> Thanks for all suggestions,

I don't know if that helps you with your project but Python already
understands some hexavigesimal:

>>> int("fool", 26)
280509


Ian Kelly

unread,
Nov 30, 2011, 11:18:39 AM11/30/11
to Alec Taylor, comp.lang.python
On Wed, Nov 30, 2011 at 8:19 AM, Alec Taylor <alec.t...@gmail.com> wrote:
> Good evening,
>
> I have defined a new numbering structure for certain mathematical advantages.
>
> How do I implement this in Python, or would I be better off writing
> this in C or C++?
>
> Ultra concise definition: http://i42.tinypic.com/af7w4h.png
> LaTeX source: http://pastebin.tlhiv.org/Kf6jPRkI
>
> Thanks for all suggestions,

So if I am understanding your definition correctly your hexavigesimals
are ordered like this?

0, 1, ..., 9, A, B, ..., P,

0A, 0B, ..., 0P,
1A, 1B, ..., 1P,
...,
9A, 9B, ..., 9P,

A0, A1, ..., A9,
B0, B1, ..., B9,
...,
P0, P1, ..., P9

And that's it, since your constraints preclude anything with more than 2 digits?

Alec Taylor

unread,
Nov 30, 2011, 11:26:53 AM11/30/11
to Ian Kelly, comp.lang.python
To put it simply:

I want a hexavigesimal of length n where each element contains at
least one letter {A, B, ..., P} and one number {0, 1, 2, ... }.
(unless n is less than 2, in which case only one of those constraints
need to be met)

and I want addition only for incrementing the element.

Why do I want all this I hear you ask? - For use as a
unique-identifier. I want a unique-ID of size 3. (3 positions, i.e.
P0A)

Any suggestions on how to implement this in python would be appreciated.

Thanks,

Alec Taylor

Devin Jeanpierre

unread,
Nov 30, 2011, 12:08:24 PM11/30/11
to Steven D'Aprano, pytho...@python.org
> Care to translate it into English?
>
> Then translate the English into pseudo-code. And the pseudo-code into
> Python. Then, if and only if the Python version is too slow, translate it
> into C. And now you are done!

Mathematical English is pseudocode. Related: all theorems are also
programs and vice versa.



That said, I don't understand the notation either. What's \mathbb{M}?
What does it mean ti be "in" some natural number n? (Are you using a
set theoretic definition of natural numbers: e.g. x in y iff x <= y ?)

Devin
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Ian Kelly

unread,
Nov 30, 2011, 1:38:45 PM11/30/11
to Alec Taylor, comp.lang.python
On Wed, Nov 30, 2011 at 9:26 AM, Alec Taylor <alec.t...@gmail.com> wrote:
> On Thu, Dec 1, 2011 at 3:18 AM, Ian Kelly <ian.g...@gmail.com> wrote:
>> On Wed, Nov 30, 2011 at 8:19 AM, Alec Taylor <alec.t...@gmail.com> wrote:
>>> Good evening,
>>>
>>> I have defined a new numbering structure for certain mathematical advantages.
>>>
>>> How do I implement this in Python, or would I be better off writing
>>> this in C or C++?
>>>
>>> Ultra concise definition: http://i42.tinypic.com/af7w4h.png
>>> LaTeX source: http://pastebin.tlhiv.org/Kf6jPRkI
>>>
>>> Thanks for all suggestions,
>>
>> So if I am understanding your definition correctly your hexavigesimals
>> are ordered like this?
>>
>> 0, 1, ..., 9, A, B, ..., P,
>>
>> 0A, 0B, ..., 0P,
>> 1A, 1B, ..., 1P,
>> ...,
>> 9A, 9B, ..., 9P,
>>
>> A0, A1, ..., A9,
>> B0, B1, ..., B9,
>> ...,
>> P0, P1, ..., P9
>>
>> And that's it, since your constraints preclude anything with more than 2 digits?
>
> To put it simply:
>
> I want a hexavigesimal of length n where each element contains at
> least one letter {A, B, ..., P} and one number {0, 1, 2, ... }.
> (unless n is less than 2, in which case only one of those constraints
> need to be met)
>
> and I want addition only for incrementing the element.
>
> Why do I want all this I hear you ask? - For use as a
> unique-identifier. I want a unique-ID of size 3. (3 positions, i.e.
> P0A)
>
> Any suggestions on how to implement this in python would be appreciated.

Okay, that's much clearer. If you don't actually care about the
ordinal value of each element, only about incrementing them, then I
suggest implementing increment as follows:

1. Convert the string to an int using the regular base-26 conversion
with the int() built-in.
2. In a loop, add 1 to the value.
3. Convert the value back to a string using the same base-26
conversion. I don't know of a built-in that does this, so you'll need
to write your own, which isn't too difficult.
4. Check whether the new string meets all your constraints. If it
does, return it. If not, go back to 2. and try again with the next
integer.

At most it will take 17 iterations to find a valid value, but most of
the time the first value tried will be valid.

I assume you're already aware that with 3 digits you'll only have
12480 possible unique IDs using this scheme? It just doesn't seem
like very many to me.

Cheers,
Ian

Alec Taylor

unread,
Nov 30, 2011, 2:00:38 PM11/30/11
to Ian Kelly, comp.lang.python
Excellent, I'll see if I can implement that.

I was thinking more base data-type, but that seems impossible in python.

17 iterations (rarghh!)

But yeah, I'll try that, thanks.

Peter Otten

unread,
Nov 30, 2011, 2:05:50 PM11/30/11
to pytho...@python.org
Maybe you don't need a class if you produce the ids with a generator like
so:

>>> from itertools import product
>>> def hxv(n):
... letters = "ABCDEFGHIJKLMNOP"
... digits = "0123456789"
... both = digits + letters
... if n == 1:
... for x in both:
... yield x
... else:
... for x in product(*[both]*n):
... s = "".join(x)
... if not s.isdigit() and not s.isalpha():
... yield s
...
>>> ids = hxv(3)
>>> next(ids)
'00A'
>>> next(ids)
'00B'
>>> for i, x in enumerate(hxv(3)): pass
...
>>> i, x
(12479, 'PP9')


0 new messages