Skip to content

Commit 4a81d0b

Browse files
committed
demo unittest
1 parent 3a3c05b commit 4a81d0b

File tree

13 files changed

+259
-26
lines changed

13 files changed

+259
-26
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# author: greyshell
3+
4+
5+
def solution(arr):
6+
# create a stack
7+
s = list()
8+
9+
for i in range(0, len(arr)):
10+
if arr[i] == '(':
11+
s.append(')')
12+
13+
elif arr[i] == ')':
14+
if len(s) != 0:
15+
out = s[-1]
16+
if out == ')':
17+
s.pop()
18+
else:
19+
s.append('(')
20+
21+
if len(s) == 0:
22+
return True
23+
24+
return False
25+
26+
27+
def main():
28+
# sample test case
29+
arr = ['(', ')']
30+
result = solution(arr)
31+
print(f"{result}")
32+
33+
34+
if __name__ == '__main__':
35+
main()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
# author: greyshell
3+
# how to run: python -m unittest test_blance_bracket.TestSolution
4+
5+
import unittest
6+
from blance_bracket import solution
7+
8+
9+
class TestSolution(unittest.TestCase):
10+
11+
def test_solution_case_1(self):
12+
self.assertEqual(solution(['(', '(']), False)
13+
14+
def test_solution_case_2(self):
15+
self.assertEqual(solution(['(', ')']), True)
16+
17+
def test_solution_case_3(self):
18+
self.assertEqual(solution([')', '(']), False)
19+
20+
def test_solution_case_4(self):
21+
self.assertEqual(solution(['(', '(', ')', ')']), True)
22+
23+
def test_solution_case_5(self):
24+
self.assertEqual(solution([')', ')']), False)
25+
26+
def test_solution_case_6(self):
27+
self.assertEqual(solution(['(', '(', ')', '(', ')']), False)
28+
29+
def test_solution_case_7(self):
30+
self.assertEqual(solution(['(', ')', '(']), False)
31+
32+
def test_solution_case_8(self):
33+
self.assertEqual(solution(['(', ')', ')']), False)
34+
35+
def test_solution_case_9(self):
36+
self.assertEqual(solution(['(', '(', ')', '(', ')', ')']), True)
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
# author: greyshell
3+
# command: python -m unittest test_two_sum.TestSolution
4+
5+
import unittest
6+
from two_sum import LeetCode
7+
8+
9+
class TestSolution(unittest.TestCase):
10+
11+
@classmethod
12+
def setUpClass(cls):
13+
"""
14+
optional ->
15+
run one time before staring the test
16+
can be used to populate a database to set up testing
17+
:return:
18+
"""
19+
print("running setUpClass method")
20+
21+
def setUp(self):
22+
"""
23+
optional ->
24+
run this before every single test function
25+
can be used to add files to a directory / set variables
26+
:return:
27+
"""
28+
print("running setUp method method")
29+
30+
def test_case_1(self):
31+
"""
32+
verify the correctness of the solution
33+
- actual -> means what we are providing into the assert statement
34+
- expected -> means what we are getting from the program
35+
:return:
36+
"""
37+
self.assertEqual(LeetCode.solution([12, 7, 11, 15, 35], 50), [3, 4])
38+
39+
def test_case_2(self):
40+
self.assertEqual(LeetCode.solution([12, 7, 11, 15, 35], 19), [0, 1])
41+
42+
def tearDown(self):
43+
"""
44+
optional ->
45+
run this after every single test
46+
can be used to delete files from directory those created during testing / unset variables
47+
:return:
48+
"""
49+
print("running tearDown method")
50+
51+
@classmethod
52+
def tearDownClass(cls):
53+
"""
54+
optional ->
55+
can be used to clean up the database to start other testing from clean state
56+
:return:
57+
"""
58+
print("running tearDownClass method")
59+
60+
61+
if __name__ == '__main__':
62+
unittest.main()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
# author: greyshell
4+
5+
"""
6+
reference: https://leetcode.com/problems/two-sum/
7+
8+
description:
9+
Given an array of integers, return indices of the two numbers such that
10+
they add up to a specific target.
11+
12+
You may assume that each input would have exactly one two_sum, and you may not
13+
use the same element twice.
14+
15+
Example:
16+
17+
Given nums = [2, 7, 11, 15], target = 9,
18+
19+
Because nums[0] + nums[1] = 2 + 7 = 9,
20+
return [0, 1].
21+
"""
22+
23+
24+
class LeetCode:
25+
@staticmethod
26+
def solution(nums: list, target: int) -> list:
27+
"""
28+
time complexity: O(n)
29+
space complexity: O(n) -> size of the hash table
30+
"""
31+
lookup = {}
32+
33+
for index in range(0, len(nums)):
34+
complement = target - nums[index]
35+
# if the complement is not present in hash_map then
36+
# insert the number as key, index as value
37+
found_value = lookup.get(complement) # O(1), not found return None
38+
if found_value is None:
39+
key = nums[index]
40+
lookup[key] = index
41+
42+
else: # found the complement in the hash_map
43+
index_of_prev_number = lookup[complement]
44+
return [index_of_prev_number, index]
45+
46+
# scenario where target is not found
47+
return [None, None] # returns a list
48+
49+
50+
def main():
51+
# sample test case
52+
nums = [12, 7, 11, 15, 35, 17, 2]
53+
target = 26
54+
55+
result = LeetCode.solution(nums, target)
56+
57+
print(f"{result}") # expected result: [1, 2]
58+
59+
60+
if __name__ == '__main__':
61+
main()
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
# author: greyshell
4+
5+
6+
def add(x, y):
7+
return x + y
8+
9+
10+
def double(x):
11+
return x + x
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
# author: greyshell
3+
4+
"""
5+
note:
6+
7+
module - a file containing python code that can be reused in other python code files.
8+
9+
advantage of module:
10+
1. simplicity
11+
2. maintainability
12+
3. reuability
13+
4. scoping - module has its own namespace
14+
15+
advantage of namesapce:
16+
- group the names into logical containers
17+
- prevent the clashes between the duplicate namespace
18+
- provide context to names
19+
20+
- during function name clash, local / current function only gets executed.
21+
22+
- when the module has only one class then import that directly, for example
23+
from datetime import datetime
24+
25+
best practice is the import module to keep the imported
26+
module separated from the callings modules namespace
27+
"""
28+
29+
from adder import add as myadd
30+
31+
32+
def add(x, y):
33+
return x - y
34+
35+
36+
def main():
37+
value = add(7, 3)
38+
print(value)
39+
40+
value = myadd(7, 3)
41+
print(value)
42+
43+
44+
if __name__ == '__main__':
45+
main()

core_concepts/quxes_fb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# !/usr/bin/env python3
1+
#!/usr/bin/env python3
22

33
# author: greyshell
44

core_concepts/slack_notification.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

core_concepts/test_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# !/usr/bin/env python3
1+
#!/usr/bin/env python3
22

33
# author: greyshell
44
# description: play with dict

0 commit comments

Comments
 (0)