Skip to content

Commit

Permalink
Merge pull request #140 from realpython/jima/PythonPracticeProbs
Browse files Browse the repository at this point in the history
Python Practice Problems unit test stubs
  • Loading branch information
jima80525 authored Aug 13, 2020
2 parents c18a5b4 + bf38c4f commit 705d8c0
Show file tree
Hide file tree
Showing 6 changed files with 1,020 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python-practice-problems/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Python Practice Problems

Unittest stubs for ["Python Practice Problems."](https://realpython.com/python-practice-problems/)

## Running the Tests

To run the test for a given problem, use `unittest` from the Python standard library;

```console
$ python -m unittest integersums.py
```

The above example will run the unit tests for the first practice problem.
71 changes: 71 additions & 0 deletions python-practice-problems/caesar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
""" Caesar Cipher
A caesar cipher is a simple substitution cipher where each letter of the
plain text is substituted with a letter found by moving 'n' places down the
alphabet. For an example, if the input plain text is:
abcd xyz
and the shift value, n, is 4. The encrypted text would be:
efgh bcd
You are to write a function which accepts two arguments, a plain-text
message and a number of letters to shift in the cipher. The function will
return an encrypted string with all letters being transformed while all
punctuation and whitespace remains unchanged.
Note: You can assume the plain text is all lowercase ascii, except for
whitespace and punctuation.
"""
import unittest


def caesar(plain_text, shift_num=1):
# TODO: Your code goes here!
result = plain_text
return result


class CaesarTestCase(unittest.TestCase):
def test_a(self):
start = "aaa"
result = caesar(start, 1)
self.assertEqual(result, "bbb")
result = caesar(start, 5)
self.assertEqual(result, "fff")

def test_punctuation(self):
start = "aaa.bbb"
result = caesar(start, 1)
self.assertEqual(result, "bbb.ccc")
result = caesar(start, -1)
self.assertEqual(result, "zzz.aaa")

def test_whitespace(self):
start = "aaa bb b"
result = caesar(start, 1)
self.assertEqual(result, "bbb cc c")
result = caesar(start, 3)
self.assertEqual(result, "ddd ee e")

def test_wraparound(self):
start = "abc"
result = caesar(start, -1)
self.assertEqual(result, "zab")
result = caesar(start, -2)
self.assertEqual(result, "yza")
result = caesar(start, -3)
self.assertEqual(result, "xyz")

start = "xyz"
result = caesar(start, 1)
self.assertEqual(result, "yza")
result = caesar(start, 2)
self.assertEqual(result, "zab")
result = caesar(start, 3)
self.assertEqual(result, "abc")


if __name__ == "__main__":
unittest.main()
33 changes: 33 additions & 0 deletions python-practice-problems/integersums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
""" Sum of Integers Up To n
Write a function, add_it_up, which returns the sum of the integers from 0
to the single integer input parameter.
The function should return 0 if a non-integer is passed in.
"""
import unittest


def add_it_up(n):
# TODO: Your code goes here!
return n


class IntegerSumTestCase(unittest.TestCase):
def test_to_ten(self):
results = [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
for n in range(10):
self.assertEqual(add_it_up(n), results[n])

def test_string(self):
self.assertEqual(add_it_up("testing"), 0)

def test_float(self):
self.assertEqual(add_it_up(0.124), 0)

def test_negative(self):
self.assertEqual(add_it_up(-19), 0)


if __name__ == "__main__":
unittest.main()
17 changes: 17 additions & 0 deletions python-practice-problems/logparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
""" log parser
Accepts a filename on the command line. The file is a linux-like log file
from a system you are debugging. Mixed in among the various statements are
messages indicating the state of the device. They look like:
Jul 11 16:11:51:490 [139681125603136] dut: Device State: ON
The device state message has many possible values, but this program only
cares about three: ON, OFF, and ERR.
Your program will parse the given log file and print out a report giving
how long the device was ON, and the time stamp of any ERR conditions.
"""


if __name__ == "__main__":
# TODO: Your code goes here
print("There are no unit tests for logparse.")
100 changes: 100 additions & 0 deletions python-practice-problems/sudokusolve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python3
""" Sudoku Solver
NOTE: A description of the Sudoku puzzle can be found at:
https://en.wikipedia.org/wiki/Sudoku
Given a string in SDM format, described below, write a program to find and
return the solution for the Sudoku puzzle given in the string. The solution
should be returned in the same SDM format as the input.
Some puzzles will not be solvable. In that case, return the string
"Unsolvable".
The general sdx format is described here:
http://www.sudocue.net/fileformats.php
For our purposes, each SDX string will be a sequence of 81 digits, one for
each position on the Sudoku puzzle. Known numbers will be given and unknown
positions will have a zero value.
For example, this string of digits (split onto two lines for readability):
0040060790000006020560923000780610305090004
06020540890007410920105000000840600100
represents this starting Sudoku puzzle:
0 0 4 0 0 6 0 7 9
0 0 0 0 0 0 6 0 2
0 5 6 0 9 2 3 0 0
0 7 8 0 6 1 0 3 0
5 0 9 0 0 0 4 0 6
0 2 0 5 4 0 8 9 0
0 0 7 4 1 0 9 2 0
1 0 5 0 0 0 0 0 0
8 4 0 6 0 0 1 0 0
The unit tests provide may take a while to run, so be patient.
"""
import unittest


def sudoku_solve(input_string):
# TODO: Your code goes here!
return input_string


class SudokuSolverTestCase(unittest.TestCase):
problems = [
"00400607900000060205609230007806103050900040602054089000741092010500"
"0000840600100",
"01640000020000900040000006207023010010000000300308704096000000500080"
"0007000006820",
"04900860500300700000000003000040080006081502000100900001000000000060"
"0400804500390",
"76050000000006000800000040320040080008000003000500100780900000060001"
"0000000003041",
"00060500000302080004509027050000000106200054040000000709806045000604"
"0700000203000",
"40900070500001000000620780020000000900370420080000000400280150000006"
"0000905000406",
"00001003004007050100200800668000000300030200030000004520050080080104"
"0020090020000",
"08007003026005001800000040000060200039001008600070900000400080081004"
"0052050090070",
"00009300600080090002000610000008005300600020037005000000250004000100"
"9000700130007",
]
expected = [
"28413657991375468275689234147896123553928741662154389736741592819532"
"8764842679153",
"31645297828567931449731856287923415614296578365318724996872143552184"
"3697734596821",
"14923867562395714875814623993547286146781592328136975431679458259268"
"3417874521396",
"76354812942136975895817246329743681518679523434582169781925437663491"
"7582572683941",
"82967531467312489514539827658743692196281754343195268739876145221654"
"9738754283169",
"41963872572851964353624789125418637919375426886792315464289153737146"
"5982985372416",
"76891543294327658151243879668519427317435296832968714523756981485174"
"3629496821357",
"48197623526745391893582146717863254939251478654678932172416589381934"
"7652653298174",
"Unsolvable",
]

def test_solver(self):
for index, problem in enumerate(self.problems):
print(f"Testing puzzle {index+1}")
result = sudoku_solve(problem)
self.assertEqual(result, self.expected[index])


if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 705d8c0

Please sign in to comment.