On Thu, Nov 22, 2012 at 10:02 AM, Prasad, Ramit <ramit.pra...@jpmorgan.com>wrote:
> Peter O'Doherty wrote: > > > > Hi list, > > Firstly, apologies for the low-level nature of this question - it's > > really quite basic but I don't seem to be able to solve it. > > > > I need to write a program that examines 3 variables x, y, z, and prints > > the largest odd number. I've tried all sorts of variations and this is > > the current version: > > > > x, y, z = 26, 15, 20 > > > > if x > y and x > z and x%2 != 0: > > print 'x is largest and odd' > > elif y > x and y > z and y%2 != 0: > > print 'y is largest and odd' > > elif z > x and z > y and z%2 != 0: > > print 'z is largest and odd' > > else: > > print 'no odd' > > > > > > A solution should be possible using only the simple operators and > > keywords above, no functions, lists or any other form of iteration. > > (It's from p. 16 of Introduction to Computation and Programming Using > > Python, and no, it's not "homework"!) > > > > The "smart" solution is eluding me so my inelegant solution would > figure out what is odd for each x,y, and z. Then compare only looking > at values that are odd. Your current if statement only works if > all values are odd, but not if the largest value is even and the > middle (or low) value is odd. The following code snippets should > give you an idea how to create the if statement so that it works > correctly > > useX = x % 2 # 1 is equivalent to True > if useX and ( ( useY and x > y ) or not useY ) # and for z: > print 'X-izard, use largest odd value attack!', x > > > ~Ramit > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Oops. I sent this to Ramit, not the list. Sorry Ramit Here is another idea: x, y, z = 26, 15, 20 largest = None if x %2: largest = x if y % 2: if y > largest: largest = y if z % 2: if z > largest: largest = z; if Largest: print "largest value is", largest else: print "no odd values" untested -- Joel Goldstick
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor