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: source/exercises/kata_fourteen.rst
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,7 +212,7 @@ Building the Trigrams dict
212
212
213
213
So you've got a list of words, and you need to build up a dict like one of the above.
214
214
215
-
It time to create a python file and start writting some code!
215
+
It's time to create a python file and start writting some code!
216
216
217
217
.. code-block:: python
218
218
@@ -244,7 +244,7 @@ So how do you actually build up that dict? That's kind of the point of the exerc
244
244
245
245
**Looping through the words**
246
246
247
-
Obviously you need to loop through all the words, so a ``for loop`` makes sense. However, this is a bit tricky. Usually in Python you loop through all the items in a list, and don't worry about the indices:
247
+
Obviously you need to loop through all the words, so a ``for`` loop makes sense. However, this is a bit tricky. Usually in Python you loop through all the items in a list, and don't worry about the indices:
248
248
249
249
.. code-block:: python
250
250
@@ -256,23 +256,23 @@ So contrary to the usual practice, an index can be helpful here:
256
256
257
257
.. code-block:: python
258
258
259
-
for i inlen(words)-2: # why -2 ?
259
+
for i inrange(len(words)-2): # why -2 ?
260
260
pair = words[i:i +2]
261
261
follower = words[i +2]
262
262
263
263
**Adding a pair to the dict:**
264
264
265
265
For each pair in the text, you need to add it to the dict. But:
266
266
267
-
- words[i:i + 2] is a list with two words in it. Can that be used as a key in a dict? (Try it.) If not, how can you make a valid key out of it?
267
+
- ``words[i:i + 2]`` is a list with two words in it. Can that be used as a key in a dict? (Try it.) If not, how can you make a valid key out of it?
268
268
269
269
- As you loop through the text, you will collect pairs of words. Each time, a given pair may already be in the dict.
270
270
271
271
- If the pair is not in the dict, you want to put it in the dict, with value being a list with the follower in it::
272
272
273
273
("may", "I"): ["wish"]
274
274
275
-
- If the pair already is in the dict, then you want to add the follower (the second word in the pair) to the list that's already there
275
+
- If the pair already is in the dict, then you want to add the follower (the second word in the pair) to the list that's already there::
276
276
277
277
("wish", "I"): ["may", "might"]
278
278
@@ -294,7 +294,7 @@ Using the Trigrams dict
294
294
295
295
This is the fun part. Once you have a mapping of word pairs to following words, you can build up some new "fake" text. Re-read the previous sections again to remind yourself of the procedure. Here are a couple of additional hints and questions to consider:
296
296
297
-
- The ```random`` module <https://docs.python.org/3/library/random.html#module-random>`_ is your friend here:
297
+
- The ``random`` module <https://docs.python.org/3/library/random.html#module-random> is your friend here:
298
298
299
299
.. code-block:: python
300
300
@@ -357,7 +357,7 @@ Do get the full trigrams code working first, then play with some of the fancier
357
357
Code Structure
358
358
--------------
359
359
360
-
Break your code down into a handful of separate functions. This way you can test each on its own, and it's easier to refactor one part without messing with the others. For instance, your __main__ block might look something like:
360
+
Break your code down into a handful of separate functions. This way you can test each on its own, and it's easier to refactor one part without messing with the others. For instance, your ``__main__`` block might look something like:
Copy file name to clipboardExpand all lines: source/exercises/mailroom-part2.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ In the first version of mailroom, you generated a letter to a donor who had just
50
50
51
51
In this version of your program, add a function (and a menu item to invoke it), that goes through all the donors in your donor data structure, generates a thank you letter for each donor, and writes each letter to disk as a text file.
Copy file name to clipboardExpand all lines: source/modules/DictsAndSets.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ Calling the dict type object constructor:
57
57
>>>dict(key1=3, key2=5)
58
58
{'key1': 3, 'key2': 5}
59
59
60
-
# creating and empty dict, and then populating it:
60
+
# creating an empty dict, and then populating it:
61
61
>>> d = {}
62
62
>>> d['key1'] =3
63
63
>>> d['key2'] =5
@@ -181,7 +181,7 @@ Traditionally, dictionaries have had no defined order. See this example from Pyt
181
181
Note how I defined the dict in a natural order, but when it gets printed, or you display the keys, they are in a different order.
182
182
183
183
However, In cPython 3.6, the internal implementation was changed, and it *does* happen to preserve order. In cPython 3.6, that is considered an implementation detail -- and you should not count on it! However, as of cPython 3.7, dictionaries preserving order will be part of the language specification. This was declared by Guido on the python-dev mailing list on
0 commit comments