Re: python coding contest
Claudio Grondi wrote:[color=blue][color=green]
>> Please send me comments, suggestions and ideas.[/color]
>
>
> 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.compressio n than to problems belonging into
> comp.lang.pytho n .
>
> 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 longIntWithBits OfBinaryString( stringToConvert ):
> intWithBitsOfBi naryString = 0L
> for singleChar in stringToConvert :
> intWithBitsOfBi naryString = (intWithBitsOfB inaryString<<8) +
> ord(singleChar)
> #:for
> return intWithBitsOfBi naryString
> #:def longIntWithBits OfBinaryString( s)
>
> def binaryStringWit hBitsOfLongInt( i):
> listOfCharsOfSt ringWithThePack edInt = []
> exponent = 1
> while i > 256**exponent: exponent+=1
> for byteNo in range(0,exponen t):
> noOfBitsToShift = byteNo*8
>
> listOfCharsOfSt ringWithThePack edInt.append(ch r(i>>noOfBitsTo Shift&0xFF))
> #:for
> # reverse (in place) in order to get the highest bits of the integer
> as leftmost byte
> listOfCharsOfSt ringWithThePack edInt.reverse()
> stringWithThePa ckedInt = ''.join(listOfC harsOfStringWit hThePackedInt)
> return stringWithThePa ckedInt
> #:def binaryStringWit hBitsOfLongInt( i)
>
> print "longIntWithBit sOfBinaryString ('ABBA') =
> %i"%longIntWith BitsOfBinaryStr ing('ABBA')
> print
> "binaryStringWi thBitsOfLongInt (longIntWithBit sOfBinaryString ('ABBA')) =
> '%s'"%binaryStr ingWithBitsOfLo ngInt(longIntWi thBitsOfBinaryS tring('ABBA'))
>
> which gives:
>
> longIntWithBits OfBinaryString( 'ABBA') = 1094861377
> binaryStringWit hBitsOfLongInt( longIntWithBits OfBinaryString( '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[/color]
After some coding trials, it turned out to be quite easy (almost
trivial) to overcome the problem of not beeing allowed to use any
literals in the script code, but I suppose, that I am not alone with not
seeing directly how to code it, so it is maybe a good exercise for a
Python beginner (like me) to cope a bit with it.
Knowing this I am curious if it is also comparable easy in other
programming languages, e.g. when using only a C/C++ compiler and linker
executables along with the provided libraries and header files? I
suppose, that each language comes with built-in literals which can be
utilized in own code to get the full range of required literals into
identifiers by using only what the language provides itself.
Am I right or not?
Claudio
Claudio Grondi wrote:[color=blue][color=green]
>> Please send me comments, suggestions and ideas.[/color]
>
>
> 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.compressio n than to problems belonging into
> comp.lang.pytho n .
>
> 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 longIntWithBits OfBinaryString( stringToConvert ):
> intWithBitsOfBi naryString = 0L
> for singleChar in stringToConvert :
> intWithBitsOfBi naryString = (intWithBitsOfB inaryString<<8) +
> ord(singleChar)
> #:for
> return intWithBitsOfBi naryString
> #:def longIntWithBits OfBinaryString( s)
>
> def binaryStringWit hBitsOfLongInt( i):
> listOfCharsOfSt ringWithThePack edInt = []
> exponent = 1
> while i > 256**exponent: exponent+=1
> for byteNo in range(0,exponen t):
> noOfBitsToShift = byteNo*8
>
> listOfCharsOfSt ringWithThePack edInt.append(ch r(i>>noOfBitsTo Shift&0xFF))
> #:for
> # reverse (in place) in order to get the highest bits of the integer
> as leftmost byte
> listOfCharsOfSt ringWithThePack edInt.reverse()
> stringWithThePa ckedInt = ''.join(listOfC harsOfStringWit hThePackedInt)
> return stringWithThePa ckedInt
> #:def binaryStringWit hBitsOfLongInt( i)
>
> print "longIntWithBit sOfBinaryString ('ABBA') =
> %i"%longIntWith BitsOfBinaryStr ing('ABBA')
> "binaryStringWi thBitsOfLongInt (longIntWithBit sOfBinaryString ('ABBA')) =
> '%s'"%binaryStr ingWithBitsOfLo ngInt(longIntWi thBitsOfBinaryS tring('ABBA'))
>
> which gives:
>
> longIntWithBits OfBinaryString( 'ABBA') = 1094861377
> binaryStringWit hBitsOfLongInt( longIntWithBits OfBinaryString( '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[/color]
After some coding trials, it turned out to be quite easy (almost
trivial) to overcome the problem of not beeing allowed to use any
literals in the script code, but I suppose, that I am not alone with not
seeing directly how to code it, so it is maybe a good exercise for a
Python beginner (like me) to cope a bit with it.
Knowing this I am curious if it is also comparable easy in other
programming languages, e.g. when using only a C/C++ compiler and linker
executables along with the provided libraries and header files? I
suppose, that each language comes with built-in literals which can be
utilized in own code to get the full range of required literals into
identifiers by using only what the language provides itself.
Am I right or not?
Claudio
Comment