-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenges.html
More file actions
189 lines (189 loc) · 11 KB
/
Challenges.html
File metadata and controls
189 lines (189 loc) · 11 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Building programs with Python</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://www.ecs.soton.ac.uk" title="Electronics and Computer Science">
<img alt="ECS banner" src="img/ecs-logo.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Building programs with Python</h1></a>
<h2 class="subtitle">Challenges</h2>
<blockquote>
<h2 id="python-basics-variables-objects-arrays-lists-etc">Python basics: Variables, Objects, Arrays, Lists etc</h2>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="whats-inside-the-box"><span class="glyphicon glyphicon-pencil"></span>What’s inside the box?</h4>
</div>
<div class="panel-body">
<p>Draw diagrams showing what variables refer to what values after each statement in the following program:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">weight <span class="op">=</span> <span class="fl">70.5</span>
age <span class="op">=</span> <span class="dv">35</span>
<span class="co"># Take a trip to the planet Neptune</span>
weight <span class="op">=</span> weight <span class="op">*</span> <span class="fl">1.14</span>
age <span class="op">=</span> age <span class="op">+</span> <span class="dv">20</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="sorting-out-references"><span class="glyphicon glyphicon-pencil"></span>Sorting out references</h4>
</div>
<div class="panel-body">
<p>What does the following program print out?</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">first, second <span class="op">=</span> <span class="st">'Grace'</span>, <span class="st">'Hopper'</span>
third, fourth <span class="op">=</span> second, first
<span class="bu">print</span> third, fourth</code></pre></div>
</div>
</section>
<blockquote>
<h2 id="loops">Loops</h2>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="from-1-to-n"><span class="glyphicon glyphicon-pencil"></span>From 1 to N</h4>
</div>
<div class="panel-body">
<p>Python has a built-in function called <code>range</code> that creates a list of numbers: <code>range(3)</code> produces <code>[0, 1, 2]</code>, <code>range(2, 5)</code> produces <code>[2, 3, 4]</code>. Using <code>range</code>, write a loop to print the first 3 natural numbers:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">1</span>
<span class="dv">2</span>
<span class="dv">3</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="computing-powers-with-loops"><span class="glyphicon glyphicon-pencil"></span>Computing powers with loops</h2>
</div>
<div class="panel-body">
<p>Exponentiation is built into Python:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span> <span class="dv">5</span> <span class="op">**</span> <span class="dv">3</span>
<span class="dv">125</span></code></pre></div>
<p>Write a loop that calculates the same result as <code>5 ** 3</code> using multiplication (and without exponentiation).</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="reverse-a-string"><span class="glyphicon glyphicon-pencil"></span>Reverse a string</h2>
</div>
<div class="panel-body">
<p>Write a loop that takes a string, and produces a new string with the characters in reverse order, so <code>Newton</code> becomes <code>notweN</code>.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="slicing-strings"><span class="glyphicon glyphicon-pencil"></span>Slicing strings</h4>
</div>
<div class="panel-body">
<p>A section of an array is called a <a href="../../reference.html#slice">slice</a>. We can take slices of character strings as well:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">element <span class="op">=</span> <span class="st">'oxygen'</span>
<span class="bu">print</span> <span class="st">'first three characters:'</span>, element[<span class="dv">0</span>:<span class="dv">3</span>]
<span class="bu">print</span> <span class="st">'last three characters:'</span>, element[<span class="dv">3</span>:<span class="dv">6</span>]</code></pre></div>
</div>
</section>
<blockquote>
<p>What is the value of <code>element[:4]</code>? What about <code>element[4:]</code>? Or <code>element[:]</code>?</p>
<p>What is <code>element[-1]</code>? What is <code>element[-2]</code>? Given those answers, explain what <code>element[1:-1]</code> does.</p>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="turn-a-string-into-a-list"><span class="glyphicon glyphicon-pencil"></span>Turn a string into a list</h2>
</div>
<div class="panel-body">
<p>Use a for-loop to convert the string “hello” into a list of letters:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">[<span class="st">"h"</span>, <span class="st">"e"</span>, <span class="st">"l"</span>, <span class="st">"l"</span>, <span class="st">"o"</span>]</code></pre></div>
<p>Hint: You can create an empty list like this:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">my_list <span class="op">=</span> []</code></pre></div>
</div>
</section>
<blockquote>
<h2 id="using-libraries">Using libraries</h2>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices</h4>
</div>
<div class="panel-body">
<p>From our previous topic challenges, the expression <code>element[3:3]</code> produces an <a href="../../reference.html#empty-string">empty string</a>, i.e., a string that contains no characters. If <code>data</code> holds our array of patient data, what does <code>data[3:3, 4:4]</code> produce? What about <code>data[3:3, :]</code>?</p>
</div>
</section>
<blockquote>
<h2 id="visualising-data-using-libraries">Visualising data using libraries</h2>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="make-your-own-plot"><span class="glyphicon glyphicon-pencil"></span>Make your own plot</h4>
</div>
<div class="panel-body">
<p>Create a plot showing the standard deviation of the inflammation data for each day across all patients. Hint: <code>data.std(axis=0)</code> gives you standard deviation.</p>
</div>
</section>
<blockquote>
<h2 id="making-choices">Making choices</h2>
</blockquote>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="how-many-paths"><span class="glyphicon glyphicon-pencil"></span>How many paths?</h4>
</div>
<div class="panel-body">
<p>Which of the following would be printed if you were to run this code? Why did you pick this answer?</p>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>B and C</li>
</ul>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> <span class="dv">4</span> <span class="op">></span> <span class="dv">5</span>:
<span class="bu">print</span> <span class="st">'A'</span>
<span class="cf">elif</span> <span class="dv">4</span> <span class="op"><=</span> <span class="dv">5</span>:
<span class="bu">print</span> <span class="st">'B'</span>
<span class="cf">elif</span> <span class="dv">4</span> <span class="op"><</span> <span class="dv">5</span>:
<span class="bu">print</span> <span class="st">'C'</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="what-is-truth"><span class="glyphicon glyphicon-pencil"></span>What is truth?</h4>
</div>
<div class="panel-body">
<p><code>True</code> and <code>False</code> aren’t the only values in Python that are true and false. In fact, <em>any</em> value can be used in an <code>if</code> or <code>elif</code>. After reading and running the code below, explain what the rule is for which values are considered true and which are considered false. (Note that if the body of a conditional is a single statement, we can write it on the same line as the <code>if</code>.)</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">if</span> <span class="st">''</span>: <span class="bu">print</span> <span class="st">'empty string is true'</span>
<span class="cf">if</span> <span class="st">'word'</span>: <span class="bu">print</span> <span class="st">'word is true'</span>
<span class="cf">if</span> []: <span class="bu">print</span> <span class="st">'empty list is true'</span>
<span class="cf">if</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]: <span class="bu">print</span> <span class="st">'non-empty list is true'</span>
<span class="cf">if</span> <span class="dv">0</span>: <span class="bu">print</span> <span class="st">'zero is true'</span>
<span class="cf">if</span> <span class="dv">1</span>: <span class="bu">print</span> <span class="st">'one is true'</span></code></pre></div>
</div>
</section>
</div>
</div>
</article>
<div class="footer">
This work is derived from prior works that are Copyright © <a href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
<a class="label swc-blue-bg" href="https://github.com/Southampton-RSG/2016-08-31-Southampton">Source</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>