473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('fi sh ')
print pets + 'python'

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

2.4983004554002 264e-010

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

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

laundry = Bin()
groceries = Bin()

laundry.add('sh irt')
groceries.add(' bananas')
groceries.add(' nuts')
laundry.add('so cks')
groceries.add(' pretzels')

print laundry.numitem s, laundry.content s
print groceries.numit ems, groceries.conte nts
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 1561
[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
2789
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 following sections contains a code snippet that someone, somewhere might find a little mysterious. Your goal is to sleuth through the code, identify what was expected versus what really happened, and to
12
2952
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, then explain what happened (again, in blindingly obvious terms). There's extra credit for giving a design insight as to why things are as they are.
5
1767
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. BACKGROUND: I've been playing around for a couple of days with parsing and
0
1026
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 amazes me every day. Elegant solutions just keep cropping up everywhere, it must be one of these brain-impedance matches I suppose :-) Yesterday I came up with this:
12
1659
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 elegance and flexibility. I thought I might mention a few of them. I'd like to hear what people think of my complaints, and I also like to hear the complaints of others. 1. many keywords (eg:try/except) are strictly imperative, and cannot be used in...
83
3604
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 <algorithm> using namespace std;
1
3192
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 "Python.h" #include <stdio.h> int main() { Py_Initialize();
0
1045
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 progressing, I got some problems. Every time I quit from Python, I get a segmentation fault, although the main program runs well. I suspect it is because I wrapped std::vector objects in C struct and I did not release the memory properly. Below is the...
0
8002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8338
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.