Skip to content

Commit 6930921

Browse files
committed
Adding stuff about strings up to slice
1 parent f4b52a2 commit 6930921

1 file changed

Lines changed: 130 additions & 3 deletions

File tree

pretext/functions/funcWithStrsAndConds.ptx

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0"?>
2-
<worksheet xml:id="func_strsconds_groupsub" groupwork="yes" groupsize="4" label="funcWithStrsAndConds">
2+
<worksheet xml:id="fsc_strsconds_groupsub" groupwork="yes" groupsize="4" label="funcWithStrsAndConds">
33
<title>Group Work: Functions with Strings and Conditionals</title>
44
<p>It is best to use a POGIL approach with the following. In POGIL students work
55
in groups on activities and each member has an assigned role. For more information see <url href="https://cspogil.org/Home" visual="https://cspogil.org/Home">https://cspogil.org/Home</url>.</p>
66
<note>
77
<p>If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.</p>
88
</note>
9-
<section xml:id="functions_group-work-functions-with-strings-and-conditionals">
9+
<section xml:id="fsc_group-work-functions-with-strings-and-conditionals">
1010
<title>Learning Objectives</title>
1111
<introduction>
1212
<p>Students will know and be able to do the following.</p>
@@ -73,7 +73,7 @@
7373
</ul>
7474
</p>
7575
</introduction>
76-
<subsection xml:id="fun_str_cond_funct_intro">
76+
<subsection xml:id="fsc_str_cond_funct_intro">
7777
<title>Function Example</title>
7878
<p>A function is a name for one or more lines of code. You first define a function using the <c>def</c> keyword and then execute
7979
it using <c>function_name(arguments)</c>.
@@ -213,5 +213,132 @@ main()
213213
</blocks>
214214
</exercise>
215215
</subsection>
216+
<subsection xml:id="fsc_str_cond_string_def">
217+
<title>String Definition</title>
218+
<p>A <term>string<term> is a sequence of characters enclosed in quotes. In Python you can use pairs of single
219+
or double quotes to enclose a string like in <c>"hello"</c> or <c>'hello'</c>. This is especially useful when a string includes a single quote <c>"they're"</c>. You can even use tripe quotes when
220+
a string covers more than one line. </p>
221+
</subsection>
222+
<subsection xml:id="fsc_str_cond_string_index">
223+
<title>String Indices</title>
224+
<p>You can get a character from a string at an index (position).</<p>
225+
<exercise label="fsc_fitb_pogil_initials">
226+
<statement>
227+
<p>What is the last thing that will be printed when the code below runs? <var/> </p>
228+
</statement>
229+
<setup>
230+
<var>
231+
<condition string="Jk">
232+
<feedback>
233+
<p>It prints the first letter of the first name and last letter of the last name.</p>
234+
</feedback>
235+
</condition>
236+
<condition string=".*">
237+
<feedback>
238+
<p>String indices start with 0 and -1 is the index of the last letter in a string.</p>
239+
</feedback>
240+
</condition>
241+
</var>
242+
</setup>
243+
</exercise>
244+
<exercise label="fsc_ac_pogil_initials_v2">
245+
<statement>
246+
<p>Run the code below to see what it prints. Then fix it to pass the given test. It should return a string with the
247+
first character of the first name and first character of the last name.</p>
248+
</statement>
249+
<program xml:id="fsc_ac_pogil_initials_v2_editor" interactive="activecode" language="python">
250+
<input>
251+
# function definition
252+
def get_initials(first, last):
253+
return first[0] + last[-1]
254+
255+
# function definition
256+
def main():
257+
print(type("Hello"))
258+
print(type('Class'))
259+
print(type(42))
260+
261+
print(get_initials("J'Quan",'Alik'))
262+
263+
264+
# function call
265+
main()
266+
267+
from unittest.gui import TestCaseGui
268+
class myTests(TestCaseGui):
269+
270+
def testOne(self):
271+
self.assertEqual(get_initials("J'Quan",'Alik'),"JA",'''get_initials("J'Quan",'Alik')''')
272+
273+
myTests().main()
274+
</input>
275+
</program>
276+
</exercise>
277+
</subsection>
278+
<subsection xml:id="fsc_string-slices">
279+
<title>String Slices</title>
280+
<exercise label="fsc_fitb_pogil_short_name">
281+
<statement>
282+
<p>What is the last thing that will be printed when the code below runs? <var/> </p>
283+
</statement>
284+
<setup>
285+
<var>
286+
<condition string="Sibs">
287+
<feedback>
288+
<p>The last thing it prints are the first two letters of the first name and the last two letters of the last name.</p>
289+
</feedback>
290+
</condition>
291+
<condition string=".*">
292+
<feedback>
293+
<p>A slice starts with the first number and ends before the second. If the second is left off it goes to the end of the string. The last character in a string is at index -1.</p>
294+
</feedback>
295+
</condition>
296+
</var>
297+
</setup>
298+
</exercise>
299+
<exercise label="fsc_ac_pogil_short_name">
300+
<statement>
301+
<p>Run the code below to see what it prints.</p>
302+
</statement>
303+
<program xml:id="fsc_ac_pogil_short_name_editor" interactive="activecode" language="python">
304+
<input>
305+
# function definition
306+
def get_short_name(first, last):
307+
print(len(first))
308+
print(len(last))
309+
return first[:2] + last[-2:]
310+
311+
# function definition
312+
def main():
313+
print(get_short_name('Simona',"Jacobs"))
314+
315+
# function call
316+
main()
317+
</input>
318+
</program>
319+
</exercise>
320+
<note>
321+
<p>Use the slice [start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start and including all the characters up to just before the end (end - 1).</p>
322+
</note>
323+
<exercise label="fsc_fitb_three_char_slice">
324+
<statement>
325+
<p>Use the slice operator to return the first three characters from the variable <c>dna</c>? <var/> </p>
326+
</statement>
327+
<setup>
328+
<var>
329+
<condition string="dna\[0\:3\]|\[:3\]">
330+
<feedback>
331+
<p>This will return a new string with the characters from index 0 to 2.</p>
332+
</feedback>
333+
</condition>
334+
<condition string=".*">
335+
<feedback>
336+
<p>Look at the note above and try again.</p>
337+
</feedback>
338+
</condition>
339+
</var>
340+
</setup>
341+
</exercise>
342+
</subsection>
216343
</section>
217344
</worksheet>

0 commit comments

Comments
 (0)