Data Structures and Algorithms in Python | Arrays and Strings

Creating a Matrix in Python without numpy

new_matrix  = [ [None]*height ] *  width
test_list = [[]]*5
id(test_list[0]) == id(test_list[1])
new_matrix = []
i = 0
while i < width:
new_matrix.append([]) # add a row
j = height - 1
while j >= 0:
new_matrix[i].append(matrix[j][i])

Unittest Heads Up

‘in-place’ function

When doing unittest for ‘in-place’ function, remember to deepcopy the test set for looping tests, i.e.

from copy import deepcopyfor (_input, _output) in  self.test_cases:
_input = deepcopy(_input) # don't forget this line, because it changes original input that make your right answer to be wrong
for text, expected in self.test_cases:
_input = deepcopy(text) # don't forget this line, because it changes original input that make your right answer to be wrong
for func in self.testable_functions:
start = time.perf_counter()
func(_input)
assert(
_input == expected
), f'{func.__name__} failed for value: {text}, output={_input}, expected={expected}'
for (_input, _output) in test_cases:
_input_copy = _input.copy()
assert zero_matrix(_input_copy) == _output

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store