Skip to content

Commit 4ada452

Browse files
committed
add docstring
1 parent 093fcef commit 4ada452

File tree

1 file changed

+80
-58
lines changed

1 file changed

+80
-58
lines changed

11_functional_programming.ipynb

Lines changed: 80 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@
635635
"def non_transparent_function() -> int:\n",
636636
" return random.randint(0, 10)\n",
637637
"\n",
638-
"\n",
639638
"# With a lamba we capture the expression x() + 5 in a function,\n",
640639
"# we get its value when we call it like a function\n",
641640
"y1 = lambda: non_transparent_function() + 5\n",
@@ -1065,10 +1064,15 @@
10651064
"%%ipytest\n",
10661065
"\n",
10671066
"def solution_filter_even(input_data: list[int]) -> list[int]:\n",
1067+
" \"\"\"A function that filters only the even elements of a given list.\n",
1068+
"\n",
1069+
" Args:\n",
1070+
" input_data: the initial list\n",
1071+
" Returns:\n",
1072+
" - the filtered list with only even elements\n",
10681073
" \"\"\"\n",
1069-
" Write your solution here\n",
1070-
" \"\"\"\n",
1071-
" pass"
1074+
"\n",
1075+
" return"
10721076
]
10731077
},
10741078
{
@@ -1090,10 +1094,15 @@
10901094
"%%ipytest\n",
10911095
"\n",
10921096
"def solution_add_one(input_data: list[int]) -> list[int]:\n",
1097+
" \"\"\"A function that adds the integer number 1 to each element of a given list.\n",
1098+
"\n",
1099+
" Args:\n",
1100+
" input_data: the initial list of integers\n",
1101+
" Returns:\n",
1102+
" - the edited list after adding 1 to each element\n",
10931103
" \"\"\"\n",
1094-
" Write your solution here\n",
1095-
" \"\"\"\n",
1096-
" pass"
1104+
"\n",
1105+
" return"
10971106
]
10981107
},
10991108
{
@@ -1313,10 +1322,16 @@
13131322
"%%ipytest\n",
13141323
"\n",
13151324
"def solution_multiples_of_n(l: \"list[int]\", k: int):\n",
1325+
" \"\"\"A function that keeps only the multiples of k from a given list.\n",
1326+
"\n",
1327+
" Args:\n",
1328+
" l: the initial list\n",
1329+
" k: the integer number\n",
1330+
" Returns:\n",
1331+
" - the filtered list\n",
13161332
" \"\"\"\n",
1317-
" Write your solution here\n",
1318-
" \"\"\"\n",
1319-
" pass"
1333+
"\n",
1334+
" return"
13201335
]
13211336
},
13221337
{
@@ -1526,7 +1541,7 @@
15261541
"### Exercise 1: Transposing a Matrix\n",
15271542
"\n",
15281543
"Consider a matrix `M` represented row-wise as a list of lists `[[1, 2, 3], [4, 5, 6], [7, 8, 8]]`.\n",
1529-
"Write a function that returns the transpose of `M`, the matrix obtained by exchanging rows and columns\n",
1544+
"Write a function that returns the transpose of `M`, the matrix obtained by exchanging rows and columns.\n",
15301545
"\n",
15311546
"- Example 1: given `M=[[1, 0], [0, 1]]`, the result must be `[[1, 0], [0, 1]]`\n",
15321547
"- 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",
@@ -1535,14 +1550,7 @@
15351550
"\n",
15361551
"<div class=\"alert alert-block alert-info\">\n",
15371552
" <h4><b>Hints</b></h4>\n",
1538-
" <ul>\n",
1539-
" <li>\n",
1540-
" The <code>zip</code> function behaves similar to a transposition: <code>zip([1,2], [3,4]) = [(1,3), (3,4)])</code>\n",
1541-
" </li>\n",
1542-
" <li>\n",
1543-
" Write your function in the cell below inside of the <code>solution_exercise1</code> function. The function receives a the \"matrix\" as an input <code>m</code> and should return another list\n",
1544-
" </li>\n",
1545-
" </ul>\n",
1553+
" The <code>zip</code> function behaves similar to a transposition: <code>zip([1,2], [3,4]) = [(1,3), (3,4)])</code>\n",
15461554
"<div>\n"
15471555
]
15481556
},
@@ -1559,10 +1567,16 @@
15591567
"%%ipytest\n",
15601568
"\n",
15611569
"def solution_exercise1(m: \"list[list[int]]\") -> \"list[list[int]]\":\n",
1570+
" \"\"\"A function that returns the transpose of a given matrix, by using the zip function.\n",
1571+
" The transpose of a matrix is obtained by swapping the rows and columns of the matrix.\n",
1572+
"\n",
1573+
" Args:\n",
1574+
" m: the initial matrix\n",
1575+
" Returns:\n",
1576+
" - the transposed matrix\n",
15621577
" \"\"\"\n",
1563-
" Write your solution here\n",
1564-
" \"\"\"\n",
1565-
" pass\n"
1578+
"\n",
1579+
" return\n"
15661580
]
15671581
},
15681582
{
@@ -1579,14 +1593,7 @@
15791593
"\n",
15801594
"<div class=\"alert alert-block alert-info\">\n",
15811595
" <h4><b>Hints</b></h4>\n",
1582-
" <ul>\n",
1583-
" <li>\n",
1584-
" This is a good exercise to use <code>functools.reduce</code> \n",
1585-
" </li>\n",
1586-
" <li>\n",
1587-
" Write your function in the cell below inside of the <code>solution_exercise2</code> function. The function receives a list <code>l</code> as an input and should return another list\n",
1588-
" </li>\n",
1589-
" </ul>\n",
1596+
" This is a good exercise to use <code>functools.reduce</code> \n",
15901597
"<div>"
15911598
]
15921599
},
@@ -1603,11 +1610,15 @@
16031610
"%%ipytest\n",
16041611
"\n",
16051612
"def solution_exercise2(l: \"list[list[any]]\") -> \"list[any]\":\n",
1613+
" \"\"\"A function that returns a flattened list from a given list of lists, by using funtools.reduce\n",
1614+
"\n",
1615+
" Args:\n",
1616+
" l: the initial list of lists\n",
1617+
" Returns:\n",
1618+
" - the flattened list\n",
16061619
" \"\"\"\n",
1607-
" Write your solution here\n",
1608-
" \"\"\"\n",
1609-
" pass\n",
1610-
"\n"
1620+
"\n",
1621+
" return\n"
16111622
]
16121623
},
16131624
{
@@ -1630,9 +1641,6 @@
16301641
" Consider the functions <code>sorted</code> and <code>itertools.groupby</code> from the Python standard library.\n",
16311642
" </li>\n",
16321643
" <li>\n",
1633-
" Write your function in the cell below inside of the <code>solution_exercise3</code> function. The function receives a list <code>w</code> as an input and should return another list\n",
1634-
" </li>\n",
1635-
" <li>\n",
16361644
" To ensure consistent capitalization you can use the <code>lower()</code> method of <code>str</code>\n",
16371645
" </li>\n",
16381646
" </ul>\n",
@@ -1653,11 +1661,18 @@
16531661
"%%ipytest\n",
16541662
"\n",
16551663
"def solution_exercise3(w: list[str]) -> list[(str, int)]:\n",
1664+
" \"\"\"A function that counts the number of words from a given list that start with each letter of the alphabet.\n",
1665+
" The function should be case insensitive.\n",
1666+
" It should return a list of tuples, where each tuple contains a letter and the number of words that start with that letter.\n",
1667+
" This tuple should be sorted in alphabetical order.\n",
1668+
"\n",
1669+
" Args:\n",
1670+
" w: the initial list of words\n",
1671+
" Returns:\n",
1672+
" - the alphabetically sorted list of tuples\n",
16561673
" \"\"\"\n",
1657-
" Write your solution here\n",
1658-
" \"\"\"\n",
1659-
" pass\n",
1660-
"\n"
1674+
"\n",
1675+
" return\n"
16611676
]
16621677
},
16631678
{
@@ -1673,20 +1688,12 @@
16731688
"If you cannot, do not worry: you will receive the correct input automatically as `l` inside the function `solution_exercise4`\n",
16741689
"\n",
16751690
"\n",
1676-
"Write a function that computes the *relative frequency* of each letter in the list `l`. \n",
1691+
"Write a function that computes the *relative frequency* of each letter in the list `l` and returns a list of tuples `(letter, frequency)`.\n",
16771692
"\n",
16781693
"\n",
16791694
"<div class=\"alert alert-block alert-info\">\n",
16801695
" <h4><b>Hints</b></h4>\n",
1681-
" <ul>\n",
1682-
" <li>\n",
1683-
" The relative frequence of a value <code>a</code> in a list is simply the number of time it appears in that\n",
1684-
" list over the total lenght of the list\n",
1685-
" </li>\n",
1686-
" <li>\n",
1687-
" Write your function in the cell below inside of the <code>solution_exercise4</code> function. The function receives a list <code>l</code> as an input and should return another list\n",
1688-
" </li>\n",
1689-
" </ul>\n",
1696+
" 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",
16901697
"<div>"
16911698
]
16921699
},
@@ -1703,7 +1710,16 @@
17031710
"%%ipytest\n",
17041711
"\n",
17051712
"def solution_exercise4(l: \"list[(str, int)]\") -> \"list[(str, float)]\":\n",
1706-
" pass"
1713+
" \"\"\"A function that computes the relative frequency of each letter from a given list of tuples.\n",
1714+
" The relative frequency is calculated as the number of occurrences of a letter in the list, divided by the length of the list.\n",
1715+
"\n",
1716+
" Args:\n",
1717+
" l: the initial list of tuples\n",
1718+
" Returns:\n",
1719+
" - the list of tuples with the relative frequency of each letter\n",
1720+
" \"\"\"\n",
1721+
"\n",
1722+
" return"
17071723
]
17081724
},
17091725
{
@@ -1715,25 +1731,22 @@
17151731
},
17161732
"source": [
17171733
"### Exercise 5: Finding palindromes\n",
1718-
"Consider again the `words` list of strings. Write a function that returns the list of all *palindromes*. A *palindrome* is a word (any string longer than 1) that reads the same left-to-right and right-to-left.\n",
1734+
"Consider again the `words` list of strings. Write a function that returns the list of all *palindromes*. A *palindrome* is a word (any string longer than 1) that reads the same left-to-right and right-to-left.\n",
17191735
"\n",
17201736
"For example:\n",
17211737
"- rotator\n",
17221738
"- wow\n",
17231739
"- noon\n",
17241740
"- radar\n",
17251741
"\n",
1726-
"\n",
1727-
"\n",
1728-
"\n",
17291742
"<div class=\"alert alert-block alert-info\">\n",
17301743
" <h4><b>Hints</b></h4>\n",
17311744
" <ul>\n",
17321745
" <li>\n",
17331746
" A single character does not count as a palindrome.\n",
17341747
" </li>\n",
17351748
" <li>\n",
1736-
" The words are available as the input <code>words</code> to <code>solution_exercise5</code>.\n",
1749+
" The words are available as the input to <code>solution_exercise3</code>.\n",
17371750
" </li>\n",
17381751
" </ul>\n",
17391752
"<div>\n"
@@ -1752,7 +1765,16 @@
17521765
"%%ipytest\n",
17531766
"\n",
17541767
"def solution_exercise5(words: \"list[str]\") -> \"list[str]\":\n",
1755-
" pass"
1768+
" \"\"\"A function that returns a list of words from a given list of words, that are palindromes.\n",
1769+
" A palindrome is a word that reads the same backwards as forward.\n",
1770+
"\n",
1771+
" Args:\n",
1772+
" words: the initial list of words\n",
1773+
" Returns:\n",
1774+
" - the list of palindromes\n",
1775+
" \"\"\"\n",
1776+
"\n",
1777+
" return"
17561778
]
17571779
}
17581780
],

0 commit comments

Comments
 (0)