conditional expression sought
Mark McEahern
mark at mceahern.com
Fri Jan 30 09:25:49 EST 2004
Elaine Jackson wrote:
>Sorry to take so long but I wasn't sure what a "unit test" was (the other guy's
>post clarified it). Tell me if this isn't what you're looking for:
>
>falsies=[0,0.0,[],(),{},'',None]
>truies=[49,3.14,[1,2,3],(4,5,6),{7:8,9:10},'nonempty']
>
>def demo(A,B):
> print "If A is ",A
> print "and B is ",B
> print "then (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2]) = ",
> print (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2])
>
>A=[]
>from random import randint
>for i in range(3):
> A.append(bool(randint(0,1)))
>B=truies[0:3]
>demo(A,B)
>
>A=[False,False,False]
>B=falsies[0:3]
>demo(A,B)
>print "I would have liked this to be B[2] = ",B[2]
>
>
Try this:
def whatever(A, B):
"""I don't know what to call this function because it doesn't really
make sense to me, but, whatever..."""
c = zip(A, B)
last = len(c) - 1
for i, item in enumerate(c):
a, b = item
# If both items are true, return b.
if a and b:
return b
# If we're at the last item, return b.
if i == last:
return b
print whatever(A, B)
import unittest
class test(unittest.TestCase):
def testAllFalse(self):
A = [False, False, False]
B = [0, 0.0, []]
expected = []
actual = whatever(A, B)
self.assertEquals(actual, expected)
def testSomeTrue(self):
A = [0, 1, 0, 1, 0, 1]
B = ['a', 'b', 'c', 'd', 'e', 'f']
expected = 'b'
actual = whatever(A, B)
self.assertEquals(actual, expected)
unittest.main()
More information about the Python-list
mailing list