python coding contest

Discussion in 'Python' started by Simon Hengel, Dec 25, 2005.

  1. Simon Hengel

    Simon Hengel Guest

    Simon Hengel, Dec 25, 2005
    #1
    1. Advertising

  2. Neat idea!

    I'm far from being a decent Python programmer but I managed (for fun)
    to do it in a one-liner; however, it was definitely longer (in term of
    number of characters) than the more readable multi-line solution.

    André
     
    =?iso-8859-1?B?QW5kcuk=?=, Dec 25, 2005
    #2
    1. Advertising

  3. Simon Hengel

    Tobias Bell Guest

    André schrieb:
    > Neat idea!

    Indeed

    >
    > I'm far from being a decent Python programmer but I managed (for fun)
    > to do it in a one-liner; however, it was definitely longer (in term of
    > number of characters) than the more readable multi-line solution.


    I made a readable version with 352 bytes and a non-readable with 290
    bytes. But it's a really ugly lambda, map, reduce kludge. Looks like Perl.

    >
    > André
    >


    Tobias
     
    Tobias Bell, Dec 25, 2005
    #3
  4. Simon Hengel

    rbt Guest

    Simon Hengel wrote:
    > Hello,
    > we are hosting a python coding contest an we even managed to provide a
    > price for the winner...
    >
    > http://pycontest.net/
    >
    > The contest is coincidentally held during the 22c3 and we will be
    > present there.
    >
    > https://events.ccc.de/congress/2005/wiki/Python_coding_contest
    >
    > Please send me comments, suggestions and ideas.
    >
    > Have fun,
    >


    Does positioning matter? For example, say I give it '123' is it ok to
    output this:

    1
    2
    3

    Or does it have to be 123
     
    rbt, Dec 26, 2005
    #4
  5. rbt wrote:

    > Does positioning matter? For example, say I give it '123' is it ok to
    > output this:
    >
    > 1
    > 2
    > 3
    >
    > Or does it have to be 123


    Download the test suite and you'll see that only 123 on one line passes
    the test. Sorry...

    --
    ==================
    Remi Villatel
    maxilys_@_tele2.fr
    ==================
     
    Remi Villatel, Dec 26, 2005
    #5
  6. On Sun, 25 Dec 2005 16:39:47 +0100, Simon Hengel <> wrote:

    >Hello,
    >we are hosting a python coding contest an we even managed to provide a
    >price for the winner...

    ^^^^^
    >

    How much are you going to sell him or her for? ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, Dec 26, 2005
    #6
  7. Simon Hengel wrote:
    > Hello,
    > we are hosting a python coding contest an we even managed to provide a
    > price for the winner...
    >
    > http://pycontest.net/
    >
    > The contest is coincidentally held during the 22c3 and we will be
    > present there.
    >
    > https://events.ccc.de/congress/2005/wiki/Python_coding_contest
    >
    > Please send me comments, suggestions and ideas.
    >
    > Have fun,
    >


    A funny thing happened to me.

    The http://www.pycontest.net/ site was down for some minutes because :

    "A problem occurred in a Python script. "

    it seems, that

    'some clever cheat'

    has crashed it.

    Claudio
     
    Claudio Grondi, Dec 28, 2005
    #7
  8. Simon Hengel

    Simon Hengel Guest

    > A funny thing happened to me.
    >
    > The http://www.pycontest.net/ site was down for some minutes because :
    >
    > "A problem occurred in a Python script. "
    >
    > it seems, that
    >
    > 'some clever cheat'
    >
    > has crashed it.

    That was me, i broke things while tweaking some stuff.

    Sorry for the inconveniences,

    Simon Hengel

    --
    python coding contest - http://www.pycontest.net/
     
    Simon Hengel, Dec 28, 2005
    #8
  9. I cannot not reach the contest site at since all this morning. :-(
     
    Marius Gedminas, Dec 29, 2005
    #9
  10. I cannot reach the contest site at since all this morning. :-(
     
    Marius Gedminas, Dec 29, 2005
    #10
  11. Simon Hengel wrote:
    > Hello,
    > we are hosting a python coding contest an we even managed to provide a
    > price for the winner...
    >
    > http://pycontest.net/
    >
    > The contest is coincidentally held during the 22c3 and we will be
    > present there.
    >
    > https://events.ccc.de/congress/2005/wiki/Python_coding_contest
    >
    > Please send me comments, suggestions and ideas.
    >
    > Have fun,
    >


    It seems, that the site had some trouble to stay online and especially
    to provide the ranking today.

    I am a bit dissapointed, that my idea of not duplicating, but utilizing
    the efforts others put into solving the job (titled by the submitter ID
    'TheParasite') which resulted in a submission of a 15 Bytes large full
    functional module was evaluated as having a 'syntax error' problem and
    was placed in the ranking at the position according to the size of the
    331 byte large .zip archive it was put into along with some necessary
    installation instructions.

    By the way: trying to submit along with the module complete installation
    instructions and comments failed because there is a 345 bytes limit for
    size of allowed uploads.

    Claudio
     
    Claudio Grondi, Dec 29, 2005
    #11
  12. Simon Hengel

    Simon Hengel Guest

    > It seems, that the site had some trouble to stay online and especially
    > to provide the ranking today.

    There was a problem with our server, sorry for that.

    Have fun,

    Simon Hengel
     
    Simon Hengel, Dec 29, 2005
    #12
  13. Simon Hengel

    Paddy Guest

    So, testosterone wins again!

    We get to boast:
    "Mine's smaller than your's"

    Lets wait for Pythonic to go to bed, then sneak downstairs, go to that
    tripple-X rated 'shortest solutions' website, and 'whack-off' some
    solutions.
    Unghhh, my solution... its coming!!!

    Well don't forget to clean up before Pythonic wakes up.

    Happy new year :)

    - Pad.
     
    Paddy, Dec 31, 2005
    #13
  14. > 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
     
    Claudio Grondi, Jan 1, 2006
    #14
  15. 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.
     
    Steven D'Aprano, Jan 1, 2006
    #15
  16. 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
    >
    > 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

    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, Jan 1, 2006
    #16
  17. Steven D'Aprano wrote:
    > 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.
    >
    >
    >

    What I have thought about as a simpler/better solution is a method
    allowing to avoid processing the content of the string or long integer
    object by looping over its content. I suppose, that knowing enough about
    Python internals it must be possible to change only the object type not
    beeing forced to process the content i.e. the value itself, what in case
    of big size of data to convert with methods like this above wastes CPU
    time.

    Claudio
     
    Claudio Grondi, Jan 1, 2006
    #17
  18. On Sun, 01 Jan 2006 15:49:58 +0100, Claudio Grondi wrote:


    > What I have thought about as a simpler/better solution is a method
    > allowing to avoid processing the content of the string or long integer
    > object by looping over its content.


    How can you avoid looping over its content? Whether you do it yourself
    using "for byte in array" or similar, or Python does it for you
    (using array.tostring perhaps), *something* has to walk through the bytes.

    If you don't like walking the string, write a function to do it once, and
    then use the function.

    > I suppose, that knowing enough about
    > Python internals it must be possible to change only the object type not
    > beeing forced to process the content i.e. the value itself, what in case
    > of big size of data to convert with methods like this above wastes CPU
    > time.


    I'm reminded of a time I was going for a drive in the country when I drove
    past an apple orchid. Standing in the orchid was a farmer with a pig. He
    lifted the pig into the air, and the pig then bit an apple and slowly
    chewed it. The farmer then carried him over to another branch, and the pig
    ate another apple.

    I was so surprised I stopped my car and wandered over to ask the farmer
    what he was doing.

    "I'm feeding apples to my pig," he replied.

    "Wouldn't it save time to just pick some apples and feed them to the pig?"

    The farmer looked at me like I was an idiot. "What's time to a pig?"


    The moral of the story is, before spending time working on some scheme to
    save CPU time, you better be absolutely sure that firstly, you are going
    to save CPU time, secondly, that it is enough CPU time to be worth saving,
    and thirdly, that you aren't wasting more of your own time to do it.



    --
    Steven.
     
    Steven D'Aprano, Jan 1, 2006
    #18
  19. Steven D'Aprano wrote:
    > On Sun, 01 Jan 2006 15:49:58 +0100, Claudio Grondi wrote:
    >
    >
    >
    >>What I have thought about as a simpler/better solution is a method
    >>allowing to avoid processing the content of the string or long integer
    >>object by looping over its content.

    >
    >
    > How can you avoid looping over its content? Whether you do it yourself
    > using "for byte in array" or similar, or Python does it for you
    > (using array.tostring perhaps), *something* has to walk through the bytes.
    >
    > If you don't like walking the string, write a function to do it once, and
    > then use the function.
    >
    >
    >>I suppose, that knowing enough about
    >>Python internals it must be possible to change only the object type not
    >>beeing forced to process the content i.e. the value itself, what in case
    >>of big size of data to convert with methods like this above wastes CPU
    >>time.

    >
    >
    > I'm reminded of a time I was going for a drive in the country when I drove
    > past an apple orchid. Standing in the orchid was a farmer with a pig. He
    > lifted the pig into the air, and the pig then bit an apple and slowly
    > chewed it. The farmer then carried him over to another branch, and the pig
    > ate another apple.
    >
    > I was so surprised I stopped my car and wandered over to ask the farmer
    > what he was doing.
    >
    > "I'm feeding apples to my pig," he replied.
    >
    > "Wouldn't it save time to just pick some apples and feed them to the pig?"
    >
    > The farmer looked at me like I was an idiot. "What's time to a pig?"
    >
    >
    > The moral of the story is, before spending time working on some scheme to
    > save CPU time, you better be absolutely sure that firstly, you are going
    > to save CPU time, secondly, that it is enough CPU time to be worth saving,
    > and thirdly, that you aren't wasting more of your own time to do it.
    >
    >
    >

    It's a funny story :))

    , but in my eyes not appropriate in given context, because my prior goal
    is to understand some more about Python internals, i.e. what is and if
    it is at all a differerence between the internal representation of a
    string and a long integer.

    I know, I should probably look into the C source of Python, but I
    suppose it could be too hard for me to find the appropriate piece of
    code, so I welcome any hints.

    If I knew the internal representation of string and long integer objects
    and were able to read/write to memory and point an identifier at a given
    memory address, a conversion between long integer and string types were
    probably nothing else as changing some bytes and repointing an
    identifier (assuming that string and long integer values are in memory
    the same data if they represent the same value). That it would save CPU
    time is secondary here, but with increasing costs of energy making the
    number on my electrical power bill higher each year due to higher power
    consumption with increasing number of programs I run (it makes a
    difference of 50 Watt between an algorithm keeping the CPU 100% busy and
    an algorithm using only 1% of it) it is not necessarily paranoia driving
    one to consider potential savings of CPU time.
    In this context the example of the bigdec class comes to my mind, where
    usage of another algorithm made it possible to cut down power
    consumption and time of printing a decimal form of the largest known
    prime number from 7 hours of a 100% busy CPU down to 7 seconds!

    Claudio
     
    Claudio Grondi, Jan 1, 2006
    #19
  20. Simon Hengel

    Guest

    Steven D'Aprano wrote:
    > I'm reminded of a time I was going for a drive in the country when I drove
    > past an apple orchid. Standing in the orchid was a farmer with a pig. He
    > lifted the pig into the air, and the pig then bit an apple and slowly
    > chewed it. The farmer then carried him over to another branch, and the pig
    > ate another apple.
    >
    > I was so surprised I stopped my car and wandered over to ask the farmer
    > what he was doing.
    >
    > "I'm feeding apples to my pig," he replied.
    >
    > "Wouldn't it save time to just pick some apples and feed them to the pig?"
    >
    > The farmer looked at me like I was an idiot. "What's time to a pig?"


    Has anyone studied if farmers like him are in general healthier ?
     
    , Jan 1, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Hengel

    Re: python coding contest

    Simon Hengel, Dec 25, 2005, in forum: Python
    Replies:
    92
    Views:
    1,460
    Alex Martelli
    Jan 2, 2006
  2. Christian Tismer

    Re: python coding contest

    Christian Tismer, Dec 25, 2005, in forum: Python
    Replies:
    4
    Views:
    311
    Alex Martelli
    Dec 26, 2005
  3. Tim Hochberg

    Re: python coding contest

    Tim Hochberg, Dec 25, 2005, in forum: Python
    Replies:
    3
    Views:
    304
    Alex Martelli
    Dec 26, 2005
  4. Tim Peters

    Re: python coding contest

    Tim Peters, Dec 26, 2005, in forum: Python
    Replies:
    0
    Views:
    442
    Tim Peters
    Dec 26, 2005
  5. Jean-Paul Calderone

    Re: python coding contest

    Jean-Paul Calderone, Dec 27, 2005, in forum: Python
    Replies:
    2
    Views:
    293
    Tim Hochberg
    Dec 28, 2005
Loading...

Share This Page