• Stars
    star
    1,383
  • Rank 33,759 (Top 0.7 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code for Tiny Python Projects (Manning, 2020, ISBN 1617297518). Learning Python through test-driven development of games and puzzles.

Introduction

This is the code repository for the Manning Publications book, Tiny Python Projects, by Ken Youens-Clark:

https://www.manning.com/books/tiny-python-projects?a_aid=youens&a_bid=b6485d52

http://tinypythonprojects.com/

There is a directory for each chapter of the book. Each directory contains a test.py program you can use with pytest to check that you have written the program correctly. I have included a short README to describe each exercise. If you have problems writing code (or if you would like to support this project!), the book contains details about the skills you need.

The testing step is integral to writing and solving these challenges as well as to the methodology of the book. I advocate a "test-driven development" mentality where we write tests before we write code. The tests should define what it means for a program to be correct, and then we write programs to satisfy the tests. In this project, I've written all the tests for you, but I also encourage you to write your own functions and tests. You should run the test suite after every change to your program to ensure you are making progress!

Videos

I've been making videos for each chapter on my YouTube channel:

https://www.youtube.com/user/kyclark

Here are the videos I've completed so far:

  • Chapter 1: How to write and test a Python program: How to create a Python program, understanding comments and the shebang, how to make a program executable and install into your $PATH, how to write a main() function, add docstrings, format your code, and run tests.

  • Chapter 2: Crow's Nest: How to write a Python program that accepts a single, positional argument and creates a newly formatted output string.

  • Chapter 3: Picnic: Writing a Python program that accepts multiple string arguments and formats the results depending on the number of items.

  • Chapter 4: Jump The Five: Writing a Python program to encode the numerals in a given text using an algorithm called "Jump The Five." Use of a dictionary as a lookup table, characters not in the dictionary remain unchanged. Introduction to encoding/decoding text, basic idea of encryption.

  • Chapter 5: Howler: Writing a Python program that can process input text either from the command line or from a file.The output prints either to STDOUT or to a file. Learning about "os.path.isfile", how to "open" a file handle for reading/writing, how to read/write the contents of a file.

  • Chapter 6: Word Count: Writing a Python program to emulate the wc (word count) program. Validates and processes multiple file inputs as well as STDIN and creates output of the counts of lines, words, and bytes for each file optionally with a "total" if more than one file is provided.

  • Chapter 7: Gashlycrumb: Writing a Python program that processes an input file to build a lookup table (dictionary) that is used with multiple positional arguments to translate to the values from the file.

  • Chapter 8: Apples and Bananas: Writing a Python program to find and replace elements in a string. Exploring multiple ways to write the same idea from for loops to list comprehensions to higher-order functions like map().

  • Chapter 9: Abuse: Writing a Python program to generate Shakespearean insults by randomly combining some number of adjectives with a randomly chosen noun. Learning about randomness, seeds, testing, how to use triple-quoted strings.

  • Chapter 10: Telephone: Using probabalistic and deterministc approaches to randomly mutating a string.

  • Chapter 11: Bottles of Beer: Writing a Python program to produce the verse to the "99 Bottles of Beer" song from a given starting point. Learning to count down, format strings, algorithm design. A focus on writing a function and unit test, exploring ways to incorporate our function to generate the verses from for loops to list comprehensions to map().

  • Chapter 12: Ransom: Writing a Python program that will randomly capitalize letters in a given piece of text for the nefarious purpose of creating a ransom note. Exploration of for loops, list comprehensions, and the map() function.

  • Chapter 13: Twelve Days of Christmas: Writing a Python program to create the verses for "The Twelve Days of Christmas" from a given day. Learning how to write a function and the test for it, then using the function in a list comprehension and a map to generate the output.

  • Chapter 14: The Rhymer: Writing a Python program that can split off any initial consonants from a word and append a list of prefixes to create new rhyming "words." Exploration of regular expressions to handle words with no initial consonants, with one or more leading consonants, and nothing but consonants. Writing a stemmer() function and the test_stemmer() function to understand it. Using list comprehensions with guard statements and how that relates to the filter() function.

  • Chapter 15: The Kentucky Friar: In this chapter we delve further into regular expressions, first learning how to split a string using a regex so we can separate things that look like "words" from non-words like punctuation and whitespace. Then we try to identify the word "you" (case-insensitive) to turn into "y'all" and any 2-syllable words ending in "-ing" so we can replace the final "g" with an apostrophe so that "cooking" becomes "cookin'" but "swing" would remain "swing." We then apply this to an entire body of text to Kentucky fry the words with amusing results.

  • Chapter 16: The Scrambler: Writing a Python program to find each "word" in a body of text and then scramble the letters such that the first and last letters remain in place, then reconstructing the text for output. Using regular expressions to split text, using random.shuffle() and understanding in-place mutation vs returning a new value. Comparing for loops to list comprehensions and the "map()" function.

  • Chapter 17: Mad Libs: Writing a Python program to play the classic Mad Libs game. Reading an input file with placeholders for parts of speech like "adjective" or "noun." Getting the inputs to replace those from the user interactively via the "input()" function or taking them from the command-line arguments. Using regular expressions to find and replace the placeholders. Learning about greedy regex and how to make them not greedy. Using the re.findall() and re.sub() functions. Using sys.exit() to prematurely exit a program with an error message/value.

Forking GitHub repo

First use the GitHub interface to "fork" this repository into your own account. Then do git clone of your repository to get a local copy. Inside that checkout, do:

git remote add upstream https://github.com/kyclark/tiny_python_projects.git 

This will allow you to git pull upstream master in order to get updates. When you create new files, git add/commit/push them to your repository. (Please do not create pull requests on my repository -- unless, of course, you have suggestions for improving my repo!).

Copyright

ยฉ Ken Youens-Clark 2019-2020

More Repositories

1

command-line-rust

Code for Command-Line Rust (O'Reilly, 2022, ISBN 9781098109417) https://learning.oreilly.com/library/view/command-line-rust/9781098109424/
Rust
1,422
star
2

biofx_python

Code for Mastering Python for Bioinformatics (O'Reilly, 2021, ISBN 9781098100889)
Python
191
star
3

sysadminshow

Code for Sysadmin Show Podcast
Python
89
star
4

new.py

Python program to write new Python programs
Python
38
star
5

make-tutorial

Tutorial on using Makefiles
Makefile
26
star
6

bioinformatics_primer

Bioinformatics with Unix, bash, Make, Python, and parallel
Python
20
star
7

metagenomics-book

Example code from metagenomics book (https://www.gitbook.com/book/kyclark/metagenomics/details)
Python
15
star
8

more_tiny_python_projects

Second volume of exercises
Python
11
star
9

excel2txt-py

Excel to delimited text (Python version)
Python
4
star
10

cutr

Rust version of `cut` that respects delimiters
Rust
4
star
11

rust-algorithms

Learning Algorithms in Rust
Rust
3
star
12

parallelprocs

Call GNU Parallel from Python
Python
3
star
13

commr

Rust version of `comm`
Rust
3
star
14

command_line_python

Command-Line Python
Python
2
star
15

remote.python.pizza

Python Pizza presentation and code
Python
2
star
16

csvchk

Rust version of csvchk
Rust
2
star
17

abe487

Perl
2
star
18

resbaz2020

Presentation for UA ResBaz 2020
Python
1
star
19

rust-paste

Rust version of paste
Rust
1
star
20

ecol596

R
1
star
21

rust-csvchk

CSV/Tab check
Rust
1
star
22

lebauer-testing

Testing ideas for LeBauer projects
Python
1
star
23

rust-atomics

Rust
1
star
24

excel2txt-rust

Rust implementation of excel2txt
Rust
1
star
25

csvchk-py

Check one record of a delimited text file
Python
1
star
26

rosalind.info

Solutions to rosalind.info problems.
Python
1
star
27

genbank-parser

genbank-parser
Perl
1
star
28

excel2txt

Excel to text
Perl
1
star
29

norvig_ngrams

Python3 port of Peter Norvig's ngrams code
Python
1
star
30

rust-hangman

Rust implementation of Hangman
Rust
1
star
31

dire

Python equivalent of Perl's warn/die functions
Python
1
star
32

greweln

Example code
Rust
1
star