12  Working with Functions

12.1 Task : Creating User Defined Functions

  • Use the package you created in the previous sessions.
  • Use the package locally (you don’t need to publish it to everyone)
  • Within this package, create a module named functions with the following within:
    • A function named myudffunction1.
    • This function will receive two parameters, a list of values X, and a value n to search for in the list, For example,X= [400,440,500,550,600,750,600,1200,1110,1000,950], n=600.
    • The function should return a dictionary with the value n and it’s index positions within the list, if it exists.
    • Obs. the above example would return {“600”:[4,6]}.
def myudffunction1(list,n):
    dict = {}
    positions = []
    for index in range(0,len(list)):
        if list[index] == n:
            positions.append(index)
    dict[str(n)]=positions
    return dict
  
BTC=[400,600,440,500,550,600,750,600,1200,1110,1000,950]
print(myudffunction1(BTC,600))
{'600': [1, 5, 7]}

12.2 Task : Creating Lambda Functions

  • Within the same module create:
    • Another function named mylambdafunction1.
      • This function will receive a single list as parameter: For example,BTC= [400,440,500,550,600,750,1200,1110,1000,950]. The function should return only a List with the values in only the even/(pares) positions in the list (not the indexes).
mylambdafunction1 = lambda x: [x[idx] for idx in decisions.odds(len(x))]
print(mylambdafunction1(BTC))
  • Within the same module create:
    • Another function named mylambdafunction2.
      • This function will receive a single list as parameter: For example,BTC= [400,440,500,550,600,750,1200,1110,1000,950].
      • The function should return only a List with the values in only the odd/(impares) positions in the list (not the indexes).
mylambdafunction2 = lambda x: [x[idx] for idx in decisions.evens(len(x)-1)]

print(mylambdafunction2(BTC))

12.3 Task : Creating Recursive Functions

  • Within the same module create:
    • Another function named myrecursivefunction1.
      • This function will receive a single parameter n and will compute and return the factorial of n.
def myrecursivefunction1(n):
  if n == 1:
    return 1
  else:
    return (n * myrecursivefunction1(n-1) )

for n in range(1,10):
  print("Factorial of",n,"is:",myrecursivefunction1(n))
Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
  • Another function named myrecursivefunction2.
  • This function will receive a single parameter n and will countdown from n to 0 (inclusive) jumping over one value at a time.
def myrecursivefunction2(n):
  if n < 1:
    return 0
  else:
    print(n)
    return (myrecursivefunction2(n-2) )

n=12
print("Countdown from",n)
Countdown from 12
myrecursivefunction2(n)
12
10
8
6
4
2
0
  • Another function named myrecursivefunction3.
  • This function will receive a single List of numbers and will compute and return the multplication of all of them.
def myrecursivefunction3(list):
    numelements = len(list)
    if numelements == 1:
      return list[0]
    else:
      last = list[numelements-1]
      list.pop(numelements-1)
      return ( last * myrecursivefunction3(list) )

list=[1,2,3,4,5,6,7]
print("Multiplication of",list)
Multiplication of [1, 2, 3, 4, 5, 6, 7]
print(myrecursivefunction3(list))
5040
  • Another function named myrecursivefunction4.
  • This function will receive a single List of numbers and will compute and return the multplication of all the odd/(impares) positions in the list (not the indexes).
def myrecursivefunction4(list):
    return ( myrecursivefunction3(list[0::2]) )

list=[1,2,3,4,5,6,7]
print("Multiplication of",list)
Multiplication of [1, 2, 3, 4, 5, 6, 7]
print(myrecursivefunction4(list))
105
  • Obs. It must be done using recursive functions (functions that call themselves).