|
1582 | 1582 | "source": [ |
1583 | 1583 | "### Exercise 1: Transposing a Matrix\n", |
1584 | 1584 | "\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", |
1587 | 1587 | "\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", |
1590 | 1590 | "\n", |
1591 | 1591 | "\n", |
1592 | 1592 | "\n", |
1593 | 1593 | "<div class=\"alert alert-block alert-info\">\n", |
1594 | 1594 | " <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", |
1596 | 1596 | "<div>\n" |
1597 | 1597 | ] |
1598 | 1598 | }, |
|
1608 | 1608 | "source": [ |
1609 | 1609 | "%%ipytest\n", |
1610 | 1610 | "\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", |
1612 | 1612 | " \"\"\"A function that returns the transpose of a given matrix, by using the zip function.\n", |
1613 | 1613 | " The transpose of a matrix is obtained by swapping the rows and columns of the matrix.\n", |
1614 | 1614 | "\n", |
1615 | 1615 | " Args:\n", |
1616 | | - " m: the initial matrix\n", |
| 1616 | + " matrix: the initial matrix\n", |
1617 | 1617 | " Returns:\n", |
1618 | 1618 | " - the transposed matrix\n", |
1619 | 1619 | " \"\"\"\n", |
|
1631 | 1631 | "source": [ |
1632 | 1632 | "### Exercise 2: Flattening list of lists\n", |
1633 | 1633 | "\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", |
1635 | 1635 | "\n", |
1636 | 1636 | "<div class=\"alert alert-block alert-info\">\n", |
1637 | 1637 | " <h4><b>Hints</b></h4>\n", |
|
1651 | 1651 | "source": [ |
1652 | 1652 | "%%ipytest\n", |
1653 | 1653 | "\n", |
1654 | | - "def solution_exercise2(l: \"list[list[any]]\") -> \"list[any]\":\n", |
| 1654 | + "def solution_exercise2(my_list: list[list[any]]) -> list[any]:\n", |
1655 | 1655 | " \"\"\"A function that returns a flattened list from a given list of lists, by using funtools.reduce\n", |
1656 | 1656 | "\n", |
1657 | 1657 | " Args:\n", |
1658 | | - " l: the initial list of lists\n", |
| 1658 | + " my_list: the initial list of lists\n", |
1659 | 1659 | " Returns:\n", |
1660 | 1660 | " - the flattened list\n", |
1661 | 1661 | " \"\"\"\n", |
|
1673 | 1673 | "source": [ |
1674 | 1674 | "### Exercise 3: Counting initials\n", |
1675 | 1675 | "\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", |
1677 | 1677 | "\n", |
1678 | 1678 | "\n", |
1679 | 1679 | "<div class=\"alert alert-block alert-info\">\n", |
|
1702 | 1702 | "source": [ |
1703 | 1703 | "%%ipytest\n", |
1704 | 1704 | "\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", |
1708 | 1708 | " 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", |
1710 | 1710 | "\n", |
1711 | 1711 | " Args:\n", |
1712 | | - " w: the initial list of words\n", |
| 1712 | + " words: the initial list of words\n", |
1713 | 1713 | " Returns:\n", |
1714 | 1714 | " - the alphabetically sorted list of tuples\n", |
1715 | 1715 | " \"\"\"\n", |
|
1726 | 1726 | }, |
1727 | 1727 | "source": [ |
1728 | 1728 | "### 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", |
1731 | 1731 | "\n", |
1732 | 1732 | "\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", |
1734 | 1734 | "\n", |
1735 | 1735 | "\n", |
1736 | 1736 | "<div class=\"alert alert-block alert-info\">\n", |
1737 | 1737 | " <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", |
1739 | 1739 | "<div>" |
1740 | 1740 | ] |
1741 | 1741 | }, |
|
1751 | 1751 | "source": [ |
1752 | 1752 | "%%ipytest\n", |
1753 | 1753 | "\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", |
1755 | 1755 | " \"\"\"A function that computes the relative frequency of each letter from a given list of tuples.\n", |
1756 | 1756 | " The relative frequency is calculated as the number of occurrences of a letter in the list, divided by the length of the list.\n", |
1757 | 1757 | "\n", |
1758 | 1758 | " 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", |
1760 | 1760 | " 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", |
1762 | 1762 | " \"\"\"\n", |
1763 | 1763 | "\n", |
1764 | 1764 | " return" |
|
1806 | 1806 | "source": [ |
1807 | 1807 | "%%ipytest\n", |
1808 | 1808 | "\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", |
1812 | 1813 | "\n", |
1813 | 1814 | " Args:\n", |
1814 | 1815 | " words: the initial list of words\n", |
1815 | 1816 | " Returns:\n", |
1816 | | - " - the list of palindromes\n", |
| 1817 | + " - the list of palindrome words\n", |
1817 | 1818 | " \"\"\"\n", |
1818 | 1819 | "\n", |
1819 | 1820 | " return" |
|
1837 | 1838 | "name": "python", |
1838 | 1839 | "nbconvert_exporter": "python", |
1839 | 1840 | "pygments_lexer": "ipython3", |
1840 | | - "version": "3.12.10" |
| 1841 | + "version": "3.10.13" |
1841 | 1842 | }, |
1842 | 1843 | "vscode": { |
1843 | 1844 | "interpreter": { |
|
0 commit comments