-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-print-first-line.Rmd
185 lines (118 loc) · 5.84 KB
/
10-print-first-line.Rmd
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
```{r, include = FALSE}
ottrpal::set_knitr_image_path()
```
# Applied Python Exercise 2
## Goal -- Print the first line in a file
Write Python code that works towards recreating the Bash tool `head`, displaying only the first line in a file: `random_snippet.vcf`
## Learning Objectives
After going through this chapter, students should be able to:
* State the sub-steps needed to meet the coding goal
* Use the following datatypes, structures, and fundamentals to meet the coding goal:
* variable assignment
* strings
* opening a file
* for loop
* integers
* logical expression
* conditional statement
* printing output
## Coding Blueprint
Let's start with and edit the pseudocode from the previous chapter to meet the needs of this chapter
Note: you may write/edit pseudocode anywhere you would like. You will not have to turn in any pseudocode for any chapter. Just remember that since pseudocode doesn't follow the syntax of any one programming language, if you choose to write it in the online python text editor interface or your local python script, you'll want to make sure when you run the script that the pseudocode is commented out (with a `#` in front of it) so that Python does not try to treat it as real code which will cause errors.
Previous chapter's pseudocode:
First, we need to SET the input file <br />
Second, FOR every line in the open file <br />
PRINT the line <br />
END FOR <br />
In this chapter, we want to only print the line if it's the first line of the file. Keeping the for loop, edit the pseudocode, inserting a step that asks which line position it is numerically in the file.
***
<details><summary> ANSWER: </summary>
First, we need to SET the input file <br />
Second, FOR every line in the open file <br />
IF the first line <br />
PRINT the line <br />
END IF <br />
END FOR <br />
</details>
***
### SET the input file
Much like how we reused the pseudocode of the previous chapter, we'll be able to reuse much of the code from the previous chapter. Do you think that defining a variable with the input file name is one of those reusable steps?
***
<details><summary> ANSWER: </summary>
Yes, it is
</details>
***
### FOR every line in the open file
Let's again focus on building the `for` loop structure by considering the 2 components of the loop used in the first Applied Python Exercise chapter.
1. The `for` statement
Do you think that the `for` statement from the previous chapter. is the best pattern for this problem? Consider that the `for` statement in the previous chapter only provided you access to each line (or item) in the file (or collection of items), but did not provide you any information on which line position numerically it was in the file.
***
<details><summary> ANSWER: </summary>
We should use the third `for` statement pattern instead which provides both the item and the item's position or index.
</details>
***
### IF the first line and PRINT the line
2. The body of the `for` loop
Considering that the body of the for loop is where the pseudocode was added, we also need to add to the body of the `for` loop. However, does the code that we already have from the first Applied Python Exercise chapter still work well for this chapter?
***
<details><summary> ANSWER: </summary>
Yes, it does.
</details>
***
What can we use to see if the index or position of the line is the first line in the file?
***
<details><summary> ANSWER: </summary>
We can use a conditional and logical expression such that we'll PRINT the line, but only if it's the first line in the file.
</details>
***
## Building the code
**Create a new Python script**
### SET the input file
To SET what the input file is, we will use the variable assignment code from the first Python Applied Exercise chapter. Within your Python script, write that variable assignment expression.
***
<details><summary> ANSWER: </summary>
```{python, eval = FALSE}
filename = "random_snippet.vcf"
```
</details>
***
### FOR every line in the open file
Consult the notes on the third pattern of the `for` statement and edit the first Applied Python Exercise chapter's `for` statement code to meet the needs of this chapter. Within your python script, write the revised `for` statement.
***
<details><summary> ANSWER: </summary>
```{python, eval = FALSE}
for i, line in enumerate(open(filename)):
```
</details>
***
### IF the first line
Next, edit the body of the `for` loop from the first Applied Python Exercise chapter such that it asks if the line is the first line. Consult the notes on indexing to see what integer you want the index to be equal to (e.g., 0 or 1). Write the conditional and logical expression code within your Python script within the `for` loop body.
***
<details><summary> ANSWER: </summary>
```{python, eval = FALSE}
if i == 0:
```
</details>
***
### PRINT the line
Finally, reuse the code from the first Applied Python Exercise chapter to print the line (when it is the first line). Add this `print()` statement to your Python script, indenting correctly under the conditional statement.
***
<details><summary> ANSWER: </summary>
```{python, eval = FALSE}
print(line.strip('\r\n'))
```
</details>
***
Within your Python script, you should have the four lines of code together which makes our complete intended goal code -- code that prints just the first line in a file.
**Use the command line or the Run button in the online interface to run the Python script and look at the output.**
## Complete Intended Goal Code
***
<details><summary> ANSWER: </summary>
```{python, eval = FALSE}
filename = "random_snippet.vcf" #SET the input filename
for i, line in enumerate(open(filename)): #FOR every line in the open file
if i == 0: #IF the first line
print(line.strip('\r\n')) #PRINT the line
```
</details>
***