S1Q1 · Check If a Number Divides Two Other Numbers¶
⚡ Quick Reference
Function: is_both_divisible_by(a: int, b: int, c: int) -> bool
Core idea: check that both a and b are divisible by c.
Key rules:
- Both conditions must hold → use and
- Divisible by c → % c == 0
- c is guaranteed non-zero - no guard needed
Problem Statement¶
Problem
Write a function is_both_divisible_by(a, b, c) that returns True if both a and b are divisible by c, and False otherwise.
Examples:
a=10, b=15, c=5
True
a=10, b=13, c=5
False
a=19, b=15, c=5
False
Understanding the problem¶
Both conditions must be true simultaneously:
| Condition | Check |
|---|---|
a is divisible by c |
a % c == 0 |
b is divisible by c |
b % c == 0 |
Use and - if either fails, return False.
Tracing all three examples¶
a |
b |
c |
a % c |
b % c |
Result |
|---|---|---|---|---|---|
| 10 | 15 | 5 | 0 ✅ | 0 ✅ | True |
| 10 | 13 | 5 | 0 ✅ | 3 ❌ | False |
| 19 | 15 | 5 | 4 ❌ | 0 ✅ | False |
Solution approaches¶
Key takeaways¶
and for "both must hold"
Use and when every condition must be true. A single failure returns False. Short-circuits - if a % c != 0, the check for b is skipped entirely.
The trio: and / or / all()
and for two conditions both true. or for at least one true. all() for a variable number of conditions all true. Know when to use each.
Compare with Set 1 S1Q1 and Set 3 S1Q1
Set 3 S1Q1 used and (even AND second-last digit). Set 1 S1Q1 used or (even OR divisible by 5). This problem uses and again. Spotting the right operator is the key skill.