Skip to content

Commit 67d249c

Browse files
committed
fix final exercises
1 parent 1ec11c3 commit 67d249c

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

11_functional_programming.ipynb

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,17 +1582,17 @@
15821582
"source": [
15831583
"### Exercise 1: Transposing a Matrix\n",
15841584
"\n",
1585-
"Consider a matrix `M` represented row-wise as a list of lists `[[1, 2, 3], [4, 5, 6], [7, 8, 8]]`.\n",
1586-
"Write a function that returns the transpose of `M`, the matrix obtained by exchanging rows and columns.\n",
1585+
"Consider a matrix represented row-wise as a list of lists `[[1, 2, 3], [4, 5, 6], [7, 8, 8]]`.\n",
1586+
"Write a function that returns its transpose, the matrix obtained by exchanging rows and columns.\n",
15871587
"\n",
1588-
"- Example 1: given `M=[[1, 0], [0, 1]]`, the result must be `[[1, 0], [0, 1]]`\n",
1589-
"- Example 2: given `M=[[1, 2, 3], [4, 5, 6], [7, 8, 8]]` the result must be `[1, 4, 7], [2, 5, 8], [3, 6, 8]]`\n",
1588+
"- Example 1: given `matrix=[[1, 0], [0, 1]]`, the result must be `[[1, 0], [0, 1]]`\n",
1589+
"- Example 2: given `matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 8]]` the result must be `[1, 4, 7], [2, 5, 8], [3, 6, 8]]`\n",
15901590
"\n",
15911591
"\n",
15921592
"\n",
15931593
"<div class=\"alert alert-block alert-info\">\n",
15941594
" <h4><b>Hints</b></h4>\n",
1595-
" The <code>zip</code> function behaves similar to a transposition: <code>zip([1,2], [3,4]) = [(1,3), (3,4)])</code>\n",
1595+
" The <code>zip</code> function behaves similarly to a transposition: <code>zip([1,2], [3,4]) = [(1,3), (2,4)])</code>\n",
15961596
"<div>\n"
15971597
]
15981598
},
@@ -1608,12 +1608,12 @@
16081608
"source": [
16091609
"%%ipytest\n",
16101610
"\n",
1611-
"def solution_exercise1(m: \"list[list[int]]\") -> \"list[list[int]]\":\n",
1611+
"def solution_exercise1(matrix: list[list[int]]) -> list[list[int]]:\n",
16121612
" \"\"\"A function that returns the transpose of a given matrix, by using the zip function.\n",
16131613
" The transpose of a matrix is obtained by swapping the rows and columns of the matrix.\n",
16141614
"\n",
16151615
" Args:\n",
1616-
" m: the initial matrix\n",
1616+
" matrix: the initial matrix\n",
16171617
" Returns:\n",
16181618
" - the transposed matrix\n",
16191619
" \"\"\"\n",
@@ -1631,7 +1631,7 @@
16311631
"source": [
16321632
"### Exercise 2: Flattening list of lists\n",
16331633
"\n",
1634-
"Imagine we receive a list of lists `l` like `[[1, 2], [3, 4]]`. Write a function that converts this list into a `flat` list like `[1, 2, 3, 4]`. \n",
1634+
"Imagine we receive a list of lists like `[[1, 2], [3, 4]]`. Write a function that converts this list into a `flat` list like `[1, 2, 3, 4]`. \n",
16351635
"\n",
16361636
"<div class=\"alert alert-block alert-info\">\n",
16371637
" <h4><b>Hints</b></h4>\n",
@@ -1651,11 +1651,11 @@
16511651
"source": [
16521652
"%%ipytest\n",
16531653
"\n",
1654-
"def solution_exercise2(l: \"list[list[any]]\") -> \"list[any]\":\n",
1654+
"def solution_exercise2(my_list: list[list[any]]) -> list[any]:\n",
16551655
" \"\"\"A function that returns a flattened list from a given list of lists, by using funtools.reduce\n",
16561656
"\n",
16571657
" Args:\n",
1658-
" l: the initial list of lists\n",
1658+
" my_list: the initial list of lists\n",
16591659
" Returns:\n",
16601660
" - the flattened list\n",
16611661
" \"\"\"\n",
@@ -1673,7 +1673,7 @@
16731673
"source": [
16741674
"### Exercise 3: Counting initials\n",
16751675
"\n",
1676-
"Given a list `w` of English words, write a function that counts how many words begin with each letter of the English alphabet and returns the result as an **alphabetically sorted** list of tuples `(letter, count)`. We do not distinguish between uppercase and lowercase letters (\"A\" and \"a\" are considered the same). \n",
1676+
"Given a list of English words, write a function that counts how many words begin with each letter of the English alphabet and returns the result as an **alphabetically sorted** list of tuples `(letter, count)`. We do not distinguish between uppercase and lowercase letters (\"A\" and \"a\" are considered the same). \n",
16771677
"\n",
16781678
"\n",
16791679
"<div class=\"alert alert-block alert-info\">\n",
@@ -1702,14 +1702,14 @@
17021702
"source": [
17031703
"%%ipytest\n",
17041704
"\n",
1705-
"def solution_exercise3(w: list[str]) -> list[(str, int)]:\n",
1706-
" \"\"\"A function that counts the number of words from a given list that start with each letter of the alphabet.\n",
1707-
" The function should be case insensitive.\n",
1705+
"def solution_exercise3(words: list[str]) -> list[(str, int)]:\n",
1706+
" \"\"\"A function that counts the number of words from a given list that start with each letter of the alphabet, using sorted() and itertools.groupby.\n",
1707+
" The function should be case insensitive, using lower() to ensure consistet capitalization.\n",
17081708
" It should return a list of tuples, where each tuple contains a letter and the number of words that start with that letter.\n",
1709-
" This tuple should be sorted in alphabetical order.\n",
1709+
" The list of tuples should be sorted in alphabetical order.\n",
17101710
"\n",
17111711
" Args:\n",
1712-
" w: the initial list of words\n",
1712+
" words: the initial list of words\n",
17131713
" Returns:\n",
17141714
" - the alphabetically sorted list of tuples\n",
17151715
" \"\"\"\n",
@@ -1726,16 +1726,16 @@
17261726
},
17271727
"source": [
17281728
"### Exercise 4: Counting initials frequency\n",
1729-
"If you could solve the previous exercise, you now have a list `l` of the form `[('a', 20), ('b', 30), ...]`.\n",
1730-
"If you cannot, do not worry: you will receive the correct input automatically as `l` inside the function `solution_exercise4`\n",
1729+
"If you could solve the previous exercise, you now have a list of the form `[('a', 20), ('b', 30), ...]`.\n",
1730+
"If you could not, do not worry: you will receive the correct input automatically as an argument inside the function `solution_exercise4`.\n",
17311731
"\n",
17321732
"\n",
1733-
"Write a function that computes the *relative frequency* of each letter in the list `l` and returns a list of tuples `(letter, frequency)`.\n",
1733+
"Write a function that computes the *relative frequency* of each letter in that list and returns a list of tuples `(letter, frequency)`.\n",
17341734
"\n",
17351735
"\n",
17361736
"<div class=\"alert alert-block alert-info\">\n",
17371737
" <h4><b>Hints</b></h4>\n",
1738-
" The relative frequence of a value <code>a</code> in a list is simply the number of time it appears in that list over the total lenght of the list.\n",
1738+
" The relative frequence of a value <code>a</code> in a list is simply the number of times it appears in that list, over the total length of the list.\n",
17391739
"<div>"
17401740
]
17411741
},
@@ -1751,14 +1751,14 @@
17511751
"source": [
17521752
"%%ipytest\n",
17531753
"\n",
1754-
"def solution_exercise4(l: \"list[(str, int)]\") -> \"list[(str, float)]\":\n",
1754+
"def solution_exercise4(my_list: list[(str, int)]) -> list[(str, float)]:\n",
17551755
" \"\"\"A function that computes the relative frequency of each letter from a given list of tuples.\n",
17561756
" The relative frequency is calculated as the number of occurrences of a letter in the list, divided by the length of the list.\n",
17571757
"\n",
17581758
" Args:\n",
1759-
" l: the initial list of tuples\n",
1759+
" my_list: the initial list of tuples (letter, count) which counts how many words start with each letter\n",
17601760
" Returns:\n",
1761-
" - the list of tuples with the relative frequency of each letter\n",
1761+
" - the list of tuples (letter, frequency) with the relative frequency of each letter\n",
17621762
" \"\"\"\n",
17631763
"\n",
17641764
" return"
@@ -1806,14 +1806,15 @@
18061806
"source": [
18071807
"%%ipytest\n",
18081808
"\n",
1809-
"def solution_exercise5(words: \"list[str]\") -> \"list[str]\":\n",
1810-
" \"\"\"A function that returns a list of words from a given list of words, that are palindromes.\n",
1811-
" A palindrome is a word that reads the same backwards as forward.\n",
1809+
"def solution_exercise5(words: list[str]) -> list[str]:\n",
1810+
" \"\"\"A function that returns a list of words that are palindromes, from a given list of words.\n",
1811+
" A palindrome is a word that reads the same forward and backward.\n",
1812+
" A word is any string longer than 1 character.\n",
18121813
"\n",
18131814
" Args:\n",
18141815
" words: the initial list of words\n",
18151816
" Returns:\n",
1816-
" - the list of palindromes\n",
1817+
" - the list of palindrome words\n",
18171818
" \"\"\"\n",
18181819
"\n",
18191820
" return"
@@ -1837,7 +1838,7 @@
18371838
"name": "python",
18381839
"nbconvert_exporter": "python",
18391840
"pygments_lexer": "ipython3",
1840-
"version": "3.12.10"
1841+
"version": "3.10.13"
18411842
},
18421843
"vscode": {
18431844
"interpreter": {

tutorial/tests/test_11_functional_programming.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ def test_multiples_of_n(
153153
#
154154

155155

156-
def reference_exercise1(x: List[List[int]]) -> List[List[int]]:
157-
return [list(i) for i in zip(*x)]
156+
def reference_exercise1(matrix: List[List[int]]) -> List[List[int]]:
157+
return [list(i) for i in zip(*matrix)]
158158

159159

160160
@pytest.mark.parametrize(
@@ -205,10 +205,12 @@ def get_data_exercise3() -> List[str]:
205205
return words.splitlines()
206206

207207

208-
def reference_exercise3(w: List[str]) -> List[Tuple[str, int]]:
208+
def reference_exercise3(words: List[str]) -> List[Tuple[str, int]]:
209209
return [
210210
(k, len(list(v)))
211-
for k, v in itertools.groupby(sorted(w, key=lambda x: x[0]), key=lambda x: x[0])
211+
for k, v in itertools.groupby(
212+
sorted(words, key=lambda x: x[0]), key=lambda x: x[0]
213+
)
212214
]
213215

214216

@@ -239,8 +241,8 @@ def test_exercise4(
239241
#
240242

241243

242-
def reference_exercise5(my_list: List[str]) -> List[str]:
243-
return list(filter(lambda x: x == x[::-1] and len(x) > 1, my_list))
244+
def reference_exercise5(words: List[str]) -> List[str]:
245+
return list(filter(lambda x: x == x[::-1] and len(x) > 1, words))
244246

245247

246248
def test_exercise5(function_to_test: Callable[[List[str]], List[str]]):

0 commit comments

Comments
 (0)