Skip to content

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:

Input
3
Output
True
Input
-4
Output
True
Input
4
Output
False
Input
0
Output
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

def is_positive_odd_or_negative_even(n: int) -> bool:
    return (n > 0 and n % 2 != 0) or (n < 0 and n % 2 == 0)
def is_positive_odd_or_negative_even(n: int) -> bool:
    is_positive_odd  = n > 0 and n % 2 != 0
    is_negative_even = n < 0 and n % 2 == 0
    return is_positive_odd or is_negative_even
def is_positive_odd_or_negative_even(n: int) -> bool:
    if n == 0:
        return False
    sign   = 1 if n > 0 else -1
    parity = n % 2   # 0 = even, 1 = odd
    # positive odd: sign=1, parity=1
    # negative even: sign=-1, parity=0
    return (sign == 1 and parity == 1) or (sign == -1 and parity == 0)
is_positive_odd_or_negative_even = lambda n: (
    (n > 0 and n % 2 != 0) or (n < 0 and n % 2 == 0)
)

Key takeaways

01

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.

02

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.

03

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.