I\'m writing a django model that allows my site to have coupons.
Coupons can have three types: lifetime account voucher, certain period of months voucher, certain numb
if (self.months && (self.dollars || self.lifetime)) || (self.dollars && (self.months || self.lifetime)) || (self.lifetime && (self.dollars || self.months))
raise ValueError("Coupon can be valid for only one of: months, lifetime, or dollars")
Edit:
I did a quick circuit mimization using a Karnaugh map (http://en.wikipedia.org/wiki/Karnaugh_map). It ends up this is the smallest possible function with boolean logic:
if((self.months && self.dollars) || (self.dollars && self.lifetime) || (self.lifetime && self.months))
raise ValueError("Coupon can be valid for only one of: months, lifetime, or dollars")
Logically both my statements are equivelant but the second one is technically faster / more efficient.
Edit #2: If anyone is interested here is the K-Map
A | B | C | f(A, B, C)
----------------------
0 | 0 | 0 | 0
----------------------
0 | 0 | 1 | 0
----------------------
0 | 1 | 0 | 0
----------------------
0 | 1 | 1 | 1
----------------------
1 | 0 | 0 | 0
----------------------
1 | 0 | 1 | 1
----------------------
1 | 1 | 0 | 1
----------------------
1 | 1 | 1 | 1
Which Reduces to:
C\AB
-----------------
| 0 | 0 | 1 | 0 |
----------------- OR AB + BC + AC
| 0 | 1 | 1 | 1 |
-----------------