On Sun, 01 Jan 2006 03:34:33 +0100, Claudio Grondi wrote:

>> Please send me comments, suggestions and ideas.
> 
> Now, after the contest is over I analysed the outcome of it and have 
> come to the conclusion, that there were two major factors which 
> contributed to squeezing of code:
> 
>    (1). usage of available variants for coding of the same thing
>    (2). sqeezing the size of used numeric and string literals

[snip]

> Is there in Python any simple way to do the same as the following two 
> following functions I have put together today:

They are already pretty simple. You can make them even more simple by
using less complicated names and getting rid of the explicit end block
markers. It is sometimes useful to put in explicit end block markers when
you have long blocks, but when the block is just a single line, well,
I don't see the point.

Here is another possibility.

>>> import array
>>> A = array.array('b')
>>> n = 1000000
>>> while n:
...     A.append(n&255); n = n >> 8
... 
>>> A.reverse()
>>> A
array('b', [15, 66, 64])
>>> 15*256**2 + 66*256 + 64
1000000
>>> A.tostring()
'\x0fB@'

The reverse transformation is just as easy:

>>> A = array.array('b', "\x0fB@")  # initialise from a byte string
>>> n = 0L
>>> for b in A:
...     n = n << 8 | b
...
>>> n
1000000L

And of course these can be turned into functions.



-- 
Steven.

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

Reply via email to