Advanced Python 2 | Inbuilt Math Functions

Series: Advanced Python

Advanced Python 2 | Inbuilt Math Functions

There are 67 inbuilt functions in python and we don’t have to know all of them in this case. In this part, we are ought to know 4 inbuilt math functions (not in the math pkg). Sometimes, if we write codes using these functions, our codes are going to be really simple and quite easy to understand. The 4 inbuilt functions that we have to know is,

  • abs: returns the absolute value of the given value
  • max: returns the maximum value in a sequence
  • min: returns the minimum value in a sequence
  • sum: returns the summation of all values in a sequence

Question 1. Bound a value

Let’s say you are working at an Internet of Things (IoT) company and want to make sure your sensors return only valid readings. You are given the operating min and max.

Write a function that does nothing if the value is in the range. If below, return min. If above, return max. Try to solve the logic as a composable collection of built-in functions. DON’T solve it with if-elif-else logic.

Question 2. Create a Fibonacci sequence

Again, we have to construct a Fibonacci sequence, which is a series in which each number is the sum of the two preceding numbers. We don’t use recursion in this case, and instead, we must use while loop in our realization code.

Question 3. Greatest Common Divisor

The greatest common divisor (GCD), also called the greatest common factor, of two numbers is the largest natural number d that divides both numbers without a remainder. Let’s code up the Euclidean algorithm (one of the oldest algorithms in common use) to find the GCD.

Do not use recursion!

Question 4. Find the largest sum with a constraint

Given a list of integers, find the largest sum with the constraint that you can not add two adjacent numbers. This is similar to Reinforcement Learning. The goal of Reinforcement Learning is maximizing the sum of cumulative rewards under constraints.