Skip to content

Commit e6c8a29

Browse files
Edit C0-A27
1 parent b08e609 commit e6c8a29

File tree

1 file changed

+102
-15
lines changed

1 file changed

+102
-15
lines changed

chapter0-exercises/chapter_0_part2.ipynb

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,35 +2313,122 @@
23132313
"id": "hourly-burst"
23142314
},
23152315
"source": [
2316-
"## Activity:\n",
2316+
"## Activity 27: Age Record\n",
23172317
"\n",
2318-
"Then combine ages and names lists to create a dictionary while the keys are the names and the values are the corresponding ages"
2318+
"You are given two lists of equal size:\n",
2319+
"\n",
2320+
"1. `names`: this contains the names of several people, and\n",
2321+
"2. `ages`: this has the ages of those people. The values are ordered such that for any index `i`, `ages[i]` is the age of the person with the name located at `names[i]`.\n",
2322+
"\n",
2323+
"How would you implement Python code to map each person's name to their age, all in one data structure?"
23192324
],
23202325
"id": "hourly-burst"
23212326
},
23222327
{
2323-
"cell_type": "code",
2328+
"cell_type": "markdown",
23242329
"metadata": {
2325-
"id": "lined-reservation",
2326-
"outputId": "56d8cce3-9f0a-40e0-f4c3-aaa668d40ea4"
2330+
"id": "jNK6UAnpoohP"
23272331
},
23282332
"source": [
2333+
"### Example Input\n",
2334+
"\n",
2335+
"This activity is a classic example of when to use a `dict` object:\n",
2336+
"\n",
2337+
"```\n",
2338+
"Input:\n",
23292339
"ages = ['15', '27', '67', '102']\n",
23302340
"names = ['Jessica', 'Daniel', 'Edward', 'Oscar']\n",
2331-
"d = {}\n",
2332-
"for age,name in zip(ages, names):\n",
2333-
" d[name] = age\n",
2334-
"print(d)"
2341+
"\n",
2342+
"Output:\n",
2343+
"{\n",
2344+
" 'Jessica': '15', \n",
2345+
" 'Daniel': '27', \n",
2346+
" 'Edward': '67', \n",
2347+
" 'Oscar': '102'\n",
2348+
"}\n",
2349+
"```"
2350+
],
2351+
"id": "jNK6UAnpoohP"
2352+
},
2353+
{
2354+
"cell_type": "code",
2355+
"metadata": {
2356+
"id": "yzWyfNeMpObj"
2357+
},
2358+
"source": [
2359+
"ages = ['15', '27', '67', '102']\n",
2360+
"names = ['Jessica', 'Daniel', 'Edward', 'Oscar']"
2361+
],
2362+
"id": "yzWyfNeMpObj",
2363+
"execution_count": 30,
2364+
"outputs": []
2365+
},
2366+
{
2367+
"cell_type": "markdown",
2368+
"metadata": {
2369+
"id": "5wip1LPzn_B3"
2370+
},
2371+
"source": [
2372+
"### Solution 1: `zip()` Revisited\n",
2373+
"\n",
2374+
"In a separate activity, we saw how using the `zip()` function made it easier to iterate over multiple lists at the same time, and how this is meaningful when those lists have values at each index that correspond to one another.\n",
2375+
"\n",
2376+
"Now let's see how using `zip()` can also make it easier to construct a Python dictionary:"
2377+
],
2378+
"id": "5wip1LPzn_B3"
2379+
},
2380+
{
2381+
"cell_type": "code",
2382+
"metadata": {
2383+
"id": "lined-reservation"
2384+
},
2385+
"source": [
2386+
"def age_record(names, ages):\n",
2387+
" records = {}\n",
2388+
" for age, name in zip(ages, names):\n",
2389+
" records[name] = age\n",
2390+
" return records"
23352391
],
23362392
"id": "lined-reservation",
2337-
"execution_count": null,
2393+
"execution_count": 28,
2394+
"outputs": []
2395+
},
2396+
{
2397+
"cell_type": "markdown",
2398+
"metadata": {
2399+
"id": "QgJW81-wpAcL"
2400+
},
2401+
"source": [
2402+
"#### Test Out Solution 1"
2403+
],
2404+
"id": "QgJW81-wpAcL"
2405+
},
2406+
{
2407+
"cell_type": "code",
2408+
"metadata": {
2409+
"colab": {
2410+
"base_uri": "https://localhost:8080/"
2411+
},
2412+
"id": "dN6wSx-wpLDP",
2413+
"outputId": "20412257-e2db-4cdf-eb37-378047ddeb0f"
2414+
},
2415+
"source": [
2416+
"age_record(names, ages)"
2417+
],
2418+
"id": "dN6wSx-wpLDP",
2419+
"execution_count": 31,
23382420
"outputs": [
23392421
{
2340-
"output_type": "stream",
2341-
"text": [
2342-
"{'Jessica': '15', 'Daniel': '27', 'Edward': '67', 'Oscar': '102'}\n"
2343-
],
2344-
"name": "stdout"
2422+
"output_type": "execute_result",
2423+
"data": {
2424+
"text/plain": [
2425+
"{'Daniel': '27', 'Edward': '67', 'Jessica': '15', 'Oscar': '102'}"
2426+
]
2427+
},
2428+
"metadata": {
2429+
"tags": []
2430+
},
2431+
"execution_count": 31
23452432
}
23462433
]
23472434
},

0 commit comments

Comments
 (0)