Skip to content
Snippets Groups Projects
Commit 01a64b53 authored by Romain Vuillemot's avatar Romain Vuillemot
Browse files

Update 02-recursion-exercices.ipynb

parent 1e50f830
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:dadeb4a2 tags:
# UE5 Fundamentals of Algorithms
## Exercices
### Ecole Centrale de Lyon, Bachelor of Science in Data Science for Responsible Business
#### [Romain Vuillemot](https://romain.vuillemot.net/)
Before you turn this problem in:
- make sure everything runs as expected.
- first, **restart the kernel** (in the menubar, select Kernel$\rightarrow$Restart)
- then **run all cells** (in the menubar, select Cell$\rightarrow$Run All).
- make sure you fill in any place that says `YOUR CODE HERE` or "YOUR ANSWER HERE"
- remove `raise NotImplementedError()` to get started with your answer
- bonus points (at the end of this notebook) are optionals
- write your name (and collaborators as a list if any) below:
%% Cell type:code id:7c45a2a5 tags:
``` python
ID = ""
COLLABORATORS_ID = []
```
%% Cell type:markdown id:f14bc602 tags:
---
%% Cell type:markdown id:a4e4fad3 tags: %% Cell type:markdown id:a4e4fad3 tags:
# Recursion # Recursion
%% Cell type:markdown id:a8adef9b tags: %% Cell type:markdown id:a8adef9b tags:
--- ---
%% Cell type:markdown id:568202fd tags: %% Cell type:markdown id:568202fd tags:
### Exercise 0: Find the maximum value in a list (iterative) ### Exercise 0: Find the maximum value in a list (iterative)
Write a function `find_maximum_iterative` that takes a list of numbers as input and returns the maximum value in the list. For this question, you are not allowed to use built-in functions like `max()``. Write a function `find_maximum_iterative` that takes a list of numbers as input and returns the maximum value in the list. For this question, you are not allowed to use built-in functions like `max()``.
%% Cell type:code id:431fe8c1-91d1-40f3-a7a4-f4a3770a4a01 tags: %% Cell type:code id:431fe8c1-91d1-40f3-a7a4-f4a3770a4a01 tags:
``` python ``` python
def find_maximum_iterative(L): def find_maximum_iterative(L):
### BEGIN SOLUTION # YOUR CODE HERE
if len(L) == 0: raise NotImplementedError()
raise ValueError("The list is empty.")
max_val = L[0]
for num in L:
if num > max_val:
max_val = num
return max_val
### END SOLUTION
``` ```
%% Cell type:code id:f6baae3c-3660-4add-ab4b-48016cba3030 tags: %% Cell type:code id:f6baae3c-3660-4add-ab4b-48016cba3030 tags:
``` python ``` python
find_maximum_iterative([1, 3, 5, 7, 9]) find_maximum_iterative([1, 3, 5, 7, 9])
``` ```
%% Output
9
%% Cell type:code id:e68b3e9a-418f-4950-9f27-18bb0fe90794 tags: %% Cell type:code id:e68b3e9a-418f-4950-9f27-18bb0fe90794 tags:
``` python ``` python
assert find_maximum_iterative([1, 3, 5, 7, 9]) == 9 assert find_maximum_iterative([1, 3, 5, 7, 9]) == 9
assert find_maximum_iterative([-1, -5, -3]) == -1 assert find_maximum_iterative([-1, -5, -3]) == -1
assert find_maximum_iterative([42]) == 42 assert find_maximum_iterative([42]) == 42
assert find_maximum_iterative([4, 8, 8, 2, 7]) == 8 assert find_maximum_iterative([4, 8, 8, 2, 7]) == 8
assert find_maximum_iterative([-10, -5, -8, -2, -7]) == -2 assert find_maximum_iterative([-10, -5, -8, -2, -7]) == -2
``` ```
%% Cell type:markdown id:612dc873-419b-42c5-be36-0accd03ffa79 tags: %% Cell type:markdown id:612dc873-419b-42c5-be36-0accd03ffa79 tags:
### Exercise 1: Find the maximum value in a list (recursive) ### Exercise 1: Find the maximum value in a list (recursive)
Write a recursive version of the previous function; you may use the `max()` function for the recursive call. Write a recursive version of the previous function; you may use the `max()` function for the recursive call.
%% Cell type:code id:07668fd8 tags: %% Cell type:code id:07668fd8 tags:
``` python ``` python
def find_maximum_recursive(L): def find_maximum_recursive(L):
### BEGIN SOLUTION # YOUR CODE HERE
if len(L) == 1: raise NotImplementedError()
return L[0]
else:
return max(L[0], find_maximum_recursive(L[1:]))
### END SOLUTION
``` ```
%% Cell type:code id:f9784710-4b2b-434c-bc47-da46fa410749 tags: %% Cell type:code id:f9784710-4b2b-434c-bc47-da46fa410749 tags:
``` python ``` python
find_maximum_recursive([1, 3, 5, 7, 9]) find_maximum_recursive([1, 3, 5, 7, 9])
``` ```
%% Output
9
%% Cell type:code id:9b0161f8-0539-4e5e-921c-1886e61c0783 tags: %% Cell type:code id:9b0161f8-0539-4e5e-921c-1886e61c0783 tags:
``` python ``` python
assert find_maximum_iterative([-10, -5, -8, -2, -7]) == find_maximum_recursive([-10, -5, -8, -2, -7]) assert find_maximum_iterative([-10, -5, -8, -2, -7]) == find_maximum_recursive([-10, -5, -8, -2, -7])
``` ```
%% Cell type:markdown id:005efd41-baa1-4505-b80e-3644d61ea094 tags: %% Cell type:markdown id:005efd41-baa1-4505-b80e-3644d61ea094 tags:
### Exercise 2: Sum of digits ### Exercise 2: Sum of digits
%% Cell type:code id:de424156-0b9b-41d0-8e38-ce335f35cec0 tags: %% Cell type:code id:de424156-0b9b-41d0-8e38-ce335f35cec0 tags:
``` python ``` python
def sum_of_digits(n): def sum_of_digits(n):
### BEGIN SOLUTION # YOUR CODE HERE
if n < 10: raise NotImplementedError()
return n
else:
return n % 10 + sum_of_digits(n // 10)
### END SOLUTION
``` ```
%% Cell type:code id:cec0caca-cb2c-4e4d-b004-27b3cf2ff611 tags: %% Cell type:code id:cec0caca-cb2c-4e4d-b004-27b3cf2ff611 tags:
``` python ``` python
sum_of_digits(10) sum_of_digits(10)
``` ```
%% Output
1
%% Cell type:code id:bf276ca2-48ed-4e87-80f2-776f54b7062b tags: %% Cell type:code id:bf276ca2-48ed-4e87-80f2-776f54b7062b tags:
``` python ``` python
assert sum_of_digits(10) == sum_of_digits(100000) assert sum_of_digits(10) == sum_of_digits(100000)
``` ```
%% Cell type:markdown id:e2de630a-f9bd-4d45-959b-430e34cc9044 tags: %% Cell type:markdown id:e2de630a-f9bd-4d45-959b-430e34cc9044 tags:
### Exercise 3: Calculate the power ### Exercise 3: Calculate the power
%% Cell type:code id:abca03a0-7bcd-4ee6-b109-ff2f2da52bb6 tags: %% Cell type:code id:abca03a0-7bcd-4ee6-b109-ff2f2da52bb6 tags:
``` python ``` python
def power(base, exponent): def power(base, exponent):
### BEGIN SOLUTION # YOUR CODE HERE
if exponent == 0: raise NotImplementedError()
return 1
else:
return base * power(base, exponent - 1)
### END SOLUTION
``` ```
%% Cell type:code id:abddd3b1-f75f-4cb6-a09e-54eed489c5b0 tags: %% Cell type:code id:abddd3b1-f75f-4cb6-a09e-54eed489c5b0 tags:
``` python ``` python
power(2, 10) power(2, 10)
``` ```
%% Output
1024
%% Cell type:code id:8a6605fe-4f6f-45de-84a3-7601e2e2e6f6 tags: %% Cell type:code id:8a6605fe-4f6f-45de-84a3-7601e2e2e6f6 tags:
``` python ``` python
assert power(2, 10) == 1024 assert power(2, 10) == 1024
``` ```
%% Cell type:markdown id:715100d3-fc21-49b9-a66d-b5243b4a279d tags: %% Cell type:markdown id:715100d3-fc21-49b9-a66d-b5243b4a279d tags:
### Exercise 4: Reverse a string ### Exercise 4: Reverse a string
%% Cell type:code id:ddc9826a-0863-4777-a08d-81b66652b5a5 tags: %% Cell type:code id:ddc9826a-0863-4777-a08d-81b66652b5a5 tags:
``` python ``` python
def reverse_string(s): def reverse_string(s):
### BEGIN SOLUTION # YOUR CODE HERE
if len(s) == 0: raise NotImplementedError()
return s
else:
return reverse_string(s[1:]) + s[0]
### END SOLUTION
``` ```
%% Cell type:code id:13acad7e-d03c-4ea6-ad86-baf02e0910eb tags: %% Cell type:code id:13acad7e-d03c-4ea6-ad86-baf02e0910eb tags:
``` python ``` python
reverse_string("Romain") reverse_string("Romain")
``` ```
%% Output
'niamoR'
%% Cell type:code id:453c8e04-6cd9-4581-a206-dd479b6115cd tags: %% Cell type:code id:453c8e04-6cd9-4581-a206-dd479b6115cd tags:
``` python ``` python
assert reverse_string("") == "" assert reverse_string("") == ""
assert reverse_string("AA") == "AA" assert reverse_string("AA") == "AA"
assert reverse_string("ABC") == "CBA" assert reverse_string("ABC") == "CBA"
``` ```
%% Cell type:markdown id:a0c5c115-1f3c-43f3-8098-930c707cd863 tags: %% Cell type:markdown id:a0c5c115-1f3c-43f3-8098-930c707cd863 tags:
def iterative_reverse_string(s): def iterative_reverse_string(s):
reversed_str = "" reversed_str = ""
for char in s: for char in s:
reversed_str = char + reversed_str reversed_str = char + reversed_str
return reversed_str return reversed_str
%% Cell type:code id:43e10b0c tags: %% Cell type:code id:43e10b0c tags:
``` python ``` python
``` ```
%% Cell type:markdown id:5ab8f3c9-ddee-45ab-a013-fdd67d9853e0 tags: %% Cell type:markdown id:5ab8f3c9-ddee-45ab-a013-fdd67d9853e0 tags:
### Example 5: convert an interative suite into a recursive tail and non-tail function ### Example 5: convert an interative suite into a recursive tail and non-tail function
%% Cell type:code id:219add7f tags: %% Cell type:code id:219add7f tags:
``` python ``` python
def sequence(n): def sequence(n):
u = 1 u = 1
while n > 0: while n > 0:
u = 2 * u + 1 u = 2 * u + 1
n -= 1 n -= 1
return u return u
``` ```
%% Cell type:code id:0787df24-8234-4286-b910-5f9e456dd279 tags: %% Cell type:code id:0787df24-8234-4286-b910-5f9e456dd279 tags:
``` python ``` python
print("The result is {}".format(sequence(3))) print("The result is {}".format(sequence(3)))
``` ```
%% Output
The result is 15
%% Cell type:code id:9c17cf1b-6d05-4589-af2b-c05cfcc33202 tags: %% Cell type:code id:9c17cf1b-6d05-4589-af2b-c05cfcc33202 tags:
``` python ``` python
def sequence_recursive_tail(n): def sequence_recursive_tail(n):
### BEGIN SOLUTION # YOUR CODE HERE
if n == 0: raise NotImplementedError()
return 1
return 2 * sequence_recursive_tail(n - 1) + 1
### END SOLUTION
``` ```
%% Cell type:code id:43507c43-de61-414d-b389-c0a771ad9e0c tags: %% Cell type:code id:43507c43-de61-414d-b389-c0a771ad9e0c tags:
``` python ``` python
print("The result is still {}".format(sequence_recursive_tail(3))) print("The result is still {}".format(sequence_recursive_tail(3)))
``` ```
%% Output
The result is still 15
%% Cell type:code id:921939d8 tags: %% Cell type:code id:921939d8 tags:
``` python ``` python
def sequence_recursive_non_tail(n, acc=1): def sequence_recursive_non_tail(n, acc=1):
### BEGIN SOLUTION # YOUR CODE HERE
if n == 0: raise NotImplementedError()
return acc
return sequence_recursive_non_tail(n - 1, 2 * acc + 1)
### END SOLUTION
``` ```
%% Cell type:code id:b754bf67-3d8d-42d9-a7ca-c43e2995837d tags: %% Cell type:code id:b754bf67-3d8d-42d9-a7ca-c43e2995837d tags:
``` python ``` python
print("The result is still {}".format(sequence_recursive_non_tail(3))) print("The result is still {}".format(sequence_recursive_non_tail(3)))
``` ```
%% Output
The result is still 15
%% Cell type:code id:dd923b7c-0cab-4678-8dc3-aad2ab9b25f9 tags: %% Cell type:code id:dd923b7c-0cab-4678-8dc3-aad2ab9b25f9 tags:
``` python ``` python
assert sequence_recursive_tail(3) == sequence_recursive_non_tail(3) assert sequence_recursive_tail(3) == sequence_recursive_non_tail(3)
``` ```
%% Cell type:markdown id:3c28b36a tags: %% Cell type:markdown id:3c28b36a tags:
### Example 6: check if annagram ### Example 6: check if annagram
Écrire une fonction \texttt{palindrome} qui indique si un mot peut être lu de manière identique dans les deux sens. Donnez la forme itérative et récursive. Écrire une fonction \texttt{palindrome} qui indique si un mot peut être lu de manière identique dans les deux sens. Donnez la forme itérative et récursive.
%% Cell type:code id:51bb3d08 tags: %% Cell type:code id:51bb3d08 tags:
``` python ``` python
def annagram_rec(mot): def annagram_rec(mot):
### BEGIN SOLUTION # YOUR CODE HERE
if len(mot) < 2: raise NotImplementedError()
return True
return mot[0] == mot [len(mot) - 1] and palindrome_rec(mot[1:len(mot)-1])
### END SOLUTION
``` ```
%% Cell type:code id:0c279628-9b96-4687-8e20-a954ab646e0f tags: %% Cell type:code id:0c279628-9b96-4687-8e20-a954ab646e0f tags:
``` python ``` python
annagram_rec("laval") annagram_rec("laval")
``` ```
%% Output
True
%% Cell type:code id:cf6fa61a-7c6f-4a32-96c2-b9fd50deacc6 tags: %% Cell type:code id:cf6fa61a-7c6f-4a32-96c2-b9fd50deacc6 tags:
``` python ``` python
assert annagram_rec("") assert annagram_rec("")
assert annagram_rec("AA") assert annagram_rec("AA")
assert not annagram_rec("ABC") assert not annagram_rec("ABC")
assert annagram_rec("ABA") assert annagram_rec("ABA")
``` ```
%% Cell type:markdown id:798c2875-7940-488a-8458-ad08f6a71c70 tags: %% Cell type:markdown id:798c2875-7940-488a-8458-ad08f6a71c70 tags:
### Example 7: Calculate GCD ### Example 7: Calculate GCD
%% Cell type:code id:48c65c83-be0c-41c4-8c04-1d29ac4415cb tags: %% Cell type:code id:48c65c83-be0c-41c4-8c04-1d29ac4415cb tags:
``` python ``` python
def iterative_gcd(a, b): def iterative_gcd(a, b):
while b: while b:
a, b = b, a % b a, b = b, a % b
return a return a
``` ```
%% Cell type:code id:bf6e1a76 tags: %% Cell type:code id:bf6e1a76 tags:
``` python ``` python
def recursive_gcd(a, b): def recursive_gcd(a, b):
### BEGIN SOLUTION # YOUR CODE HERE
if b == 0: raise NotImplementedError()
return a
else:
return recursive_gcd(b, a % b)
### END SOLUTION
``` ```
%% Cell type:code id:4f1feace tags: %% Cell type:code id:4f1feace tags:
``` python ``` python
recursive_gcd(10, 2) recursive_gcd(10, 2)
``` ```
%% Output
2
%% Cell type:code id:e6ff1765-ff4b-49a5-80d3-684d2627e961 tags: %% Cell type:code id:e6ff1765-ff4b-49a5-80d3-684d2627e961 tags:
``` python ``` python
assert iterative_gcd(10, 2) == recursive_gcd(10, 2) assert iterative_gcd(10, 2) == recursive_gcd(10, 2)
``` ```
%% Cell type:markdown id:d8b05edf-9536-4fc1-bfcc-ddbbeee426fc tags: %% Cell type:markdown id:d8b05edf-9536-4fc1-bfcc-ddbbeee426fc tags:
### Example 8: Calculate average of a list ### Example 8: Calculate average of a list
%% Cell type:code id:1661dfb5-88f2-411f-8fe2-63bbaa29ce72 tags: %% Cell type:code id:1661dfb5-88f2-411f-8fe2-63bbaa29ce72 tags:
``` python ``` python
def calculate_average_recursive(lst, index=0): def calculate_average_recursive(lst, index=0):
### BEGIN SOLUTION # YOUR CODE HERE
if not lst: raise NotImplementedError()
return None
if index == len(lst):
return 0
sum_rest = calculate_average_recursive(lst, index + 1)
return (lst[index] + sum_rest) / (index + 1)
### END SOLUTION
``` ```
%% Cell type:code id:0c5c72c6-3237-4f98-ab96-50a1838b833f tags: %% Cell type:code id:0c5c72c6-3237-4f98-ab96-50a1838b833f tags:
``` python ``` python
calculate_average_recursive([1, 2, 3]) calculate_average_recursive([1, 2, 3])
``` ```
%% Output
2.5
%% Cell type:code id:9f18b6b7-d980-4a72-a2a8-3aa201003d21 tags: %% Cell type:code id:9f18b6b7-d980-4a72-a2a8-3aa201003d21 tags:
``` python ``` python
assert calculate_average_recursive([1, 2, 3]) == 2.5 assert calculate_average_recursive([1, 2, 3]) == 2.5
``` ```
%% Cell type:markdown id:79eef392-1d3d-46c0-aeee-ac805686e6f1 tags: %% Cell type:markdown id:79eef392-1d3d-46c0-aeee-ac805686e6f1 tags:
### Example 9: Check for prime number ### Example 9: Check for prime number
%% Cell type:code id:ac374a08-11c9-47e6-ba11-419549911266 tags: %% Cell type:code id:ac374a08-11c9-47e6-ba11-419549911266 tags:
``` python ``` python
def is_prime_recursive(n, divisor=2): def is_prime_recursive(n, divisor=2):
### BEGIN SOLUTION # YOUR CODE HERE
if n < 2: raise NotImplementedError()
return False
# Base case: If n is 2, it's a prime number
if n == 2:
return True
# Base case: If n is divisible by the current divisor, it's not prime
if n % divisor == 0:
return False
# Base case: If the divisor squared is greater than n, it's prime
if divisor * divisor > n:
return True
# Recursive case: Check with the next divisor
return is_prime_recursive(n, divisor + 1)
### END SOLUTION
``` ```
%% Cell type:code id:996fc91f tags: %% Cell type:code id:996fc91f tags:
``` python ``` python
is_prime_recursive(3) is_prime_recursive(3)
``` ```
%% Output
True
%% Cell type:code id:5be1051c-3b60-4810-b855-6f8575ad6380 tags: %% Cell type:code id:5be1051c-3b60-4810-b855-6f8575ad6380 tags:
``` python ``` python
assert is_prime_recursive(3) assert is_prime_recursive(3)
``` ```
%% Cell type:markdown id:5423682d-c31f-4a8d-9a0f-6812de7b1ae3 tags: %% Cell type:markdown id:5423682d-c31f-4a8d-9a0f-6812de7b1ae3 tags:
### Example 10: Count occurrences of a given element in a list ### Example 10: Count occurrences of a given element in a list
%% Cell type:code id:cfeb0ad0-ed82-499e-9af3-64923291a0e7 tags: %% Cell type:code id:cfeb0ad0-ed82-499e-9af3-64923291a0e7 tags:
``` python ``` python
def count_occurrences(arr, target, index=0): def count_occurrences(arr, target, index=0):
### BEGIN SOLUTION # YOUR CODE HERE
if index == len(arr): raise NotImplementedError()
return 0
# Check if the current element is equal to the target element.
# If it is, increment the count by 1.
count = (1 if arr[index] == target else 0)
# Recursively call the function on the rest of the list (from the next index).
return count + count_occurrences(arr, target, index + 1)
### END SOLUTION
``` ```
%% Cell type:code id:49daec07-00b3-412e-ad44-5bafadbd9f36 tags: %% Cell type:code id:49daec07-00b3-412e-ad44-5bafadbd9f36 tags:
``` python ``` python
count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2) count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2)
``` ```
%% Output
4
%% Cell type:code id:14a2eb85-1126-4506-93bb-4bf624d046b6 tags: %% Cell type:code id:14a2eb85-1126-4506-93bb-4bf624d046b6 tags:
``` python ``` python
assert count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2) == 4 assert count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2) == 4
``` ```
%% Cell type:markdown id:bb1784f5-2426-4661-aa13-dba96743ceb5 tags: %% Cell type:markdown id:bb1784f5-2426-4661-aa13-dba96743ceb5 tags:
# Bonus points # Bonus points
%% Cell type:markdown id:7fdcd705-dc0b-4614-8e27-9ba62f8fe442 tags: %% Cell type:markdown id:7fdcd705-dc0b-4614-8e27-9ba62f8fe442 tags:
### Exercise 1: Find the maximum value in a list (iterative) ### Exercise 1: Find the maximum value in a list (iterative)
Check that an empty lists raises a `ValueError` exception with a `"The list is empty."` message. Check that an empty lists raises a `ValueError` exception with a `"The list is empty."` message.
%% Cell type:code id:f9334f32-f760-46c0-a649-c82f267aba5b tags: %% Cell type:code id:f9334f32-f760-46c0-a649-c82f267aba5b tags:
``` python ``` python
try: try:
result = find_maximum_iterative([]) result = find_maximum_iterative([])
assert False, "Exception not raised" assert False, "Exception not raised"
except ValueError as e: except ValueError as e:
assert str(e) == "The list is empty.", f"Expected error message: 'The list is empty.' but got '{str(e)}'" assert str(e) == "The list is empty.", f"Expected error message: 'The list is empty.' but got '{str(e)}'"
``` ```
%% Cell type:markdown id:c121315a-4aaa-4e04-912f-73f56555be56 tags: %% Cell type:markdown id:c121315a-4aaa-4e04-912f-73f56555be56 tags:
### Exercise 1: Find the maximum value in a list (recursive) ### Exercise 1: Find the maximum value in a list (recursive)
Witout using the built-in `max` function Witout using the built-in `max` function
%% Cell type:code id:775b7e32-a5ae-431b-9987-fcd3671fd022 tags: %% Cell type:code id:775b7e32-a5ae-431b-9987-fcd3671fd022 tags:
``` python ``` python
def find_maximum_recursive_no_max_func(L): def find_maximum_recursive_no_max_func(L):
# Base case: If the list is empty, raise a ValueError # Base case: If the list is empty, raise a ValueError
if not L: if not L:
raise ValueError("The list is empty.") raise ValueError("The list is empty.")
# Base case: If there's only one element in the list, return it as the maximum # Base case: If there's only one element in the list, return it as the maximum
if len(L) == 1: if len(L) == 1:
return L[0] return L[0]
# Recursive case: Compare the first element with the maximum of the rest of the list # Recursive case: Compare the first element with the maximum of the rest of the list
rest_max = find_maximum_recursive(L[1:]) rest_max = find_maximum_recursive(L[1:])
return L[0] if L[0] > rest_max else rest_max return L[0] if L[0] > rest_max else rest_max
``` ```
%% Cell type:code id:f76ed657-3288-4b36-9175-21168d2eb761 tags: %% Cell type:code id:f76ed657-3288-4b36-9175-21168d2eb761 tags:
``` python ``` python
assert not calls_builtin_max(find_maximum_recursive_no_max_func) assert not calls_builtin_max(find_maximum_recursive_no_max_func)
``` ```
%% Cell type:markdown id:bbe7023a-aabd-489d-bae6-3b71566e22ef tags: %% Cell type:markdown id:bbe7023a-aabd-489d-bae6-3b71566e22ef tags:
# Additional tests (do not change) # Additional tests (do not change)
%% Cell type:code id:b8bc657e-d491-4851-aa8c-ac2f2eac0841 tags: %% Cell type:code id:b8bc657e-d491-4851-aa8c-ac2f2eac0841 tags:
``` python ``` python
# checks if a function uses another function, eg "max" # checks if a function uses another function, eg "max"
import inspect import inspect
def calls_builtin_max(func): def calls_builtin_max(func):
source_code = inspect.getsource(func) source_code = inspect.getsource(func)
return 'max(' in source_code return 'max(' in source_code
# Example of such function using max # Example of such function using max
def my_function(lst): def my_function(lst):
return max(lst) return max(lst)
``` ```
%% Cell type:code id:1c73ad0e-ddc9-483d-85a1-f2a65d04f30b tags: %% Cell type:code id:1c73ad0e-ddc9-483d-85a1-f2a65d04f30b tags:
``` python ``` python
assert calls_builtin_max(my_function) assert calls_builtin_max(my_function)
assert not calls_builtin_max(find_maximum_iterative) assert not calls_builtin_max(find_maximum_iterative)
``` ```
%% Cell type:code id:8ce960f5-08da-449e-9e6f-dae215bc1b6a tags: %% Cell type:code id:8ce960f5-08da-449e-9e6f-dae215bc1b6a tags:
``` python ``` python
# generates more examples for a given function # generates more examples for a given function
def gen_examples(fnct, n=10): def gen_examples(fnct, n=10):
return [fnct(i) for i in range(0, n)] return [fnct(i) for i in range(0, n)]
``` ```
%% Cell type:code id:d601b2f3-c33b-4ed2-81be-639ce1ffab76 tags: %% Cell type:code id:d601b2f3-c33b-4ed2-81be-639ce1ffab76 tags:
``` python ``` python
``` ```
%% Cell type:code id:1f252530-7817-444c-ae10-d7b3bca87f2d tags: %% Cell type:code id:1f252530-7817-444c-ae10-d7b3bca87f2d tags:
``` python ``` python
``` ```
%% Cell type:code id:0976f7c2-4375-4d76-a6c1-9e729a6c8aeb tags: %% Cell type:code id:0976f7c2-4375-4d76-a6c1-9e729a6c8aeb tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment