# Data Structures and Algorithms in Python | Arrays and Strings

2 min readJul 24, 2022

--

In this series and in the next few weeks, I will show you Data Structures and Algorithms in Python by some classic interview solutions. Please take a pen, whiteboard, and your laptop. Let’s start learning!

Cheat sheet to Arrays and Strings. And some heads up for you.

# Creating a Matrix in Python without numpy

`new_matrix = [ [None]*height ] * width`

This way, all the inner list will be pointed to the same address, i.e.

`test_list = [[]]*5`

id(test_list[0]) == id(test_list[1])

Do something like this instead.

`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

or

`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}'

or

`for (_input, _output) in test_cases:`

_input_copy = _input.copy()

assert zero_matrix(_input_copy) == _output

See example

- Data-Structures-and-Algorithms-in-Python/Rotate Matrix.py at main · chienhsiang-hung/Data-Structures-and-Algorithms-in-Python (github.com)
- Data-Structures-and-Algorithms-in-Python/Zero Matrix.py at main · chienhsiang-hung/Data-Structures-and-Algorithms-in-Python (github.com)

Test your script: Hsiang | PyScript Hello World (chienhsiang-hung.github.io)

Contact me: Hung, Chien-Hsiang (chienhsiang-hung.github.io)