python coding contest

Claudio Grondi claudio.grondi at freenet.de
Sat Dec 31 21:34:33 EST 2005


> 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

As (1) leads to less readable cryptic code it makes not much sense from 
my point of view to dig deeper in that direction. As already mentioned 
in this thread by Tim Peters ( pointing to 
http://spoj.sphere.pl/problems/KAMIL/ ) it seems, that Pearl is here the 
proper language of choice for such kind of problems anyway.

Trying to improve on (2) belongs in my eyes much more into the area of 
problems discussed in comp.compression than to problems belonging into 
comp.lang.python .

So what is my point? Ok, I will mention it at the end of this post.

Before that I want to thank the originators of the contest and 
especially the participants for providing insight into the techniques 
they have used. I have learned from the contest what lambda expression 
is good for and how it works  where I failed to grasp it from reading 
tutorials only.

I have detected, that it would be a nice thing to have in Python a 
function able to convert values from binary string to an integer 
representation as in my eyes both in case of long integer values are 
more or less the same thing/object. The only difference is probably in 
the header not in the representation of the actual value in memory - am 
I right here? Will it make sense to have a string-integer object which 
value can be used in both contexts as a binary string and a long integer 
value?
Is there in Python any simple way to do the same as the following two 
following functions I have put together today:

def longIntWithBitsOfBinaryString(stringToConvert):
   intWithBitsOfBinaryString = 0L
   for singleChar in stringToConvert:
     intWithBitsOfBinaryString = (intWithBitsOfBinaryString<<8) + 
ord(singleChar)
   #:for
   return intWithBitsOfBinaryString
#:def longIntWithBitsOfBinaryString(s)

def binaryStringWithBitsOfLongInt(i):
   listOfCharsOfStringWithThePackedInt = []
   exponent = 1
   while i > 256**exponent: exponent+=1
   for byteNo in range(0,exponent):
     noOfBitsToShift = byteNo*8
 
listOfCharsOfStringWithThePackedInt.append(chr(i>>noOfBitsToShift&0xFF))
   #:for
   # reverse (in place) in order to get the highest bits of the integer 
as leftmost byte
   listOfCharsOfStringWithThePackedInt.reverse()
   stringWithThePackedInt = ''.join(listOfCharsOfStringWithThePackedInt)
   return stringWithThePackedInt
#:def binaryStringWithBitsOfLongInt(i)

print "longIntWithBitsOfBinaryString('ABBA') = 
%i"%longIntWithBitsOfBinaryString('ABBA')
print 
"binaryStringWithBitsOfLongInt(longIntWithBitsOfBinaryString('ABBA')) = 
'%s'"%binaryStringWithBitsOfLongInt(longIntWithBitsOfBinaryString('ABBA'))

which gives:

longIntWithBitsOfBinaryString('ABBA') = 1094861377
binaryStringWithBitsOfLongInt(longIntWithBitsOfBinaryString('ABBA')) = 
'ABBA'

?

And now my point I have promised to write about:

If squeezing code makes it bad code and compressing literals is more or 
less compression technique and not Python programming, it is maybe a 
good idea to try to explore what Python distribution provides as data 
and modules and rewrite the  seven_seg  module, but with following 
limitations:

1. it is not allowed to use any literals in the provided code
2. it is not allowed to misuse the names of the identifiers as a kind of 
literals providing data
3. it is not allowed to use modules or files which doesn't come with the 
Python distribution.

I have no slightest idea if it is possible to program a  seven_seg 
module under such conditions. It could be a sign, that it would be a 
very interesting challenge worth to get involved into or a sign I have 
no slightest idea about Python and programming.

What do you all think about it?

Claudio



More information about the Python-list mailing list