-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4StringExtraction.html
More file actions
56 lines (37 loc) · 5.68 KB
/
4StringExtraction.html
File metadata and controls
56 lines (37 loc) · 5.68 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
<h2 id="problem-the-expression-used-below-splits-a-string-into-two-parts-and-flips-them-extract-this-expression-into-a-function-named-flip_halves-so-we-dont-have-to-rewrite-the-expression-for-every-string-then-replaces-the-existing-expressions-with-function-calls">Problem: The expression used below splits a string into two parts and flips them. Extract this expression into a function named <code>flip_halves</code> so we dont have to rewrite the expression for every string. Then replaces the existing expressions with function calls.</h2>
<pre class="codehilite"><code>print("ABCDEFG"[int(len("ABCDEFG")/2):] + "ABCDEFG"[:int(len("ABCDEFG")/2)])
print("testing"[int(len("testing")/2):] + "testing"[:int(len("testing")/2)])
print("python"[int(len("python")/2):] + "python"[:int(len("python")/2)])</code></pre>
<h3 id="step-0-understanding-the-problem">Step 0: Understanding the problem:</h3>
<p>The goal here is to put the expression that flips the halves of a string into a function. Doing this will reduce the amount of redundant code, which is the whole point of functions. If we wanted to flip the halves of a string a bunch of times (don’t worry about why we’d need to do that), we would have to copy the expression and type in the string every time. If we put this into a function, we could simply call the function, like <code>flip_halves("ABCDEFG")</code>, and it would return the result. This eliminates the need to rewrite code and to memorize or copy/paste the expression. </p>
<h3 id="step-1-understanding-the-expression">Step 1: Understanding the expression:</h3>
<p>The expression we need to use in <code>flip_halves</code> is the <code>"ABCDEFG"[int(len("ABCDEFG")/2):] + "ABCDEFG"[:int(len("ABCDEFG")/2)]</code> part of the first line, where we need to put our string wherever we see “ABCDEFG”. The first part of the expression, <code>"ABCDEFG"[int(len("ABCDEFG")/2):]</code> gets the last half of the string using indexing. The indexing <code>[int(len("ABCDEFG")/2):]</code> begins at <code>int(len("ABCDEFG")/2)</code> which finds the length of the string, divides it by 2 to find the middle, and converts that value to an integer using <code>int()</code> because string indexes must be integers. The end of the indexing (after the colon) is left blank to tell python to go to the end of the string. The second part of the expression <code>"ABCDEFG"[:int(len("ABCDEFG")/2)]</code> gets the first half in a similar way, starting at the beginning by putting nothing before the colon and going until the middle. The <code>+</code> in between these two parts combines the strings generated by the indexing back into one string.</p>
<h3 id="step-2-creating-the-function">Step 2: Creating the function:</h3>
<p>Like in a prior example, <a href="https://canvas.vt.edu/courses/66476/pages/example-make-address" title="Make Address">Make Address</a>, we start by creating the function header. Our function needs to be called <code>flip_halves</code> and it needs to take a single string, which we can just call string. </p>
<pre class="codehilite"><code>def flip_halves(string):</code></pre>
<h3 id="step-3-adding-the-expression">Step 3: Adding the expression:</h3>
<p>Since our function needs to perform the expression above on our parameter, <code>string</code>, we need to replace “ABCDEFG” with <code>string</code>:</p>
<pre class="codehilite"><code>string[int(len(string)/2):] + string[:int(len(string)/2)]</code></pre>
<p>Notice the quotation marks were removed. This is because instead of working with a string literal, <code>"ABCDEFG"</code>, we are working with the variable <code>string</code>. </p>
<p>Now that we have changed the expression to work with the variable string, we need to return the result. This is simply done with the return statement.</p>
<pre class="codehilite"><code>return string[int(len(string)/2):] + string[:int(len(string)/2)]</code></pre>
<p>Once our function hits this return statement, it will end and return the value from the expression to whatever part of the code called our function. Now, our completed <code>flip_halves</code> function is: </p>
<pre class="codehilite"><code>def flip_halves(string):
return string[int(len(string)/2):] + string[:int(len(string)/2)]</code></pre>
<h3 id="step-4-using-our-function">Step 4: Using our function:</h3>
<p>Now that we have a function that will flip the halves of any string we give it, we need to modify the lines given to us to use our function instead of using the expression directly. This is simply done by calling our function, passing it the proper string:</p>
<pre class="codehilite"><code>flip_halves("ABCDEFG")</code></pre>
<p>We also need to print the return value:</p>
<pre class="codehilite"><code>print(flip_halves("ABCDEFG"))</code></pre>
<p>Doing this for every line that we had originally we get:</p>
<pre class="codehilite"><code>print(flip_halves("ABCDEFG"))
print(flip_halves("testing"))
print(flip_halves("python"))</code></pre>
<h3 id="solution">Solution:</h3>
<p>Now we can put it all together, making sure our function is before the lines we call it so python knows it exists:</p>
<pre class="codehilite"><code>def flip_halves(string):
return string[int(len(string)/2):] + string[:int(len(string)/2)]
print(flip_halves("ABCDEFG"))
print(flip_halves("testing"))
print(flip_halves("python"))</code></pre>
<p>As you can see, writing functions for often repeated parts of code makes things a lot less tedious by removing the need to copy and paste, less error prone for the same reason, and makes it easier to read and understand. </p>