S1Q1 · Check Positive Odd or Negative Even¶
⚡ Quick Reference
Function: is_positive_odd_or_negative_even(n: int) -> bool
Core idea: two separate conditions - either must be true.
def is_positive_odd_or_negative_even(n: int) -> bool:
return (n > 0 and n % 2 != 0) or (n < 0 and n % 2 == 0)
Key rules:
- Positive odd: n > 0 and n % 2 != 0
- Negative even: n < 0 and n % 2 == 0
- 0 is neither positive nor negative → False
Problem Statement¶
Problem
Write a function is_positive_odd_or_negative_even(n) that returns True if n is a positive odd number or a negative even number, and False otherwise.
Examples:
3
True
-4
True
4
False
0
False
Tracing all examples¶
n |
n > 0 |
odd? | n < 0 |
even? | Result |
|---|---|---|---|---|---|
| 0 | ❌ | - | ❌ | - | False |
| 3 | ✅ | ✅ | ❌ | - | True (positive odd) |
| -4 | ❌ | - | ✅ | ✅ | True (negative even) |
| 4 | ✅ | ❌ | ❌ | - | False |
| -3 | ❌ | - | ✅ | ❌ | False |
n % 2 for negative numbers
In Python, n % 2 for negative numbers: -4 % 2 = 0 (even), -3 % 2 = 1 (odd). Python's modulo always returns a non-negative result when the divisor is positive, so n % 2 == 0 correctly identifies even numbers regardless of sign.
Solution approaches¶
Key takeaways¶
Two compound conditions joined by or
Each branch uses and to combine sign and parity checks. The two branches are joined by or - either qualifying is enough to return True.
Zero is neither positive nor negative
0 > 0 is False and 0 < 0 is False - so 0 fails both branches automatically. No special case needed.
Python % always returns non-negative
In Python, -4 % 2 == 0 and -3 % 2 == 1. The modulo result is always between 0 and divisor-1, so n % 2 == 0 reliably tests evenness for negative numbers too.