-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreadme.html
94 lines (88 loc) · 3.19 KB
/
readme.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>readme</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="practice-exercises-for-data-structures">
Practice exercises for data structures
</h1>
<h2 id="overview">Overview</h2>
<p>
In this section, you’ll have a chance to practice the concepts you’ve
learned in the videos. First, review the core concepts covered that you’ll
need to keep in mind. Then go through the exercises below.
</p>
<p>
Remember, these are for your own benefit. Feel free to skip them if you
don’t find a particular exercise valuable or you get stuck for too long.
</p>
<h2 id="core-concepts">Core concepts</h2>
<h3 id="creating-a-static-dictionary">Creating a static dictionary</h3>
<p>
You can create a dictionary a number of ways. How you do this depends on
how much data is static and how much is dynamic as part of the program’s
execution.
</p>
<pre><code># Static data styles:
# empty dictionary
names = {}
# A dictionary with players start at zero score
two_names = {'player1': 0, 'player2': 0}
# This is the same as before
two_names = dict(player1=0, player2=0)</code></pre>
<h3 id="creating-a-dynamic-dictionary">Creating a dynamic dictionary</h3>
<p>If you have dynamic data, this requires something else to build them:</p>
<pre><code>names = get_list_of_names()
scores = {}
for n in names:
scores[n] = 0
# We can condense this using a dictionary comprehension.
# Same as above:
names = get_list_of_names()
scores = {n: 0 for n in names}</code></pre>
<h3 id="reading-values-from-a-dictionary">
Reading values from a dictionary
</h3>
<pre><code># Access a *known* value in the dictionary:
p1_score = scores['player1']
# Access a score, unsure whether player1 is a key, if it isn't there, return 0.
p1_score = scores.get('player1', 0)</code></pre>
<h2 id="exercises">Exercises</h2>
<p>Now it’s your turn. Try this practice below.</p>
<p>
The core idea in this chapter was about dictionaries and data structures
in general. Create a simple program that creates a dictionary called
<code>d</code> such that the following runs without error and prints what
is expected:
</p>
<pre><code># d = create d using core concepts above.
print(d["Sam"]) # outputs 7
print(d['rolls']) # outputs ['rock', 'paper', 'scissors']
print(d.get('Sarah')) # outputs None
print(d.get('Jeff', -1)) # outputs -1
print(d['done']) # outputs True</code></pre>
</body>
</html>