diff --git a/03_containers_and_collections/03_dict.rst b/03_containers_and_collections/03_dict.rst index 3dbeab4..c9091c8 100644 --- a/03_containers_and_collections/03_dict.rst +++ b/03_containers_and_collections/03_dict.rst @@ -1,44 +1,48 @@ ->>> def most_significant(value): -... while value >= 10: -... value //= 10 -... return value - ->>> most_significant(12345) -1 ->>> most_significant(99) -9 ->>> most_significant(0) -0 +.. code-block:: python + + >>> def most_significant(value): + ... while value >= 10: + ... value //= 10 + ... return value + + >>> most_significant(12345) + 1 + >>> most_significant(99) + 9 + >>> most_significant(0) + 0 ------------------------------------------------------------------------------ ->>> def add(collection, key, value): -... index = most_significant(key) -... collection[index].append((key, value)) - ->>> def contains(collection, key): -... index = most_significant(key) -... for k, v in collection[index]: -... if k == key: -... return True -... return False - -# Create the collection of 10 lists ->>> collection = [[], [], [], [], [], [], [], [], [], []] - -# Add some items, using key/value pairs ->>> add(collection, 123, 'a') ->>> add(collection, 456, 'b') ->>> add(collection, 789, 'c') ->>> add(collection, 101, 'c') - -# Look at the collection ->>> collection -[[], [(123, 'a'), (101, 'c')], [], [], - [(456, 'b')], [], [], [(789, 'c')], [], []] - -# Check if the contains works correctly ->>> contains(collection, 123) -True ->>> contains(collection, 1) -False +.. code-block:: python + + >>> def add(collection, key, value): + ... index = most_significant(key) + ... collection[index].append((key, value)) + + >>> def contains(collection, key): + ... index = most_significant(key) + ... for k, v in collection[index]: + ... if k == key: + ... return True + ... return False + + Create the collection of 10 lists + >>> collection = [[], [], [], [], [], [], [], [], [], []] + + Add some items, using key/value pairs + >>> add(collection, 123, 'a') + >>> add(collection, 456, 'b') + >>> add(collection, 789, 'c') + >>> add(collection, 101, 'c') + + Look at the collection + >>> collection + [[], [(123, 'a'), (101, 'c')], [], [], + [(456, 'b')], [], [], [(789, 'c')], [], []] + + Check if the contains works correctly + >>> contains(collection, 123) + True + >>> contains(collection, 1) + False diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a127a51 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Rick van Hattem + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.rst b/README.rst index c13c265..15d3145 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,7 @@ Code samples from Mastering Python (https://www.packtpub.com/application-development/mastering-python) All of the code in this repository is tested using the bundled tests. To run -the tests yourself simply install the requirements and run the tests: +the tests yourself you just have to install the requirements and run the tests: pip3 install --upgrade --requirement requirements.txt py.test