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

Update 02-recursion-exercises.ipynb

parent e54143fc
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:386bb47e tags: %% Cell type:markdown id:61ded096 tags:
NAME: NAME:
%% Cell type:markdown id:a4e4fad3 tags: %% Cell type:markdown id:a4e4fad3 tags:
# UE5 Fundamentals of Algorithms # UE5 Fundamentals of Algorithms
# Lab 2: Recursion # Lab 2: Recursion
%% Cell type:markdown id:a8adef9b tags: %% Cell type:markdown id:a8adef9b tags:
--- ---
%% Cell type:markdown id:eee7125e-cd0b-4aff-ac6d-fba530b13050 tags: %% Cell type:markdown id:eee7125e-cd0b-4aff-ac6d-fba530b13050 tags:
For each of the following questions: For each of the following questions:
- In the `# YOUR CODE HERE` cell, remove `raise NotImplementedError()` to write your code - In the `# YOUR CODE HERE` cell, remove `raise NotImplementedError()` to write your code
- Write an example of use of your code or make sure the given examples and tests pass - Write an example of use of your code or make sure the given examples and tests pass
- Add extra tests in the `#Tests` cell - Add extra tests in the `#Tests` cell
Recall on recursion:
- Find a base case to stop the recursion
- Fin a decomposition of the problem to reach it
- Don't reach the Python recursion limits or infinite loops
To compare iterative and recursive functions you may use the following time comparison functions:
%% Cell type:code id:f6c23281-9c3d-488a-acb9-069964faff4b tags:
``` python
import time
t1 = time.time()
# YOUR CODE
time.sleep(1)
dt = time.time() - t1
print(f"Elapsed time: {dt:.4f} seconds")
```
%% Cell type:markdown id:568202fd tags: %% Cell type:markdown id:568202fd tags:
### Example: Find the maximum value in a list (iterative) ### Example: 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):
if len(L) == 0: if len(L) == 0:
raise ValueError("the list is empty") raise ValueError("the list is empty")
max_val = L[0] max_val = L[0]
for num in L: for num in L:
if num > max_val: if num > max_val:
max_val = num max_val = num
return max_val return max_val
``` ```
%% Cell type:code id:f6baae3c-3660-4add-ab4b-48016cba3030 tags: %% Cell type:code id:f6baae3c-3660-4add-ab4b-48016cba3030 tags:
``` python ``` python
# Example of use # Example of use
find_maximum_iterative([1, 3, 5, 7, 9]) # 9 find_maximum_iterative([1, 3, 5, 7, 9]) # 9
``` ```
%% Cell type:code id:e68b3e9a-418f-4950-9f27-18bb0fe90794 tags: %% Cell type:code id:e68b3e9a-418f-4950-9f27-18bb0fe90794 tags:
``` python ``` python
# Tests # Tests
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:772184f4-6af4-4996-9bb1-da46c31891f8 tags: %% Cell type:markdown id:772184f4-6af4-4996-9bb1-da46c31891f8 tags:
To test exceptions you can use the following code: To test exceptions you can use the following code:
%% Cell type:code id:a4201264-48f2-44f3-817b-a008500aed0c tags: %% Cell type:code id:a4201264-48f2-44f3-817b-a008500aed0c tags:
``` python ``` python
try: try:
find_maximum_iterative([]) find_maximum_iterative([])
except ValueError: except ValueError:
assert True assert True
``` ```
%% 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 to find the max value in a list of integers. You may use the `max()` function for the recursive call but not as a unique solution. Compare with the iterative version from the example. Write a recursive version to find the max value in a list of integers. You may use the `max()` function for the recursive call but not as a unique solution. Compare with the iterative version from the example.
%% Cell type:code id:07668fd8 tags: %% Cell type:code id:07668fd8 tags:
``` python ``` python
def find_maximum_recursive(L): def find_maximum_recursive(L):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:f9784710-4b2b-434c-bc47-da46fa410749 tags: %% Cell type:code id:f9784710-4b2b-434c-bc47-da46fa410749 tags:
``` python ``` python
# Example of use # Example of use
find_maximum_recursive([1, 3, 5, 7, 9]) # 9 find_maximum_recursive([1, 3, 5, 7, 9]) # 9
``` ```
%% Cell type:code id:9b0161f8-0539-4e5e-921c-1886e61c0783 tags: %% Cell type:code id:9b0161f8-0539-4e5e-921c-1886e61c0783 tags:
``` python ``` python
# Tests # Tests
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
Write a recursive function that calculates the sum of the digits so that given a positive integer (e.g. `123` of type `int`), it return the number of digits it contains (e.g. for `123` the solution is `3`, or for `56` the solution is `2`). Compare with the iterative function from the previous lab. Write a recursive function that calculates the sum of the digits so that given a positive integer (e.g. `123` of type `int`), it return the number of digits it contains (e.g. for `123` the solution is `3`, or for `56` the solution is `2`). Compare with the iterative function from the previous lab.
%% 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:cec0caca-cb2c-4e4d-b004-27b3cf2ff611 tags: %% Cell type:code id:cec0caca-cb2c-4e4d-b004-27b3cf2ff611 tags:
``` python ``` python
# Example of use # Example of use
sum_of_digits(10) # 1 sum_of_digits(10) # 1
``` ```
%% Cell type:code id:bf276ca2-48ed-4e87-80f2-776f54b7062b tags: %% Cell type:code id:bf276ca2-48ed-4e87-80f2-776f54b7062b tags:
``` python ``` python
# Tests # Tests
assert sum_of_digits(10) == sum_of_digits(100000) assert sum_of_digits(10) == sum_of_digits(100000)
assert sum_of_digits(0) == 0 assert sum_of_digits(0) == 0
assert sum_of_digits(123) == 6 assert sum_of_digits(123) == 6
``` ```
%% Cell type:markdown id:e2de630a-f9bd-4d45-959b-430e34cc9044 tags: %% Cell type:markdown id:e2de630a-f9bd-4d45-959b-430e34cc9044 tags:
### Exercise 3: Calculate the n-th power value ### Exercise 3: Calculate the n-th power value
Calculate the n-th power of a number using a recursive function. Calculate the n-th power of a number using a recursive function.
%% 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:abddd3b1-f75f-4cb6-a09e-54eed489c5b0 tags: %% Cell type:code id:abddd3b1-f75f-4cb6-a09e-54eed489c5b0 tags:
``` python ``` python
# Example of use # Example of use
power(2, 10) # 1024 power(2, 10) # 1024
``` ```
%% Cell type:code id:8a6605fe-4f6f-45de-84a3-7601e2e2e6f6 tags: %% Cell type:code id:8a6605fe-4f6f-45de-84a3-7601e2e2e6f6 tags:
``` python ``` python
# Tests # Tests
assert power(2, 10) == 1024 assert power(2, 10) == 1024
assert power(2, 0) == 1 assert power(2, 0) == 1
assert power(5, 3) == 125 assert power(5, 3) == 125
assert power(1, 0) == 1 ** 0 assert power(1, 0) == 1 ** 0
``` ```
%% 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
Write a recursive function that takes a string s as input and returns the reversed version of the string. Write a recursive function that takes a string s as input and returns the reversed version of the 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:13acad7e-d03c-4ea6-ad86-baf02e0910eb tags: %% Cell type:code id:13acad7e-d03c-4ea6-ad86-baf02e0910eb tags:
``` python ``` python
# Example of use # Example of use
reverse_string("Romain") # niamoR reverse_string("Romain") # niamoR
``` ```
%% Cell type:code id:453c8e04-6cd9-4581-a206-dd479b6115cd tags: %% Cell type:code id:453c8e04-6cd9-4581-a206-dd479b6115cd tags:
``` python ``` python
# Tests # Tests
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:c1587148-f816-4af2-8f3a-6d8a8480d54d tags: %% Cell type:markdown id:c1587148-f816-4af2-8f3a-6d8a8480d54d tags:
Write another version in an iterative way and compare with the recursive one. Write another version in an iterative way and compare with the recursive one.
%% Cell type:code id:159b6d78-03ae-4cf8-8545-e822b7160b32 tags: %% Cell type:code id:159b6d78-03ae-4cf8-8545-e822b7160b32 tags:
``` python ``` python
def iterative_reverse_string(s): def iterative_reverse_string(s):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:0dcdbd99-5f57-4d1c-bc31-8f8afc617d0e tags: %% Cell type:code id:0dcdbd99-5f57-4d1c-bc31-8f8afc617d0e tags:
``` python ``` python
iterative_reverse_string("Romain") iterative_reverse_string("Romain")
``` ```
%% Cell type:code id:43e10b0c tags: %% Cell type:code id:43e10b0c tags:
``` python ``` python
# Tests # Tests
assert iterative_reverse_string("Romain") == reverse_string("Romain") assert iterative_reverse_string("Romain") == reverse_string("Romain")
``` ```
%% Cell type:markdown id:5ab8f3c9-ddee-45ab-a013-fdd67d9853e0 tags: %% Cell type:markdown id:5ab8f3c9-ddee-45ab-a013-fdd67d9853e0 tags:
### Exercise 5: Convert an interative suite into a recursive tail function ### Exercise 5: Convert an interative suite into a recursive tail function
Convert an iterative function into a recursive tail function (a function where the last operation is the recursive call). Convert an iterative function into a recursive tail function (a function where the last operation is the recursive call).
%% Cell type:code id:219add7f tags: %% Cell type:code id:219add7f tags:
``` python ``` python
# iterative function # iterative function
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
# Example of use # Example of use
print("The result is {}".format(sequence(3))) print("The result is {}".format(sequence(3)))
``` ```
%% 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:43507c43-de61-414d-b389-c0a771ad9e0c tags: %% Cell type:code id:43507c43-de61-414d-b389-c0a771ad9e0c tags:
``` python ``` python
# Example of use # Example of use
print("The result is still {}".format(sequence_recursive_tail(3))) print("The result is still {}".format(sequence_recursive_tail(3)))
``` ```
%% Cell type:code id:dd923b7c-0cab-4678-8dc3-aad2ab9b25f9 tags: %% Cell type:code id:dd923b7c-0cab-4678-8dc3-aad2ab9b25f9 tags:
``` python ``` python
# Tests # Tests
assert sequence_recursive_tail(3) == sequence(3) == 15 assert sequence_recursive_tail(3) == sequence(3) == 15
``` ```
%% Cell type:markdown id:3c28b36a tags: %% Cell type:markdown id:3c28b36a tags:
### Exercise 6: Check if a word is a palindrom ### Exercise 6: Check if a word is a palindrom
Write a function that takes a string as input and returns `True` if the word is a palindrome (i.e., it reads the same forwards and backwards), and `False` otherwise. Write a function that takes a string as input and returns `True` if the word is a palindrome (i.e., it reads the same forwards and backwards), and `False` otherwise.
%% Cell type:code id:51bb3d08 tags: %% Cell type:code id:51bb3d08 tags:
``` python ``` python
def palindrom_rec(word): def palindrom_rec(word):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:0c279628-9b96-4687-8e20-a954ab646e0f tags: %% Cell type:code id:0c279628-9b96-4687-8e20-a954ab646e0f tags:
``` python ``` python
# Example of use # Example of use
palindrom_rec("laval") palindrom_rec("laval")
``` ```
%% Cell type:code id:cf6fa61a-7c6f-4a32-96c2-b9fd50deacc6 tags: %% Cell type:code id:cf6fa61a-7c6f-4a32-96c2-b9fd50deacc6 tags:
``` python ``` python
# Tests # Tests
assert palindrom_rec("") assert palindrom_rec("")
assert palindrom_rec("AA") assert palindrom_rec("AA")
assert not palindrom_rec("ABC") assert not palindrom_rec("ABC")
assert palindrom_rec("ABA") assert palindrom_rec("ABA")
assert palindrom_rec("LAVAL") assert palindrom_rec("LAVAL")
assert palindrom_rec("toto") == False
``` ```
%% Cell type:markdown id:798c2875-7940-488a-8458-ad08f6a71c70 tags: %% Cell type:markdown id:798c2875-7940-488a-8458-ad08f6a71c70 tags:
### Exercise 7: Calculate GCD ### Exercise 7: Calculate GCD
Given two positive integers, write a recursive function to calculate their Greatest Common Divisor (GCD). The GCD of two integers is the largest integer that divides both numbers without leaving a remainder. Compare with the following iterative function: Given two positive integers, write a recursive function to calculate their Greatest Common Divisor (GCD). The GCD of two integers is the largest integer that divides both numbers without leaving a remainder. Compare with the following iterative function:
%% 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:4f1feace tags: %% Cell type:code id:4f1feace tags:
``` python ``` python
# Example of use # Example of use
recursive_gcd(10, 2) recursive_gcd(10, 2)
``` ```
%% Cell type:code id:e6ff1765-ff4b-49a5-80d3-684d2627e961 tags: %% Cell type:code id:e6ff1765-ff4b-49a5-80d3-684d2627e961 tags:
``` python ``` python
# Tests # Tests
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:
### Exercise 8: Check if a list is sorted ### Exercise 8: Check if a list is sorted
Write a recursive function that takes a list of numbers as input and returns `True` if the list is sorted in non-decreasing order and `False` otherwise. Write a recursive function that takes a list of numbers as input and returns `True` if the list is sorted in non-decreasing order and `False` otherwise.
%% Cell type:code id:1661dfb5-88f2-411f-8fe2-63bbaa29ce72 tags: %% Cell type:code id:1661dfb5-88f2-411f-8fe2-63bbaa29ce72 tags:
``` python ``` python
def is_sorted(L): def is_sorted(L):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:0c5c72c6-3237-4f98-ab96-50a1838b833f tags: %% Cell type:code id:0c5c72c6-3237-4f98-ab96-50a1838b833f tags:
``` python ``` python
is_sorted([1, 2, 3, 4, 5]) is_sorted([1, 2, 3, 4, 5])
``` ```
%% Cell type:code id:9f18b6b7-d980-4a72-a2a8-3aa201003d21 tags: %% Cell type:code id:9f18b6b7-d980-4a72-a2a8-3aa201003d21 tags:
``` python ``` python
# Tests # Tests
assert is_sorted([2, 3]) assert is_sorted([2, 3])
assert is_sorted([]) assert is_sorted([])
assert is_sorted([3, 2]) == False assert is_sorted([3, 2]) == False
``` ```
%% Cell type:markdown id:79eef392-1d3d-46c0-aeee-ac805686e6f1 tags: %% Cell type:markdown id:79eef392-1d3d-46c0-aeee-ac805686e6f1 tags:
### Exercise 9: Check for prime number ### Exercise 9: Check for prime number
Write a recursive function that checks if a given integer is a prime number. A prime number is a natural number greater than 1 that has no divisors other than 1 and itself. Write a recursive function that checks if a given integer is a prime number. A prime number is a natural number greater than 1 that has no divisors other than 1 and itself.
%% 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):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:996fc91f tags: %% Cell type:code id:996fc91f tags:
``` python ``` python
# Example of use # Example of use
is_prime_recursive(3) is_prime_recursive(3)
``` ```
%% Cell type:code id:5be1051c-3b60-4810-b855-6f8575ad6380 tags: %% Cell type:code id:5be1051c-3b60-4810-b855-6f8575ad6380 tags:
``` python ``` python
# Tests # Tests
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:
### Exercise 10: Count occurrences of an element in a list ### Exercise 10: Count occurrences of an 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(L, target, index=0): def count_occurrences(L, target, index=0):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:49daec07-00b3-412e-ad44-5bafadbd9f36 tags: %% Cell type:code id:49daec07-00b3-412e-ad44-5bafadbd9f36 tags:
``` python ``` python
# Example of use # Example of use
count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2) count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2)
``` ```
%% Cell type:code id:14a2eb85-1126-4506-93bb-4bf624d046b6 tags: %% Cell type:code id:14a2eb85-1126-4506-93bb-4bf624d046b6 tags:
``` python ``` python
# Tests # Tests
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:88255769-ea62-4910-a459-109cb03e94d2 tags: %% Cell type:markdown id:88255769-ea62-4910-a459-109cb03e94d2 tags:
## Bonus ## Bonus
%% Cell type:markdown id:e6bcb281-7f97-406a-ae20-729dddd518a3 tags: %% Cell type:markdown id:e6bcb281-7f97-406a-ae20-729dddd518a3 tags:
## Exercise 11: 2D Binary search ### Exercise 11: 2D Binary search
We want to search for a target value in a 2D matrix where: We want to search for a target value in a 2D matrix where:
- Each row is sorted in ascending order. - Each row is sorted in ascending order.
- The first element of each row is greater than the last element of the previous row. - The first element of each row is greater than the last element of the previous row.
Implement a recursive function adn Implement a recursive function to solve this problem (hint: use a similar approach to binary search in a 1D array).
%% Cell type:code id:e3e14407-2567-4531-bb8e-b35a6ba6d036 tags: %% Cell type:code id:e3e14407-2567-4531-bb8e-b35a6ba6d036 tags:
``` python ``` python
def binary_search_2D(matrix, target, left=0, right=None): def binary_search_2D(matrix, target, left=0, right=None):
# YOUR CODE HERE # YOUR CODE HERE
raise NotImplementedError() raise NotImplementedError()
``` ```
%% Cell type:code id:291b699a-519c-41ba-9a7d-1cb055f78b40 tags: %% Cell type:code id:291b699a-519c-41ba-9a7d-1cb055f78b40 tags:
``` python ``` python
m = [ m = [
[1, 3, 5, 7], [1, 3, 5, 7],
[10, 11, 16, 20], [10, 11, 16, 20],
[23, 30, 34, 50], [23, 30, 34, 50],
[60, 70, 80, 90] [60, 70, 80, 90]
] ]
``` ```
%% Cell type:code id:6c7215d2-f99a-41d1-a931-3623ab094674 tags: %% Cell type:code id:6c7215d2-f99a-41d1-a931-3623ab094674 tags:
``` python ``` python
# Example of use # Example of use
binary_search_2D(m, 3) # True binary_search_2D(m, 3) # True
``` ```
%% Cell type:code id:38f7f201-26d9-42c3-86dc-ef8712eb4167 tags: %% Cell type:code id:38f7f201-26d9-42c3-86dc-ef8712eb4167 tags:
``` python ``` python
# Tests # Tests
assert binary_search_2D(m, 11) == True assert binary_search_2D(m, 11) == True
assert binary_search_2D(m, 15) == False assert binary_search_2D(m, 15) == False
``` ```
%% Cell type:code id:34fea954-1cf6-4721-9d83-4504ca6e9901 tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment