Ho w to pass a v ariable n u mber of arg u ments to a f u nction ? P - - PowerPoint PPT Presentation

ho w to pass a v ariable n u mber of arg u ments to a f u
SMART_READER_LITE
LIVE PREVIEW

Ho w to pass a v ariable n u mber of arg u ments to a f u nction ? P - - PowerPoint PPT Presentation

Ho w to pass a v ariable n u mber of arg u ments to a f u nction ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran Arg u ment t y pes There are t w o t y pes of arg u ments :


slide-1
SLIDE 1

How to pass a variable number of arguments to a function?

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

Kirill Smirnov

Data Science Consultant, Altran

slide-2
SLIDE 2

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument types

There are two types of arguments:

slide-3
SLIDE 3

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument types

There are two types of arguments: positional arguments

slide-4
SLIDE 4

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument types

There are two types of arguments: positional arguments keyword arguments

slide-5
SLIDE 5

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument types

There are two types of arguments: positional arguments keyword arguments

slide-6
SLIDE 6

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Positional arguments

def func_with_pos_args(arg1, arg2): pass def multiply(x, y): return x * y multiply(2, 3) 6

slide-7
SLIDE 7

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

*args

def func_with_var_pos_args(*args): pass func_with_var_pos_args(1, 2, 'hello')

slide-8
SLIDE 8

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

*args

def func_with_var_pos_args(*args): print(args) func_with_var_pos_args(1, 2, 'hello') (1, 2, 'hello')

slide-9
SLIDE 9

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

*args

def func_with_var_pos_args(*args): for arg in args: print(arg) func_with_var_pos_args(1, 2, 'hello') 1 2 'hello'

slide-10
SLIDE 10

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Redefining multiply()

def multiply(*args): result = 1 for arg in args: result = result * arg return result multiply(1, 2, 3): 6 multiply(1, 2, 3, 4) 24

slide-11
SLIDE 11

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Redefining multiply()

def multiply(*nums): result = 1 for num in nums: result = result * num return result multiply(1, 2, 3): 6 multiply(1, 2, 3, 4) 24

slide-12
SLIDE 12

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of single asterisk *

def multiply(num1, num2, num3): return num1 * num2 * num3 multiply(1, 2, 3) 6

slide-13
SLIDE 13

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of single asterisk *

def multiply(num1, num2, num3): return num1 * num2 * num3 nums = (2, 3, 4) multiply(*nums) 24 nums = [2, 3] multiply(*nums, 4) 24

slide-14
SLIDE 14

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of single asterisk *

def multiply(*args): result = 1 for arg in args: result = result * num return result nums = (2, 3, 4, 5) multiply(*nums) 120

slide-15
SLIDE 15

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument types

There are two types of arguments: positional arguments keyword arguments

slide-16
SLIDE 16

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Keyword arguments

def func_with_kwargs(arg1=1, arg2=2): def multiply(x=1, y=2): print(str(x) + ' : ' + str(y)) multiply(2, 3) 2 : 3 multiply() 1 : 2

slide-17
SLIDE 17

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Keyword arguments

def func_with_kwargs(arg1=1, arg2=2): def multiply(x=1, y=2): print(str(x) + " : " + str(y)) multiply(y=5, x=3) 3 : 5

slide-18
SLIDE 18

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

**kwargs

kwargs - keyword arguments

def func_with_var_kwargs(**kwargs): print(kwargs) func_with_var_kwargs(arg1=1, arg2=2, arg3=3) {arg1: 1, arg2: 2, arg3: 3} func_with_var_kwargs(1, arg2=2, arg3=3) TypeError

slide-19
SLIDE 19

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Redefining multiply()

def multiply_kwargs(**kwargs): result = 1 for (key, value) in kwargs.items(): print(key + ' = ' + str(value)) result = result * value return result def multiply(*args): result = 1 for arg in args: result = result * arg return result

slide-20
SLIDE 20

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Calling multiply_kwargs()

multiply_kwargs(num1=1, num2=2, num3=3, num4=4) num1 = 1 num2 = 2 num3 = 3 num4 = 4 24

slide-21
SLIDE 21

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3): print('num1 = ' + str(num1)) print('num2 = ' + str(num2)) print('num3 = ' + str(num3)) return num1 * num2 * num3 multiply() num1 = 1 num2 = 2 num3 = 3 6

slide-22
SLIDE 22

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3): print('num1 = ' + str(num1)) print('num2 = ' + str(num2)) print('num3 = ' + str(num3)) return num1 * num2 * num3 nums = {'num1': 10, 'num2': 20, 'num3': 30 multiply(**nums) num1 = 10 num2 = 20 num3 = 30 6000

slide-23
SLIDE 23

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3): print('num1 = ' + str(num1)) print('num2 = ' + str(num2)) print('num3 = ' + str(num3)) return num1 * num2 * num3 nums = {'num1': 10, 'num3': 30} multiply(**nums) num1 = 10 num2 = 2 num3 = 30 600

slide-24
SLIDE 24

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3): print('num1 = ' + str(num1)) print('num2 = ' + str(num2)) print('num3 = ' + str(num3)) return num1 * num2 * num3 nums = {'NUM10': 1, 'num2': 2, 'num3': 3} multiply(**nums) TypeError

slide-25
SLIDE 25

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Another use of double asterisk **

def multiply_kwargs(**kwargs): result = 1 for (key, value) in kwargs.items(): print(key + ' = ' + str(value)) result = result * value return result nums = { 'num1': 2, 'num2': 3, 'num3': 4, 'num4': 5 } multiply_kwargs(**nums) num1 = 2 num2 = 3 num3 = 4 num4 = 5 120

slide-26
SLIDE 26

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument order

def func( ):

slide-27
SLIDE 27

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument order

def func(arg1, arg2, ):

arg1 , arg2 - positional arguments

slide-28
SLIDE 28

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument order

def func(arg1, arg2, *args, ):

arg1 , arg2 - positional arguments *args - positional arguments of variable size

slide-29
SLIDE 29

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument order

def func(arg1, arg2, *args, kwarg1, kwarg2, ):

arg1 , arg2 - positional arguments *args - positional arguments of variable size kwarg1 , kwarg2 - keyword arguments

slide-30
SLIDE 30

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Argument order

def func(arg1, arg2, *args, kwarg1, kwarg2, **kwargs):

arg1 , arg2 - positional arguments *args - positional arguments of variable size kwarg1 , kwarg2 - keyword arguments **kwargs - keyword arguments of variable size

def func(arg1, arg2, *args): def func(arg1, arg2, **kwargs): def func(*args, **kwargs):

slide-31
SLIDE 31

Let's practice!

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

slide-32
SLIDE 32

What is a lambda expression?

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

Kirill Smirnov

Data Science Consultant, Altran

slide-33
SLIDE 33

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...)

slide-34
SLIDE 34

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda

slide-35
SLIDE 35

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda arg1, arg2, ...:

slide-36
SLIDE 36

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...) lambda x: x**2 squared = lambda x: x**2 squared(4) 16

4 → x → x**2 → 16

slide-37
SLIDE 37

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...) power = lambda x, y: x**y power(2, 3) 8

2, 3 → x, y → x**y → 8

slide-38
SLIDE 38

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Missing argument

power = lambda x, y: x**y power(2) TypeError

slide-39
SLIDE 39

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Comparison to normal function definition

squared_lambda = lambda x: x**2 def squared_normal(x): return x**2

slide-40
SLIDE 40

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Comparison to normal function definition

lambda def

slide-41
SLIDE 41

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Comparison to normal function definition

squared_lambda = lambda def squared_normal

slide-42
SLIDE 42

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Comparison to normal function definition

squared_lambda = lambda x: def squared_normal(x):

slide-43
SLIDE 43

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Comparison to normal function definition

squared_lambda = lambda x: x**2 def squared_normal(x): return x**2 squared_lambda(3) 9 squared_normal(3) 9

slide-44
SLIDE 44

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Passing lambda function as an argument

def function_with_callback(num, callback_function): return callback(num)

callback_function(arg) - a function with one argument

def squared_normal(x): return x**2 function_with_callback(2, squared_normal) 4

slide-45
SLIDE 45

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Passing lambda function as an argument

def function_with_callback(num, callback_function): return callback(num)

callback_function(arg) - a function with one argument

  • -> def squared_normal(x): <--
  • -> return x**2 <--
  • -> function_with_callback(2, squared_normal) <--

4

slide-46
SLIDE 46

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Passing lambda function as an argument

def function_with_callback(num, callback_function): return callback(num)

callback_function(arg) - a function with one argument

function_with_callback(2, lambda x: x**2) 4

slide-47
SLIDE 47

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...)

slide-48
SLIDE 48

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short (anonymous) function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...) squared = lambda x: x**2 squared(3) 9

slide-49
SLIDE 49

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

lambda expression/function - is a short (anonymous) function having the following syntax:

lambda arg1, arg2, ...: expression(arg1, arg2, ...) (lambda x: x**2)(3) 9

slide-50
SLIDE 50

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Ternary operator

def odd_or_even(num): if num % 2 == 0: return 'even' else: return 'odd'

  • dd_or_even(3)

'odd'

  • dd_or_even(6)

'even'

slide-51
SLIDE 51

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Ternary operator

def odd_or_even(num): return 'even' if num % 2 == 0 else 'odd'

  • dd_or_even(3)

'odd'

  • dd_or_even(6)

'even'

slide-52
SLIDE 52

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Ternary operator

  • dd_or_even = lambda num: 'even' if num % 2 == 0 else 'odd'
  • dd_or_even(3)

'odd'

  • dd_or_even(6)

'even'

slide-53
SLIDE 53

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Practical use

Use lambda expressions when it is really necessary! within function bodies to perform a small task as callbacks

slide-54
SLIDE 54

Let's practice!

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

slide-55
SLIDE 55

What are the functions map(), filter(), reduce()?

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

Kirill Smirnov

Data Science Consultant, Altran

slide-56
SLIDE 56

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map()

map( )

slide-57
SLIDE 57

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map()

map( Iterable1, Iterable2, ...)

Iterables: [1, 2, 3, 4, 5] , [10, 20, 30, 40, 50] , ...

slide-58
SLIDE 58

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map()

map(function(x1, x2, ...), Iterable1, Iterable2, ...)

Iterables: [1, 2, 3, 4, 5] , [10, 20, 30, 40, 50] , ...

1 , 10 , ... → function(1, 10, ...) → new object 2 , 20 , ... → function(2, 20, ...) → new object 3 , 30 , ... → function(3, 30, ...) → new object 4 , 40 , ... → function(4, 40, ...) → new object 5 , 50 , ... → function(5, 50, ...) → new object

slide-59
SLIDE 59

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x): return x**2 squares = map(squared, nums) print(squares) <map object at 0x7fdbe4ab3da0>

squares is iterable

for square in squares: print(square) 1 4 9 16 25

slide-60
SLIDE 60

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x): return x**2 squares = map(squared, nums) print(squares) <map object at 0x7fdbe4ab3da0>

squares is Iterable

list(squares) [1, 4, 9, 16, 25]

slide-61
SLIDE 61

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x): return x**2 squares = map(squared, nums) print(squares) <map object at 0x7fdbe4ab3da0>

squares is Iterator

next(squares) 1 next(squares) 4

slide-62
SLIDE 62

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map() with lambda expressions

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x): return x**2 squares = map(squared, nums) list(squares) [1, 4, 9, 16, 25] nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums) list(squares) [1, 4, 9, 16, 25]

slide-63
SLIDE 63

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

map() with multiple Iterables

nums1 = [1, 2, 3, 4, 5] nums2 = [10, 20, 30, 40, 50]

The task is to get: [1*10, 2*20, 3*30, 4*40, 5*50] = [10, 40, 90, 160, 250]

mult = map(lambda x, y: x*y, nums1, nums2) list(mult) [10, 40, 90, 160, 250]

slide-64
SLIDE 64

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter()

filter( )

slide-65
SLIDE 65

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter()

filter( Iterable)

Iterable: [1, 2, 3, 4, 5]

slide-66
SLIDE 66

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter()

filter(function(x), Iterable)

Iterable: [1, 2, 3, 4, 5]

1 → function(1) → True → 1 is kept 2 → function(2) → False → 2 is rejected 3 → function(3) → True → 3 is kept 4 → function(4) → False → 4 is rejected 5 → function(5) → True → 5 is kept

slide-67
SLIDE 67

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x): return x > 0 fobj = filter(positive, nums) print(fobj) <filter object at 0x7f196d378d68>

fobj is Iterable

for item in fobj: print(item) 1 2 3

slide-68
SLIDE 68

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x): return x > 0 fobj = filter(positive, nums) print(fobj) <filter object at 0x7f196d378d68>

fobj is Iterable

list(fobj) [1, 2, 3]

slide-69
SLIDE 69

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x): return x > 0 fobj = filter(positive, nums) print(fobj) <filter object at 0x7f196d378d68>

