Skip to content

Commit 40f9e0f

Browse files
committed
finish updating exercises
1 parent 6d90989 commit 40f9e0f

2 files changed

Lines changed: 213 additions & 103 deletions

File tree

13_object_oriented_programming_advanced.ipynb

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@
554554
"\n",
555555
"from attrs import define\n",
556556
"\n",
557-
"def solution_child_eye_color(mother_eye_color: str, father_eye_color: str) -> str:\n",
557+
"def solution_child_eye_color(mother_eye_color: str, father_eye_color: str) -> list:\n",
558558
" \"\"\"\n",
559559
" Given the eye colors of the mother and father, defines the eye color of the child.\n",
560560
" The possible eye colors are: brown or blue, with brown being the dominant one.\n",
@@ -1475,10 +1475,10 @@
14751475
"\n",
14761476
"<div class=\"alert alert-block alert-warning\">\n",
14771477
" <h4><b>Question</b></h4>\n",
1478-
" Complete the solution function such that it creates the instances of the two computers mentioned in the list below.\n",
1479-
" <strong>Pay attention to the type!</strong>\n",
1478+
" Complete the solution function such that it creates the instances of the two computers mentioned below.\n",
1479+
" They will be automatically passed as arguments to the solution function.\n",
14801480
" </br>\n",
1481-
" This function should return a list that collects the <strong>string representations</strong> of the two computers.\n",
1481+
" This function should return a list that collects the two instances you created.\n",
14821482
"</div>"
14831483
]
14841484
},
@@ -1491,27 +1491,41 @@
14911491
"source": [
14921492
"%%ipytest\n",
14931493
"\n",
1494-
"computers = [\n",
1495-
" {\n",
1496-
" \"type\": \"PC\",\n",
1497-
" \"name\": \"pc_1\",\n",
1498-
" \"price\": 1500,\n",
1499-
" \"quantity\": 1,\n",
1500-
" \"expansion_slots\": 2\n",
1501-
" },\n",
1502-
" {\n",
1503-
" \"type\": \"Laptop\",\n",
1504-
" \"name\": \"laptop_1\",\n",
1505-
" \"price\": 1200,\n",
1506-
" \"quantity\": 4,\n",
1507-
" \"battery_life\": 6\n",
1508-
" }\n",
1509-
"]\n",
1494+
"pc = {\n",
1495+
" \"name\": \"pc_1\",\n",
1496+
" \"price\": 1500,\n",
1497+
" \"quantity\": 1,\n",
1498+
" \"expansion_slots\": 2\n",
1499+
"}\n",
1500+
"\n",
1501+
"laptop = {\n",
1502+
" \"name\": \"laptop_1\",\n",
1503+
" \"price\": 1200,\n",
1504+
" \"quantity\": 4,\n",
1505+
" \"battery_life\": 6\n",
1506+
"}\n",
1507+
"\n",
1508+
"def solution_store_inventory(pc: dict, laptop: dict) -> list:\n",
1509+
" \"\"\"\n",
1510+
" Creates instances of `PC` and `Laptop` classes based on the provided input dictionaries.\n",
1511+
" The `Computer` class serves as the base class with attributes `name`, `price`, and `quantity`.\n",
1512+
" The `PC` and `Laptop` classes inherit from `Computer` and extend it with additional attributes:\n",
1513+
" - `PC` includes `expansion_slots`.\n",
1514+
" - `Laptop` includes `battery_life`.\n",
15101515
"\n",
1516+
" Each class implements the `__init__` and `__str__` methods:\n",
1517+
" - `Computer.__str__`: Returns a string representation of the `Computer` instance.\n",
1518+
" - `PC.__str__`: Appends to the `Computer` string.\n",
1519+
" - `Laptop.__str__`: Appends to the `Computer` string.\n",
15111520
"\n",
1512-
"def solution_store_inventory(computers: list[dict]) -> list[str]:\n",
1513-
" # Write your solution here\n",
1514-
" pass"
1521+
" Args:\n",
1522+
" pc: A dictionary containing the attributes for the `PC` instance.\n",
1523+
" laptop: A dictionary containing the attributes for the `Laptop` instance.\n",
1524+
" Returns:\n",
1525+
" A list containing the created the `PC` and `Laptop` instances.\n",
1526+
" \"\"\"\n",
1527+
"\n",
1528+
" return"
15151529
]
15161530
},
15171531
{
@@ -1529,15 +1543,18 @@
15291543
"- **User** with attributes: username, playlists.\n",
15301544
"\n",
15311545
"Based on these, create the respective classes:\n",
1532-
"- `Song`: should contain attributes `title` (string), `artist` (string) and `album_title` (string)\n",
1533-
"- `Playlist`: should contain attributes `name` (string) and `songs` (a list of `Song` instances). It should also include a method for adding song a song to the playlist.\n",
1534-
"- `User`: should contain attributes `name` (string) and `playlists` (a dict where key is the name of the playlist and value is a `Playlist` instance). It should also include a method for creating a playlist and a method for adding a specific song to a specific playlist.\n",
1546+
"- `Song`: should contain attributes `title` (string), `artist` (string) and `album_title` (string).\n",
1547+
"- `Playlist`: should contain attributes `name` (string) and `songs` (a list of `Song` instances). It should also include a method for adding a song to the playlist.\n",
1548+
"- `User`: should contain attributes `username` (string) and `playlists` (a dict where key is the name of the playlist and value is a `Playlist` instance). It should also include a method for creating a playlist and a method for adding a specific song to a specific playlist.\n",
15351549
"\n",
15361550
"<div class=\"alert alert-block alert-warning\">\n",
15371551
" <h4><b>Question</b></h4>\n",
15381552
" Using <strong>composition</strong> in Python, create a music streaming service system that includes the classes mentioned above.\n",
15391553
" Create <strong>one user that has one playlist</strong>, containing the songs provided in the list below.\n",
1540-
" Your solution function should <strong>return the <code>User</code></strong> instance.\n",
1554+
" They will be automatically passed as arguments to the solution function.\n",
1555+
" The user's username and the name of the playlist are also provided.\n",
1556+
" </br>\n",
1557+
" Your solution function should return the <code>User</code> instance.\n",
15411558
"</div>"
15421559
]
15431560
},
@@ -1568,9 +1585,29 @@
15681585
" },\n",
15691586
"]\n",
15701587
"\n",
1571-
"def solution_music_streaming_service(song_info: list[dict]):\n",
1572-
" # Write your solution here\n",
1573-
" pass"
1588+
"def solution_music_streaming_service(song_info: list[dict], username: str, playlist_name: str):\n",
1589+
" \"\"\"\n",
1590+
" Creates a music streaming service system using composition, including classes for `Song`, `Playlist`, and `User`:\n",
1591+
" - `Song` represents a song with:\n",
1592+
" - title\n",
1593+
" - artist\n",
1594+
" - album_title\n",
1595+
" - `Playlist` represents a playlist with:\n",
1596+
" - name\n",
1597+
" - songs\n",
1598+
" - Includes a method to add a song to the playlist.\n",
1599+
" - `User` represents a user with:\n",
1600+
" - username\n",
1601+
" - playlists\n",
1602+
" - Includes methods to create a playlist and add a song to a specific playlist.\n",
1603+
"\n",
1604+
" Args:\n",
1605+
" song_info: A list of dictionaries, where each dictionary contains the details of a song.\n",
1606+
" Returns:\n",
1607+
" An instance of the `User` class with one playlist containing the provided songs.\n",
1608+
" \"\"\"\n",
1609+
"\n",
1610+
" return"
15741611
]
15751612
},
15761613
{
@@ -1796,8 +1833,29 @@
17961833
"%%ipytest\n",
17971834
"\n",
17981835
"def solution_n_body(universe_start: str) -> int:\n",
1799-
" # Write your solution here\n",
1800-
" pass"
1836+
" \"\"\"\n",
1837+
" Simulates the motion of moons in a 3-dimensional space over a specified number of time steps\n",
1838+
" and calculates the average total energy of the system.\n",
1839+
"\n",
1840+
" The simulation involves:\n",
1841+
" 1. Updating the velocity of each moon based on the gravitational interaction with other moons.\n",
1842+
" 2. Updating the position of each moon by applying its velocity.\n",
1843+
" 3. Calculating the total energy of the system after the simulation.\n",
1844+
"\n",
1845+
" Total energy for a single moon is calculated as:\n",
1846+
" - Potential energy: The sum of the absolute values of its positional coordinates.\n",
1847+
" - Kinetic energy: The sum of the absolute values of its velocity components.\n",
1848+
" - Total energy: Potential energy multiplied by kinetic energy.\n",
1849+
"\n",
1850+
" Args:\n",
1851+
" universe_start (str): A string representing the initial positions of the moons in the format:\n",
1852+
" \"MoonName: x=<value>, y=<value>, z=<value>\"\n",
1853+
"\n",
1854+
" Returns:\n",
1855+
" int: The average total energy of the system after simulating the universe for 1000 time steps.\n",
1856+
" \"\"\"\n",
1857+
"\n",
1858+
" return"
18011859
]
18021860
}
18031861
],

0 commit comments

Comments
 (0)