Skip to content
Snippets Groups Projects
Commit 5c69baca authored by Jon's avatar Jon
Browse files

Create material and solutions for week1 workshop

parent dd311f8d
No related branches found
No related tags found
No related merge requests found
# Week 1 Workshop - Python Basics, Virtual Environments and Numpy
conda create -n cosc350
conda activate cosc350
conda install pip
pip install -e .
\ No newline at end of file
import numpy as np
def multiply_item(original_matrix, r, c, multiplier):
original_matrix[r, c] *= multiplier
return original_matrix
my_matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("Your newly created matrix:", my_matrix)
result_matrix = multiply_item(my_matrix, 1, 1, 2)
print("Your matrix now:", my_matrix)
print("Resulting matrix:", result_matrix)
\ No newline at end of file
first_list = [8, 10, 9]
second_list = [5, 7, 1, 2]
lambdas = []
for first_item in first_list:
for second_item in second_list:
cur_lambda = lambda: first_item + second_item
lambdas.append(cur_lambda)
results = []
for f in lambdas:
results.append(f())
print("Results: ", results)
\ No newline at end of file
import numpy as np
def main():
# Place your code for matrix operations here
pass
if __name__ == '__main__':
main()
\ No newline at end of file
# Week 1 Workshop - Proposed Solutions
\ No newline at end of file
import numpy as np
def multiply_item(original_matrix, r, c, multiplier):
# we need to create a copy of the original matrix
# so that we do not risk to modify it
matrix_copy = original_matrix.copy()
# now we can modify the item in the copy of the matrix
matrix_copy[r, c] *= multiplier
# and return it
return matrix_copy
my_matrix = np.array([[1, 2, 3], [4, 5, 6]])
print("Your newly created matrix:", my_matrix)
result_matrix = multiply_item(my_matrix, 1, 1, 2)
print("Your matrix now:", my_matrix)
print("Resulting matrix:", result_matrix)
\ No newline at end of file
first_list = [8, 10, 9]
second_list = [5, 7, 1, 2]
lambdas = []
for first_item in first_list:
for second_item in second_list:
# we need to add two parameters to the lambda function
# and we can pass the desired items as the default
# values for this lambda function
cur_lambda = lambda a=first_item, b=second_item: a + b
lambdas.append(cur_lambda)
results = []
for f in lambdas:
results.append(f())
print("Results: ", results)
\ No newline at end of file
import numpy as np
# creating a function to retrieve valid inputs from the user
def get_valid_input(prompt, validation_function, error_message):
# setting return value to None
value = None
# looping until we do not have a valid value
while value is None:
# retrieving the input from the user
value = input(prompt)
# checking the value fits the validation criteria
if not validation_function(value):
# invalid input
# we notify the user with the error message
print(error_message)
# set value back to None so we loop again
value = None
# returning the valid value
return value
def main():
my_matrix = np.zeros((3, 4), dtype=int)
print("Please, input the values for the matrix")
for r in range(0, my_matrix.shape[0]):
print("Row {0}:".format(r))
for c in range(0, my_matrix.shape[1]):
value = get_valid_input(
"Please, input item {0} for the current row:".format(c),
lambda v: v.isnumeric(),
"Invalid input. The values must be int."
)
# if we are here, it means the value is valid
# we add it to the matrix
my_matrix[r, c] = int(value)
# asking for the operation to perform
valid_operations = ['mean', 'sum', 'std']
operation = get_valid_input(
"Please, select the desired operation to perform (mean, sum or std):",
lambda v: v in valid_operations,
"The selected operation is invalid."
)
# asking for the row / column
valid_choices = ['row', 'colum']
choice = get_valid_input(
"Do you want to compute the operation on a row or on a column?",
lambda v: v in valid_choices,
"Invalid input value. The input must be 'row' or 'column'"
)
# asking for the index
valid_idx_row = range(0, my_matrix.shape[0])
valid_idx_col = range(0, my_matrix.shape[1])
idx = get_valid_input(
"Specify the index of the {0}:".format(choice),
lambda v: int(v) in valid_idx_row if choice == 'row' else int(v) in valid_idx_col,
"Invalid input value. The input must be a valid positive index."
)
idx = int(idx)
if choice == 'row':
items = my_matrix[idx, :]
else:
items = my_matrix[:, idx]
if operation == 'mean':
result = np.mean(items)
elif operation == 'sum':
result = np.sum(items)
else:
result = np.std(items)
print("Your matrix:")
print(my_matrix)
print("The operation '{0}' applied on the {1} at index {2} gives the result {3}".format(operation, choice, idx, result))
if __name__ == '__main__':
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment