473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Python Mystery Theatre -- Episode 3: Extend this

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[i] > a[j]:
i, j, a[i], a[j], swaps = j, i, a[j], a[i], swaps+1
print "Output:", a
print "Workload;", swaps

Jul 18 '05 #1
1 1552
[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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Raymond Hettinger | last post by:
For your amusement and edification, I'm working on a series of Python puzzles designed to highlight areas of the language known only to those who have read the docs more than once. Each of the...
12
by: Raymond Hettinger | last post by:
Here are four more mini-mysteries for your amusement and edification. In this episode, the program output is not shown. Your goal is to predict the output and, if anything mysterious occurs,...
5
by: Robert Brewer | last post by:
WARNING: The following is NOT all Pythonic. However, it is Python. This is just fun--nothing I'm going to put into production. I don't have any questions or problems--just thought I'd share. ...
0
by: Volker Gietz | last post by:
Hello all, I have been using Python for almost a year now and by now I do 95% of my programming in it except some DSP stuff in C++. I learnt Python's basics in one week, yet the language still...
12
by: Ryan Paul | last post by:
I've spent a lot of time using python, and personally, I feel like it is vastly superior when compared to languages like java, and c++, but there are still a few things that detract from its...
83
by: Licheng Fang | last post by:
Hi, I'm learning STL and I wrote some simple code to compare the efficiency of python and STL. //C++ #include <iostream> #include <string> #include <vector> #include <set> #include...
1
by: nimitsis | last post by:
Hello everyone I try to extend Python using a simple C wrapped module . This module includes embedded python code and it is product of Swig . The corresponding files follows : //main.c #include...
0
by: RLC | last post by:
Hello I am new to python SWIG. Recently I wrote a small program trying to import collada files(using colladadom) into python so I could use python cgkit to render them. However, during the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.