|
635 | 635 | "def non_transparent_function() -> int:\n", |
636 | 636 | " return random.randint(0, 10)\n", |
637 | 637 | "\n", |
638 | | - "\n", |
639 | 638 | "# With a lamba we capture the expression x() + 5 in a function,\n", |
640 | 639 | "# we get its value when we call it like a function\n", |
641 | 640 | "y1 = lambda: non_transparent_function() + 5\n", |
|
1065 | 1064 | "%%ipytest\n", |
1066 | 1065 | "\n", |
1067 | 1066 | "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", |
1068 | 1073 | " \"\"\"\n", |
1069 | | - " Write your solution here\n", |
1070 | | - " \"\"\"\n", |
1071 | | - " pass" |
| 1074 | + "\n", |
| 1075 | + " return" |
1072 | 1076 | ] |
1073 | 1077 | }, |
1074 | 1078 | { |
|
1090 | 1094 | "%%ipytest\n", |
1091 | 1095 | "\n", |
1092 | 1096 | "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", |
1093 | 1103 | " \"\"\"\n", |
1094 | | - " Write your solution here\n", |
1095 | | - " \"\"\"\n", |
1096 | | - " pass" |
| 1104 | + "\n", |
| 1105 | + " return" |
1097 | 1106 | ] |
1098 | 1107 | }, |
1099 | 1108 | { |
|
1313 | 1322 | "%%ipytest\n", |
1314 | 1323 | "\n", |
1315 | 1324 | "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", |
1316 | 1332 | " \"\"\"\n", |
1317 | | - " Write your solution here\n", |
1318 | | - " \"\"\"\n", |
1319 | | - " pass" |
| 1333 | + "\n", |
| 1334 | + " return" |
1320 | 1335 | ] |
1321 | 1336 | }, |
1322 | 1337 | { |
|
1526 | 1541 | "### Exercise 1: Transposing a Matrix\n", |
1527 | 1542 | "\n", |
1528 | 1543 | "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", |
1530 | 1545 | "\n", |
1531 | 1546 | "- Example 1: given `M=[[1, 0], [0, 1]]`, the result must be `[[1, 0], [0, 1]]`\n", |
1532 | 1547 | "- 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 | 1550 | "\n", |
1536 | 1551 | "<div class=\"alert alert-block alert-info\">\n", |
1537 | 1552 | " <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", |
1546 | 1554 | "<div>\n" |
1547 | 1555 | ] |
1548 | 1556 | }, |
|
1559 | 1567 | "%%ipytest\n", |
1560 | 1568 | "\n", |
1561 | 1569 | "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", |
1562 | 1577 | " \"\"\"\n", |
1563 | | - " Write your solution here\n", |
1564 | | - " \"\"\"\n", |
1565 | | - " pass\n" |
| 1578 | + "\n", |
| 1579 | + " return\n" |
1566 | 1580 | ] |
1567 | 1581 | }, |
1568 | 1582 | { |
|
1579 | 1593 | "\n", |
1580 | 1594 | "<div class=\"alert alert-block alert-info\">\n", |
1581 | 1595 | " <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", |
1590 | 1597 | "<div>" |
1591 | 1598 | ] |
1592 | 1599 | }, |
|
1603 | 1610 | "%%ipytest\n", |
1604 | 1611 | "\n", |
1605 | 1612 | "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", |
1606 | 1619 | " \"\"\"\n", |
1607 | | - " Write your solution here\n", |
1608 | | - " \"\"\"\n", |
1609 | | - " pass\n", |
1610 | | - "\n" |
| 1620 | + "\n", |
| 1621 | + " return\n" |
1611 | 1622 | ] |
1612 | 1623 | }, |
1613 | 1624 | { |
|
1630 | 1641 | " Consider the functions <code>sorted</code> and <code>itertools.groupby</code> from the Python standard library.\n", |
1631 | 1642 | " </li>\n", |
1632 | 1643 | " <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", |
1636 | 1644 | " To ensure consistent capitalization you can use the <code>lower()</code> method of <code>str</code>\n", |
1637 | 1645 | " </li>\n", |
1638 | 1646 | " </ul>\n", |
|
1653 | 1661 | "%%ipytest\n", |
1654 | 1662 | "\n", |
1655 | 1663 | "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", |
1656 | 1673 | " \"\"\"\n", |
1657 | | - " Write your solution here\n", |
1658 | | - " \"\"\"\n", |
1659 | | - " pass\n", |
1660 | | - "\n" |
| 1674 | + "\n", |
| 1675 | + " return\n" |
1661 | 1676 | ] |
1662 | 1677 | }, |
1663 | 1678 | { |
|
1673 | 1688 | "If you cannot, do not worry: you will receive the correct input automatically as `l` inside the function `solution_exercise4`\n", |
1674 | 1689 | "\n", |
1675 | 1690 | "\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", |
1677 | 1692 | "\n", |
1678 | 1693 | "\n", |
1679 | 1694 | "<div class=\"alert alert-block alert-info\">\n", |
1680 | 1695 | " <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", |
1690 | 1697 | "<div>" |
1691 | 1698 | ] |
1692 | 1699 | }, |
|
1703 | 1710 | "%%ipytest\n", |
1704 | 1711 | "\n", |
1705 | 1712 | "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" |
1707 | 1723 | ] |
1708 | 1724 | }, |
1709 | 1725 | { |
|
1715 | 1731 | }, |
1716 | 1732 | "source": [ |
1717 | 1733 | "### 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", |
1719 | 1735 | "\n", |
1720 | 1736 | "For example:\n", |
1721 | 1737 | "- rotator\n", |
1722 | 1738 | "- wow\n", |
1723 | 1739 | "- noon\n", |
1724 | 1740 | "- radar\n", |
1725 | 1741 | "\n", |
1726 | | - "\n", |
1727 | | - "\n", |
1728 | | - "\n", |
1729 | 1742 | "<div class=\"alert alert-block alert-info\">\n", |
1730 | 1743 | " <h4><b>Hints</b></h4>\n", |
1731 | 1744 | " <ul>\n", |
1732 | 1745 | " <li>\n", |
1733 | 1746 | " A single character does not count as a palindrome.\n", |
1734 | 1747 | " </li>\n", |
1735 | 1748 | " <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", |
1737 | 1750 | " </li>\n", |
1738 | 1751 | " </ul>\n", |
1739 | 1752 | "<div>\n" |
|
1752 | 1765 | "%%ipytest\n", |
1753 | 1766 | "\n", |
1754 | 1767 | "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" |
1756 | 1778 | ] |
1757 | 1779 | } |
1758 | 1780 | ], |
|
0 commit comments