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).

  1. def f1(a):
      if a > 0:
         b = a
      else:
         b = abs(a)
      return b
    
  2. def f2(found):
      if found :
         equals = False
      else:
         equals = True
      return equals
    
  3. def f3(a, b):
      if a != b :
         equals = False
      else:
         equals = True
      return equals
    
  4. def f4(n):
      a = n%3
      if a == 0:
         b = 1
      elif a == 1:
         b = 2
      elif a == 2:
         b = 3
      return b
    
  5. We have been given the following function avoidable. Discuss the result returned for the calls avoidable(a, 'A') with the following current values for parameter a: 10, 11, 12, 13 and 14.

    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.