Skip to content

Commit 705d8c0

Browse files
authored
Merge pull request #140 from realpython/jima/PythonPracticeProbs
Python Practice Problems unit test stubs
2 parents c18a5b4 + bf38c4f commit 705d8c0

File tree

6 files changed

+1020
-0
lines changed

6 files changed

+1020
-0
lines changed

python-practice-problems/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Python Practice Problems
2+
3+
Unittest stubs for ["Python Practice Problems."](https://realpython.com/python-practice-problems/)
4+
5+
## Running the Tests
6+
7+
To run the test for a given problem, use `unittest` from the Python standard library;
8+
9+
```console
10+
$ python -m unittest integersums.py
11+
```
12+
13+
The above example will run the unit tests for the first practice problem.

python-practice-problems/caesar.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
""" Caesar Cipher
3+
A caesar cipher is a simple substitution cipher where each letter of the
4+
plain text is substituted with a letter found by moving 'n' places down the
5+
alphabet. For an example, if the input plain text is:
6+
7+
abcd xyz
8+
9+
and the shift value, n, is 4. The encrypted text would be:
10+
11+
efgh bcd
12+
13+
You are to write a function which accepts two arguments, a plain-text
14+
message and a number of letters to shift in the cipher. The function will
15+
return an encrypted string with all letters being transformed while all
16+
punctuation and whitespace remains unchanged.
17+
18+
Note: You can assume the plain text is all lowercase ascii, except for
19+
whitespace and punctuation.
20+
"""
21+
import unittest
22+
23+
24+
def caesar(plain_text, shift_num=1):
25+
# TODO: Your code goes here!
26+
result = plain_text
27+
return result
28+
29+
30+
class CaesarTestCase(unittest.TestCase):
31+
def test_a(self):
32+
start = "aaa"
33+
result = caesar(start, 1)
34+
self.assertEqual(result, "bbb")
35+
result = caesar(start, 5)
36+
self.assertEqual(result, "fff")
37+
38+
def test_punctuation(self):
39+
start = "aaa.bbb"
40+
result = caesar(start, 1)
41+
self.assertEqual(result, "bbb.ccc")
42+
result = caesar(start, -1)
43+
self.assertEqual(result, "zzz.aaa")
44+
45+
def test_whitespace(self):
46+
start = "aaa bb b"
47+
result = caesar(start, 1)
48+
self.assertEqual(result, "bbb cc c")
49+
result = caesar(start, 3)
50+
self.assertEqual(result, "ddd ee e")
51+
52+
def test_wraparound(self):
53+
start = "abc"
54+
result = caesar(start, -1)
55+
self.assertEqual(result, "zab")
56+
result = caesar(start, -2)
57+
self.assertEqual(result, "yza")
58+
result = caesar(start, -3)
59+
self.assertEqual(result, "xyz")
60+
61+
start = "xyz"
62+
result = caesar(start, 1)
63+
self.assertEqual(result, "yza")
64+
result = caesar(start, 2)
65+
self.assertEqual(result, "zab")
66+
result = caesar(start, 3)
67+
self.assertEqual(result, "abc")
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
""" Sum of Integers Up To n
3+
Write a function, add_it_up, which returns the sum of the integers from 0
4+
to the single integer input parameter.
5+
6+
The function should return 0 if a non-integer is passed in.
7+
"""
8+
import unittest
9+
10+
11+
def add_it_up(n):
12+
# TODO: Your code goes here!
13+
return n
14+
15+
16+
class IntegerSumTestCase(unittest.TestCase):
17+
def test_to_ten(self):
18+
results = [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
19+
for n in range(10):
20+
self.assertEqual(add_it_up(n), results[n])
21+
22+
def test_string(self):
23+
self.assertEqual(add_it_up("testing"), 0)
24+
25+
def test_float(self):
26+
self.assertEqual(add_it_up(0.124), 0)
27+
28+
def test_negative(self):
29+
self.assertEqual(add_it_up(-19), 0)
30+
31+
32+
if __name__ == "__main__":
33+
unittest.main()

python-practice-problems/logparse.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
""" log parser
3+
Accepts a filename on the command line. The file is a linux-like log file
4+
from a system you are debugging. Mixed in among the various statements are
5+
messages indicating the state of the device. They look like:
6+
Jul 11 16:11:51:490 [139681125603136] dut: Device State: ON
7+
The device state message has many possible values, but this program only
8+
cares about three: ON, OFF, and ERR.
9+
10+
Your program will parse the given log file and print out a report giving
11+
how long the device was ON, and the time stamp of any ERR conditions.
12+
"""
13+
14+
15+
if __name__ == "__main__":
16+
# TODO: Your code goes here
17+
print("There are no unit tests for logparse.")
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python3
2+
""" Sudoku Solver
3+
NOTE: A description of the Sudoku puzzle can be found at:
4+
5+
https://en.wikipedia.org/wiki/Sudoku
6+
7+
Given a string in SDM format, described below, write a program to find and
8+
return the solution for the Sudoku puzzle given in the string. The solution
9+
should be returned in the same SDM format as the input.
10+
11+
Some puzzles will not be solvable. In that case, return the string
12+
"Unsolvable".
13+
14+
The general sdx format is described here:
15+
16+
http://www.sudocue.net/fileformats.php
17+
18+
For our purposes, each SDX string will be a sequence of 81 digits, one for
19+
each position on the Sudoku puzzle. Known numbers will be given and unknown
20+
positions will have a zero value.
21+
22+
For example, this string of digits (split onto two lines for readability):
23+
24+
0040060790000006020560923000780610305090004
25+
06020540890007410920105000000840600100
26+
27+
represents this starting Sudoku puzzle:
28+
29+
0 0 4 0 0 6 0 7 9
30+
0 0 0 0 0 0 6 0 2
31+
0 5 6 0 9 2 3 0 0
32+
33+
0 7 8 0 6 1 0 3 0
34+
5 0 9 0 0 0 4 0 6
35+
0 2 0 5 4 0 8 9 0
36+
37+
0 0 7 4 1 0 9 2 0
38+
1 0 5 0 0 0 0 0 0
39+
8 4 0 6 0 0 1 0 0
40+
41+
The unit tests provide may take a while to run, so be patient.
42+
"""
43+
import unittest
44+
45+
46+
def sudoku_solve(input_string):
47+
# TODO: Your code goes here!
48+
return input_string
49+
50+
51+
class SudokuSolverTestCase(unittest.TestCase):
52+
problems = [
53+
"00400607900000060205609230007806103050900040602054089000741092010500"
54+
"0000840600100",
55+
"01640000020000900040000006207023010010000000300308704096000000500080"
56+
"0007000006820",
57+
"04900860500300700000000003000040080006081502000100900001000000000060"
58+
"0400804500390",
59+
"76050000000006000800000040320040080008000003000500100780900000060001"
60+
"0000000003041",
61+
"00060500000302080004509027050000000106200054040000000709806045000604"
62+
"0700000203000",
63+
"40900070500001000000620780020000000900370420080000000400280150000006"
64+
"0000905000406",
65+
"00001003004007050100200800668000000300030200030000004520050080080104"
66+
"0020090020000",
67+
"08007003026005001800000040000060200039001008600070900000400080081004"
68+
"0052050090070",
69+
"00009300600080090002000610000008005300600020037005000000250004000100"
70+
"9000700130007",
71+
]
72+
expected = [
73+
"28413657991375468275689234147896123553928741662154389736741592819532"
74+
"8764842679153",
75+
"31645297828567931449731856287923415614296578365318724996872143552184"
76+
"3697734596821",
77+
"14923867562395714875814623993547286146781592328136975431679458259268"
78+
"3417874521396",
79+
"76354812942136975895817246329743681518679523434582169781925437663491"
80+
"7582572683941",
81+
"82967531467312489514539827658743692196281754343195268739876145221654"
82+
"9738754283169",
83+
"41963872572851964353624789125418637919375426886792315464289153737146"
84+
"5982985372416",
85+
"76891543294327658151243879668519427317435296832968714523756981485174"
86+
"3629496821357",
87+
"48197623526745391893582146717863254939251478654678932172416589381934"
88+
"7652653298174",
89+
"Unsolvable",
90+
]
91+
92+
def test_solver(self):
93+
for index, problem in enumerate(self.problems):
94+
print(f"Testing puzzle {index+1}")
95+
result = sudoku_solve(problem)
96+
self.assertEqual(result, self.expected[index])
97+
98+
99+
if __name__ == "__main__":
100+
unittest.main()

0 commit comments

Comments
 (0)