You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book.md
+180-6Lines changed: 180 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4572,7 +4572,148 @@ You lose, loser! The word was "metromania."
4572
4572
4573
4573
\newpage
4574
4574
4575
-
# Chapter 33: Markov Chain
4575
+
# Chapter 33: First Bank of Change
4576
+
4577
+
Write a Python program called `fboc.py` that will figure out all the different combinations of pennies, nickels, dimes, and quarters in a given `value` provided as a single positional argument. The value must be greater than 0 and less than or equal to 100.
4578
+
4579
+
````
4580
+
$ ./fboc.py
4581
+
usage: fboc.py [-h] int
4582
+
fboc.py: error: the following arguments are required: int
4583
+
$ ./fboc.py -h
4584
+
usage: fboc.py [-h] int
4585
+
4586
+
First Bank of Change
4587
+
4588
+
positional arguments:
4589
+
int Sum
4590
+
4591
+
optional arguments:
4592
+
-h, --help show this help message and exit
4593
+
$ ./fboc.py 0
4594
+
usage: fboc.py [-h] int
4595
+
fboc.py: error: value "0" must be > 0 and <= 100
4596
+
$ ./fboc.py 124
4597
+
usage: fboc.py [-h] int
4598
+
fboc.py: error: value "124" must be > 0 and <= 100
Write a Python program called `markov.py` that takes one or more text files as positional arguments for training. Use the `-n|--num_words` argument (default `2`) to find clusters of words and the words that follow them, e.g., in "The Bustle" by Emily Dickinson:
4578
4719
@@ -4779,7 +4920,7 @@ Advice and Consent of the United States.
4779
4920
4780
4921
\newpage
4781
4922
4782
-
# Chapter 34: Hamming Chain
4923
+
# Chapter 35: Hamming Chain
4783
4924
4784
4925
Write a Python program called `chain.py` that takes a `-s|--start` word and searches a `-w|--wordlist` argument (default `/usr/local/share/dict`) for words no more than `-d|--max_distance` Hamming distance for some number of `-i|--iteration` (default `20`). Be sure to accept a `-S|--seed` for `random.seed`.
4785
4926
@@ -4982,7 +5123,7 @@ Failed to find more words!
4982
5123
4983
5124
\newpage
4984
5125
4985
-
# Chapter 35: Morse Encoder/Decoder
5126
+
# Chapter 36: Morse Encoder/Decoder
4986
5127
4987
5128
Write a Python program called `morse.py` that will encrypt/decrypt text to/from Morse code. The program should expect a single positional argument which is either the name of a file to read for the input or the character `-` to indicate reading from STDIN. The program should also take a `-c|--coding` option to indicate use of the `itu` or standard `morse` tables, `-o|--outfile` for writing the output (default STDOUT), and a `-d|--decode` flag to indicate that the action is to decode the input (the default is to encode it).
4988
5129
@@ -5200,7 +5341,7 @@ THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
5200
5341
5201
5342
\newpage
5202
5343
5203
-
# Chapter 36: ROT13 (Rotate 13)
5344
+
# Chapter 37: ROT13 (Rotate 13)
5204
5345
5205
5346
Write a Python program called `rot13.py` that will encrypt/decrypt input text by shifting the text by a given `-s|--shift` argument or will move each character halfway through the alphabet, e.g., "a" becomes "n," "b" becomes "o," etc. The text to rotate should be provided as a single positional argument to your program and can either be a text file, text on the command line, or `-` to indicate STDIN so that you can round-trip data through your program to ensure you are encrypting and decrypting properly.
5206
5347
@@ -5365,7 +5506,7 @@ The quick brown fox jumps over the lazy dog.
5365
5506
5366
5507
\newpage
5367
5508
5368
-
# Chapter 37: Tranpose ABC Notation
5509
+
# Chapter 38: Tranpose ABC Notation
5369
5510
5370
5511
Write a Python program called `transpose.py` that will read a file in ABC notation (https://en.wikipedia.org/wiki/ABC_notation) and transpose the melody line up or down by a given `-s|--shift` argument. Like the `rot13` exercise, it might be helpful to think of the space of notes (`ABCDEFG`) as a list which you can roll through. For instance, if you have the note `c` and want to transpose up a (minor) third (`-s 3`), you would make the new note `e`; similarly if you have the note `F` and you go up a (major) third, you get `A`. You will not need to worry about the actual number of semitones that you are being asked to shift, as the previous example showed that we might be shifting by a major/minor/augmented/diminished/pure interval. The purpose of the exercise is simply to practice with lists.
5371
5512
@@ -5561,7 +5702,7 @@ aba agE | g2g gab | cba agE |1 gED DEg :|2 gED DBG |]
5561
5702
5562
5703
\newpage
5563
5704
5564
-
# Chapter 38: Word Search
5705
+
# Chapter 39: Word Search
5565
5706
5566
5707
Write a Python program called `search.py` that takes a file name as the single positional argument and finds the words hidden in the puzzle grid.
5567
5708
@@ -6190,6 +6331,39 @@ number = 2
6190
6331
files = foo, bar
6191
6332
````
6192
6333
6334
+
## Choices
6335
+
6336
+
Sometimes you want to limit the values of an argument. You can pass in a `list` of valid values to the `choices` option.
The `argparse` module reserves the `-h` and `--help` flags for generating help documentation. You do not need to add these nor are you allowed to use these flags for other purposes. Using the above definition, this is the help that `argparse` will generate:
0 commit comments