1# ASSIGN MICE TO HOLES
2
3def assign_hole(mice: list, holes: list): 4 if len(mice) != len(holes):
5 return "Number of mice and holes not the same"
6
1# ASSIGN MICE TO HOLES
2
3def assign_hole(mice: list, holes: list): 4 if len(mice) != len(holes):
5 return "Number of mice and holes not the same"
6
5y = np.array([[2, 3], [3, 4]])
6
7
8def strassen_iter(x: list, y: list): 9 a, b, c, d, = x[0, 0,], x[0, 1], x[1, 0], x[1, 1]
10 e, f, g, h, = y[0, 0,], y[0, 1], y[1, 0], y[1, 1]
11
5y = np.array([[2, 3], [3, 4]])
6
7
8def strassen_iter(x: list, y: list): 9 a, b, c, d, = x[0, 0,], x[0, 1], x[1, 0], x[1, 1]
10 e, f, g, h, = y[0, 0,], y[0, 1], y[1, 0], y[1, 1]
11
1# Search and sort
2
3# return index position of searching element
4def simple_search(arr: list, target: int) -> int: 5 for i in range(len(arr)):
6 if arr[i] == target:
7 return i
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()