- Docstrings: You should include a
docstring
at the beginning of every method, class, and module you write. This will allow documentation to identify what your program is supposed to do.
- Constants Naming: Any constants that you have in your program (eg.
cube
) should be in UPPERCASE, like so: CUBE
.
- Main Guard: Any code that isn't inside a method/class should be inside an
if __name__ == '__main__'
guard. This will prevent this code from being executed if you decide to import this module from other programs. This StackOverflow answer provides more inside and explanation.
- Unnecessary if/else with returns: It is unnecessary to have an
else
after an if
, if you return something in the if
. Simply put the else
return statement outside the if
statement. This return will be run if the if
evaluates to be False
, which removes the necessity for the else
statement.
- Reserved Names: You shouldn't use reserved names for variable names, such as
iter
and slice
, to avoid confusion.
- Variable/Parameter Naming: You have a parameter named
iter
which you use as the number of times you iterate over the cube. It took a bit to identify this, as I see iter
as an iterator, not the number of times you iterate. Renaming it iterations
makes it clearer about what that parameter is supposed to be.
Updated Code
"""
Module Docstring
A description of your program goes here
"""
def updated_trajectory_component(row):
"""
Updated the trajectory component of the passed row
:param row: Row to be updated
"""
num = 1
return row[0] + sum([num / i for i in row[1:]])
def updated_distance_component(row):
"""
Updated the distance component of the passed row
:param row: Row to be updated
"""
return [i - row[0] for i in row[1:]]
def iterate_row(row):
"""
Iterates over the passed row
:param row: Row to be iterated
"""
dist = updated_distance_component(row)
return [updated_trajectory_component(row)] + dist
def iterate_slice(_slice):
"""
Iterates over the rows in the passed slice
:param _slice: Slice to be iterated over
"""
return [iterate_row(i) for i in _slice]
def iterate_cube(cube):
"""
Iterates over the slices in the passed cube
:param cube: Cube to be iterated over
"""
return [iterate_slice(i) for i in cube]
def num_iterator(cube, iterations):
"""
Recursive iterator, iterates over the cube `iterations` times
:param cube: Cube to iterate
:param iterations: Number of times to iterate
"""
print(cube)
if iterations == 1:
return iterate_cube(cube)
return num_iterator(iterate_cube(cube), iterations - 1)
if __name__ == '__main__':
CUBE = [
[
[0, 0.1, 1],
[0, 1, 0.1],
[0, 1, 1]
],
[
[0, 0.1, 1],
[0, -1, 1],
[0, -1, 0.1]
],
[
[0, -1, -1],
[0, 0.1, -1],
[0, -1, 0.1]
]
]
#Sample Cases
num_iterator(CUBE, 5)
num_iterator(CUBE, 2)
num_iterator(CUBE, 7)
num_iterator(CUBE, 4)