Last active
August 29, 2015 13:56
-
-
Save aparrish/9138691 to your computer and use it in GitHub Desktop.
supplemental exercise for RWET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1) create a program that prints every other line of input. | |
i.e., if you ran the program like so: | |
$ python every_other_line.py <sea_rose.txt | |
it would produce the output | |
Rose, harsh rose, | |
meagre flower, thin, | |
than a wet rose | |
you are caught in the drift. | |
Stunted, with small leaf, | |
you are lifted | |
that drives in the wind. | |
Can the spice-rose | |
hardened in a leaf? | |
(2) modify the program above to print every other word on | |
every other line. i.e., if you ran the program like so: | |
$ python every_other_line_and_word.py <sea_rose.txt | |
it would produce the output | |
Rose, rose, | |
meagre thin, | |
than wet | |
you caught the | |
Stunted, small | |
you lifted | |
that in wind. | |
Can spice-rose | |
hardened a | |
(3) modify your program further, such that you can pass a | |
number on the command line to indicate how many lines and | |
words should be skipped. i.e., if you pass 3, it would | |
print every third word on every third line; if you pass 4 | |
it would print every fourth word on every fourth line, etc. | |
for example: | |
$ python every_n_lines_and_words.py 3 <sea_rose.txt | |
would produce this output | |
Rose, | |
spare | |
than rose | |
you | |
hardened leaf? | |
(running with "2" as the command-line parameter should | |
produce exactly the same output as the program you made in | |
step 2) | |
POTENTIALLY HELPFUL THINGS TO KNOW ABOUT (you don't have to | |
use these, but they might make your task easier) | |
- the modulo operator, %: returns the remainder of doing | |
integer division of two numbers. e.g. | |
>>> 3 % 2 | |
1 | |
>>> 4 % 2 | |
0 | |
- the enumerate function: | |
http://www.quora.com/Asad-Awan/Learn-Python/Looping-through-a-list-using-enumerate | |
- the mysterious "step" parameter for list slices, | |
discussed here: http://effbot.org/zone/python-list.htm | |
(under the section "accessing lists") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment