Python Mystery Theatre -- Episode 3: Extend this

Discussion in 'Python' started by Raymond Hettinger, Jul 22, 2003.

  1. Here are few more mini-mysteries for your amusement
    and edification.

    Again in this episode, the program output is not shown.
    Your goal is to predict the output and, if anything
    mysterious occurs, then explain what happened
    (in blindingly obvious terms).

    This time, the extra credit is for picking-out the three
    that surfaced as errors in my own, real code over
    the past few years (the example context below may be
    different, but the type of error occurred in real code).

    Try to solve these without looking at the other posts.
    Let me know if you learned something new along the way.


    Enjoy,


    Raymond Hettinger


    ACT I ---------------------
    def real(z):
    return hasattr(z, 'real') and z.real or z
    def imag(z):
    return hasattr(z, 'imag') and z.imag or 0
    for z in 3+4j, 5, 0+6j, 7.0, 8+0j, 9L:
    print real(z), imag(z)

    ACT II ---------------------
    uniq = {}
    for k in (10, 'ten', 10+0j, u'ten', 'Ten', 't'+'en', 10.0):
    uniq(k) = 1
    print len(uniq)

    ACT III ---------------------
    s = 'abracadabra'
    for i in [3, 2, 1, 0]:
    print s[i:], s[-i:]

    ACT IV ---------------------
    pets = list('cat ')
    pets += 'dog '
    pets.extend('fish ')
    print pets + 'python'

    INTERMISSION (with output included, oh yeah! ------------
    >>> import operator
    >>> 1 - reduce(operator.add, [1e-7]* 10**7)

    2.4983004554002264e-010

    ACT V ---------------------
    class Bin:
    numitems = 0
    contents = []

    def add(self, item):
    self.numitems += 1
    self.contents += [item]

    laundry = Bin()
    groceries = Bin()

    laundry.add('shirt')
    groceries.add('bananas')
    groceries.add('nuts')
    laundry.add('socks')
    groceries.add('pretzels')

    print laundry.numitems, laundry.contents
    print groceries.numitems, groceries.contents


    ACT VI -----------------------------------------
    print "What is the technical name for this algorithm or transformation?"
    a = range(0, 100, 10)
    import random
    random.shuffle(a)
    print "Input:", a
    swaps = 0
    for i in range(len(a)):
    for j in range(i+1, len(a)):
    if a > a[j]:
    i, j, a, a[j], swaps = j, i, a[j], a, swaps+1
    print "Output:", a
    print "Workload;", swaps
     
    Raymond Hettinger, Jul 22, 2003
    #1
    1. Advertising

  2. [Steven Taschuk]
    > > ACT VI -----------------------------------------
    > > print "What is the technical name for this algorithm or transformation?"

    > [...]
    >
    > Heh. Nice one.
    >
    > The technical name is "the identity transformation".



    Ding ding! We have a winner.

    Others thought the i,j swap was suspicious but did
    not put the finger on left-to-right assignment undermining
    the whole selection sort to make it do nothing at all.


    Raymond Hettinger
     
    Raymond Hettinger, Jul 22, 2003
    #2
    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. Ben Finney
    Replies:
    12
    Views:
    635
    Chris Reedy
    Jul 14, 2003
  2. Raymond Hettinger

    Python Mystery Theatre -- Episode 2: Así Fue

    Raymond Hettinger, Jul 14, 2003, in forum: Python
    Replies:
    12
    Views:
    557
    Erik Max Francis
    Jul 16, 2003
  3. Army1987

    delirious program, episode II

    Army1987, Mar 21, 2007, in forum: C Programming
    Replies:
    14
    Views:
    540
    Army1987
    Mar 23, 2007
  4. Peter Szinek
    Replies:
    3
    Views:
    136
    Adam Akhtar
    Aug 19, 2008
  5. Stedwick
    Replies:
    6
    Views:
    150
    Stedwick
    Jan 21, 2008
Loading...

Share This Page