Loop control and sentinel iterations [ref]
Problem
- Collect odd values from
[1, 2, 3, 4] by skipping even values with continue.
- Stop at the first odd value in
[2, 4, 7, 8] using break.
- Decrement
3 to 0 using while.
- Process
"12" plus one synthetic final "+" without indexing past the string.
- Define a function whose body does nothing using
pass.
Python solution
odds = []
for x in [1, 2, 3, 4]:
if x % 2 == 0:
continue
odds.append(x)
odds # [1, 3]
first_odd = None
for x in [2, 4, 7, 8]:
if x % 2:
first_odd = x
break
first_odd # 7
n = 3
while n:
n -= 1
n # 0
s = "12"
seen = []
for i in range(len(s) + 1):
seen.append(s[i] if i < len(s) else "+")
seen # ['1', '2', '+']
def placeholder():
pass
placeholder() # None
Links