Conditional statement simplification¶
Analyse which of these functions with conditional statements can be simplified. Then simplify those that can be simplified (removing the conditional statement).
def f1(a): if a > 0: b = a else: b = abs(a) return b
def f2(found): if found : equals = False else: equals = True return equals
def f3(a, b): if a != b : equals = False else: equals = True return equals
def f4(n): a = n%3 if a == 0: b = 1 elif a == 1: b = 2 elif a == 2: b = 3 return b
We have been given the following function
avoidable. Discuss the result returned for the callsavoidable(a, 'A')with the following current values for parametera:10,11,12,13and14.Next, analyse whether it can be simplified or not and, if, so, simplify it by removing the conditional statement
def avoidable (a, letter): b = a%4 if b == 0: c = '' elif b == 1: c = letter elif b == 2: c = letter + letter else: c = b * letter return c
Solution
A solution is provided in file
avoidables.py file.