========
UPDATE 23-Jun-2013: After approving comments yesterday and tweaking my doctests, this exercise certainly must have been on my mind! I woke up this morning with a “why didn’t I do it this way before?!” thought. My 2nd revision to this exercise is posted as a reply below.
========
UPDATE 10-Mar-2013: OUCH! Mongo apologies for all the kind people who have commented!!!! Just received another one today. I’m just a budding blogger (ha – more like blogger-wannabe; i don’t deserve the “blogger” title by any stretch — yet!) as well as a terrifically overworked employee, so there’s little time to play these days. I will be getting back into it with a small diversion toward into some HTML parsing.
Thank you, thank you, thank you to the commenters! It’s really delightful you happened to discover my little post. Cheers to all.
— Original 20-Jan-2013 post below —
Over the weekend, I was at the MIT Press bookstore in Cambridge MA for “The CSound Book” and of course I had to ask if they had any Python books. I picked up a copy of John V. Guttag’s “Introduction to Computation and Programming Using Python“, Spring 2013 Edition.
Starting reading it, and will likely reveal various things from it as this blog continues. For today, however, here’s the “finger exercise” from section 2.2:
“Write a program that examines three variables–x, y, and z–and prints the largest odd number among them. If none of them are odd, it should print a message to that effect.”
Here’s my solution. It sure looks ugly to me, but I think it works.
I also added in the doctest module with verbose, as a kind of unit test, to validate against some outputs recorded in an IDLE session. According to the documention, “The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.”
Self-documenting AND testable — cool, yes?
POSTSCRIPT: One can google and find an approach like this, but I took the path of a person who hasn’t yet learned anything other than simple comparison operators, compound Boolean operations, and int, print, and the modulo operator %.
OH, and to be clear. because I wanted to have the doctest module, the solution is also implemented with a function definition. Output from running is below the source code.
# largestodd.py
#
# Example 2.2 taken from "Introduction to Computation and
# Programming Using Python", Spring 2013 Edition, p.16
# Exercise author: John V. Guttag
#
# Code author: mgh from "My Pythonic Year" blog at pythonicyear.com
def largestodd(x,y,z):
"""Compare three numbers and print largest odd,
or none is odd, a message to that effect.
>>> largestodd(1,3,5)
z is largest odd
>>> largestodd(1,5,3)
y is largest odd
>>> largestodd(7,5,3)
x is largest odd
>>> largestodd(2,5,3)
y is largest odd
>>> largestodd(2,5,7)
z is largest odd
>>> largestodd(5,2,7)
z is largest odd
>>> largestodd(7,2,5)
x is largest odd
>>> largestodd(7,9,2)
y is largest odd
>>> largestodd(11,9,2)
x is largest odd
>>> largestodd(11,2,2)
x is largest odd
>>> largestodd(2,3,2)
y is largest odd
>>> largestodd(2,2,23)
z is largest odd
>>> largestodd(2,2,2)
NONE of x y z are odd
"""
if x%2 != 0 :
if y%2 != 0 :
if z%2 !=0 :
# all three are odd
if x > y and x > z :
print 'x is largest odd'
elif y > z:
print 'y is largest odd'
else:
print 'z is largest odd'
else: # only x and y are odd
if x > y :
print 'x is largest odd'
else:
print 'y is largest odd'
elif z%2 != 0:
# both x and z are odd
if x > z :
print 'x is largest odd'
else:
print 'z is largest odd'
else:
#x odd, but neither y nor z
print 'x is largest odd'
else:
#x not odd, test y and z
if y%2 != 0:
# y is odd
if z%2 != 0:
# y and z are odd
if y > z:
print 'y is largest odd'
else:
print 'z is largest odd'
else:
print 'y is largest odd'
else:
#y not odd
if z%2 != 0:
print 'z is largest odd'
else:
print 'NONE of x y z are odd'
return
if __name__ == "__main__":
import doctest
doctest.testmod()
Output when run:
>>>
Trying:
largestodd(1,3,5)
Expecting:
z is largest odd
ok
Trying:
largestodd(1,5,3)
Expecting:
y is largest odd
ok
Trying:
largestodd(7,5,3)
Expecting:
x is largest odd
ok
Trying:
largestodd(2,5,3)
Expecting:
y is largest odd
ok
Trying:
largestodd(2,5,7)
Expecting:
z is largest odd
ok
Trying:
largestodd(5,2,7)
Expecting:
z is largest odd
ok
Trying:
largestodd(7,2,5)
Expecting:
x is largest odd
ok
Trying:
largestodd(7,9,2)
Expecting:
y is largest odd
ok
Trying:
largestodd(11,9,2)
Expecting:
x is largest odd
ok
Trying:
largestodd(11,2,2)
Expecting:
x is largest odd
ok
Trying:
largestodd(2,3,2)
Expecting:
y is largest odd
ok
Trying:
largestodd(2,2,23)
Expecting:
z is largest odd
ok
Trying:
largestodd(2,2,2)
Expecting:
NONE of x y z are odd
ok
1 items had no tests:
__main__
1 items passed all tests:
13 tests in __main__.largestodd
13 tests in 2 items.
13 passed and 0 failed.
Test passed.
>>>