fobj is Iterator

next(fobj) 1 next(fobj) 4

slide-70
SLIDE 70

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

filter() with lambda expressions

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x): return x > 0 fobj = filter(positive, nums) list(fobj) [1, 2, 3] nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

fobj = filter(lambda x: x > 0, nums) list(fobj) [1, 2, 3]

slide-71
SLIDE 71

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce()

from functools import reduce reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → new object of the same

type as the content

slide-72
SLIDE 72

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce()

from functools import reduce reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → new object of the same

type as the content

slide-73
SLIDE 73

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce()

from functools import reduce reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → new object of the same

type as the content

slide-74
SLIDE 74

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce()

from functools import reduce reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → new object of the same

type as the content

slide-75
SLIDE 75

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce()

from functools import reduce reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → new object of the same

type as the content

slide-76
SLIDE 76

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce() example

nums = [8, 4, 5, 1, 9]

The task is to get: 1 - minimum

def smallest(x, y): if x < y: return x else: return y reduce(smallest, nums) 1

smallest(8, 4) → 4 smallest(4, 5) → 4 smallest(4, 1) → 1 smallest(1, 9) → 1 - nal result

slide-77
SLIDE 77

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

reduce() with lambda expressions

nums = [8, 1, 4, 2, 9]

The task is to get: 1 - minimum

def smallest(x, y): if x < y: return x else: return y reduce(smallest, nums) 1 nums = [8, 1, 4, 2, 9]

The task is to get: 1 - minimum

reduce(lambda x, y: x if x < y else y, nums) 1

slide-78
SLIDE 78

Let's practice!

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

slide-79
SLIDE 79

What is recursion?

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

Kirill Smirnov

Data Science Consultant, Altran

slide-80
SLIDE 80

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Definition

Recursion is the process of dening a problem in terms of itself Recursion is a process in which a function calls itself as a subroutine

slide-81
SLIDE 81

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Example: Factorial n!

n! = n ⋅ (n − 1) ⋅ (n − 2) ⋅ ... ⋅ 1 n = 4: 4! = 4 ⋅ 3 ⋅ 2 ⋅ 1

4! = 24

slide-82
SLIDE 82

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Factorial - Iterative Approach

n! = n ⋅ (n − 1) ⋅ (n − 2) ⋅ ... ⋅ 1 = = 1 ⋅ 2 ⋅ 3 ⋅ ... ⋅ n

Iterative solution:

# iterative factorial def fact_iter(n): result = 1 # looping over numbers from 1 to n for num in range(1, n+1) result = num * result return result

n = 4 :

result = 1

  • 1. result = 1 * result (1) = 1
  • 2. result = 2 * result (1) = 2
  • 3. result = 3 * result (2) = 6
  • 4. result = 4 * result (4) = 24

4! = 1 ⋅ 2 ⋅ 3 ⋅ 4 = 24

slide-83
SLIDE 83

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Factorial - Recursive Approach

n! = n ⋅ (n − 1)!

def fact_rec(n): return n * fact_rec(n-1)

What's wrong with that code?

fact_rec(4) RecursionError

We must dene a base case!

n! = n ⋅ (n − 1) ⋅ (n − 2) ⋅ ... ⋅ 1

A stopping criterion / base case: 1! = 1

def fact_rec(n): if n == 1: return 1 return n * fact_rec(n-1) fact_rec(4) 24

slide-84
SLIDE 84

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Wrapping Up

Recursive functions have two main components: a recursive call to a smaller problem of itself a base case that prevents an innite calling

slide-85
SLIDE 85

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Example - Decision Trees

slide-86
SLIDE 86

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Traversing a Decision Tree

x - a new sample (x ,x )

# Pseudo algorithm for finding out the category: category = pred(node, x): # Check if there is a split if node.hasSplitting: # Check which child node to take if node.goToLeftChild(x): return pred(node.leftChild, x) if node.goToRightChild(x): return pred(node.rightChild, x)

1 2

slide-87
SLIDE 87

PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Traversing a Decision Tree

x - a new sample (x ,x )

# Pseudo algorithm for finding out the category: category = pred(node, x): # Check if there is a split if node.hasSplitting: # Check which child node to take if node.goToLeftChild(x): return pred(node.leftChild, x) if node.goToRightChild(x): return pred(node.rightChild, x) # Returning the category return node.category

1 2

slide-88
SLIDE 88

Let's practice!

P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON