Skip to content

Commit b7fbc23

Browse files
pattern printing pyramids and unit test cases (#85)
* pattern printing pyramids and unit test cases * Changed naming convention pyramid print and documentation added
1 parent 226036d commit b7fbc23

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
4+
def print_pyramid(num_rows: 'int', char: 'str') -> 'None':
5+
"""
6+
Parameters
7+
----------
8+
num_rows : int
9+
The number of rows a pyramid should have
10+
char : str
11+
The character to print out when displaying the pyramid
12+
e.g. using method like print_pyramid(3, &) will print as below:
13+
&
14+
&&
15+
&&&
16+
17+
Returns
18+
----------
19+
None
20+
"""
21+
22+
for i in range(num_rows):
23+
for j in range(i+1):
24+
print(char, end="")
25+
print()
26+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
4+
import unittest
5+
from unittest.mock import patch
6+
from methods.patternprinting import print_pyramid
7+
from io import StringIO
8+
import sys
9+
import contextlib
10+
11+
class TestPrintPyramid(unittest.TestCase):
12+
@patch('builtins.print')
13+
def test_patternprinting_numrows_zero_call_print_zero_times(self, mock_print):
14+
print_pyramid(0, '*')
15+
self.assertEqual(mock_print.call_count, 0)
16+
17+
def test_pyramid_pattern_printing_zerorows_asteriks(self):
18+
expected_output = ''
19+
20+
with StringIO() as buffer, contextlib.redirect_stdout(buffer):
21+
print_pyramid(0, "*")
22+
printed_output = buffer.getvalue()
23+
24+
self.assertEqual(printed_output, expected_output)
25+
26+
@patch('builtins.print')
27+
def test_patternprinting_numrows_three_call_print_twenty_times(self, mock_print):
28+
print_pyramid(5, '*')
29+
self.assertEqual(mock_print.call_count, 20)
30+
31+
def test_pyramid_pattern_printing_fiverows_asteriks(self):
32+
expected_output = (
33+
"*\n"
34+
"**\n"
35+
"***\n"
36+
"****\n"
37+
"*****\n"
38+
)
39+
40+
# Assert that the printed output contains the expected parameter
41+
with StringIO() as buffer, contextlib.redirect_stdout(buffer):
42+
print_pyramid(5, "*")
43+
printed_output = buffer.getvalue()
44+
45+
self.assertEqual(printed_output, expected_output)
46+
47+
@patch('builtins.print')
48+
def test_patternprinting_numrows_three_call_print_nine_times(self, mock_print):
49+
print_pyramid(3, '&')
50+
self.assertEqual(mock_print.call_count, 9)
51+
52+
def test_pyramid_pattern_printing_fiverows_ampersand(self):
53+
expected_output = (
54+
"&\n"
55+
"&&\n"
56+
"&&&\n"
57+
)
58+
59+
# Assert that the printed output contains the expected parameter
60+
with StringIO() as buffer, contextlib.redirect_stdout(buffer):
61+
print_pyramid(3, "&")
62+
printed_output = buffer.getvalue()
63+
64+
self.assertEqual(printed_output, expected_output)
65+
66+
if __name__ == '__main__':
67+
unittest.main()

0 commit comments

Comments
 (0)