Skip to content

Commit 938eb90

Browse files
committed
Python Practice Problems unit test stubs
1 parent c18a5b4 commit 938eb90

File tree

6 files changed

+1007
-0
lines changed

6 files changed

+1007
-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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
import string
23+
24+
25+
def caesar(plain_text, shift_num=1):
26+
# TODO: Your code goes here!
27+
result = plain_text
28+
return result
29+
30+
31+
class CaesarTestCase(unittest.TestCase):
32+
def test_a(self):
33+
start = "aaa"
34+
result = caesar(start, 1)
35+
self.assertEqual(result, "bbb")
36+
result = caesar(start, 5)
37+
self.assertEqual(result, "fff")
38+
39+
def test_punctuation(self):
40+
start = "aaa.bbb"
41+
result = caesar(start, 1)
42+
self.assertEqual(result, "bbb.ccc")
43+
result = caesar(start, -1)
44+
self.assertEqual(result, "zzz.aaa")
45+
46+
def test_whitespace(self):
47+
start = "aaa bb b"
48+
result = caesar(start, 1)
49+
self.assertEqual(result, "bbb cc c")
50+
result = caesar(start, 3)
51+
self.assertEqual(result, "ddd ee e")
52+
53+
def test_wraparound(self):
54+
start = "abc"
55+
result = caesar(start, -1)
56+
self.assertEqual(result, "zab")
57+
result = caesar(start, -2)
58+
self.assertEqual(result, "yza")
59+
result = caesar(start, -3)
60+
self.assertEqual(result, "xyz")
61+
62+
start = "xyz"
63+
result = caesar(start, 1)
64+
self.assertEqual(result, "yza")
65+
result = caesar(start, 2)
66+
self.assertEqual(result, "zab")
67+
result = caesar(start, 3)
68+
self.assertEqual(result, "abc")
69+
70+
71+
if __name__ == "__main__":
72+
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 to
4+
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()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 how
11+
long the device was ON, and the time stamp of any ERR conditions.
12+
"""
13+
import sys
14+
import time
15+
16+
17+
if __name__ == "__main__":
18+
# TODO: Your code goes here
19+
print("There are no unit tests for logparse.")
20+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
"004006079000000602056092300078061030509000406020540890007410920105000000840600100",
54+
"016400000200009000400000062070230100100000003003087040960000005000800007000006820",
55+
"049008605003007000000000030000400800060815020001009000010000000000600400804500390",
56+
"760500000000060008000000403200400800080000030005001007809000000600010000000003041",
57+
"000605000003020800045090270500000001062000540400000007098060450006040700000203000",
58+
"409000705000010000006207800200000009003704200800000004002801500000060000905000406",
59+
"000010030040070501002008006680000003000302000300000045200500800801040020090020000",
60+
"080070030260050018000000400000602000390010086000709000004000800810040052050090070",
61+
"000093006000800900020006100000080053006000200370050000002500040001009000700130007",
62+
]
63+
expected = [
64+
"284136579913754682756892341478961235539287416621543897367415928195328764842679153",
65+
"316452978285679314497318562879234156142965783653187249968721435521843697734596821",
66+
"149238675623957148758146239935472861467815923281369754316794582592683417874521396",
67+
"763548129421369758958172463297436815186795234345821697819254376634917582572683941",
68+
"829675314673124895145398276587436921962817543431952687398761452216549738754283169",
69+
"419638725728519643536247891254186379193754268867923154642891537371465982985372416",
70+
"768915432943276581512438796685194273174352968329687145237569814851743629496821357",
71+
"481976235267453918935821467178632549392514786546789321724165893819347652653298174",
72+
"Unsolvable",
73+
]
74+
75+
def test_solver(self):
76+
for index, problem in enumerate(self.problems):
77+
print(f"Testing puzzle {index+1}")
78+
result = sudoku_solve(problem)
79+
self.assertEqual(result, self.expected[index])
80+
81+
82+
if __name__ == "__main__":
83+
unittest.main()

0 commit comments

Comments
 (0)