Problem overview
The method parity takes a non-negative integer n and returns the string "even" or "odd". A number is even when it is divisible by 2, and odd otherwise. For n = 12 the method returns "even", because 12 divided by 2 leaves nothing over.
Approach: the remainder operator
Swift's remainder operator % gives what is left over after a division. 12 % 2 is 0 and 13 % 2 is 1. A number is even exactly when dividing it by 2 leaves a remainder of 0, so the test is n % 2 == 0.
No second test is needed for odd numbers. If a number isn't even, it must be odd, so the method can return "even" when the test succeeds and "odd" in every other case.
Step by step
- Write the
if. The condition isn % 2 == 0. Read it as "the remainder ofndivided by 2 equals 0". The body can be empty at first. - Return
"even"inside theif. Thereturnends the method immediately. - Return
"odd"after theif. This line is reached only when theifcondition was false, because an even number has already left the method. That is why noelseis needed.
The solution in Swift
class Solution {
func parity(_ n: Int) -> String {
if n % 2 == 0 {
return "even"
}
return "odd"
}
}
Swift details worth knowing:
- Operator precedence.
%binds tighter than==, son % 2 == 0means(n % 2) == 0and needs no parentheses. - Return type.
-> Stringmeans every path must return a string. The last line guarantees that, since it returns"odd"whenever theifdoesn't. - Calling it. After
let s = Solution(),s.parity(12)returns"even"ands.parity(13)returns"odd". - Two equal signs. The condition compares with
==. A single=would be an assignment, and Swift rejects it inside anif. - A built-in alternative.
n.isMultiple(of: 2)is a method on integers that returnstruefor 4 andfalsefor 7. It reads closer to the idea, but this lesson practises the remainder operator.
Two equivalent shapes
The same logic can be written with an explicit else:
if n % 2 == 0 {
return "even"
} else {
return "odd"
}
Both versions behave identically for every input. The first has one less level of nesting, which is why the lesson uses it. Because an if that returns leaves the method, the lines below it act as the "otherwise" part without any extra keyword.
It also helps to see why, for a non-negative number, the remainder by 2 can only be 0 or 1. Every integer is either an exact multiple of 2 or one more than a multiple of 2, so there is no third case to handle. The method has exactly two outcomes, and one comparison is enough to separate them.
Edge cases and common mistakes
- Zero.
0 % 2is0, soparity(0)returns"even". That agrees with the definition: zero is divisible by 2. - One.
1 % 2is1, so the method returns"odd". - Negative numbers. The task promises
nis non-negative, but the choice of test matters if you reuse the method. In Swift the remainder takes the sign of the left operand, so-3 % 2is-1, not1. A version that testsn % 2 == 1would call -3 even. The test against0works for negative numbers as well, ands.parity(-3)returns"odd". - Typing
"Even"or"ODD". The returned strings must match exactly, in lowercase. - Dividing instead of taking the remainder.
n / 2is integer division, so7 / 2is3. It doesn't tell you whether anything was left over.
Testing
parity(12)returns"even".parity(13)returns"odd".parity(0)returns"even".parity(1)returns"odd".parity(1000000)returns"even".
