diff --git a/week1/README.md b/week1/README.md index 02c62ad599830406adbe55703750d3660b3026ac..ebc19a57dc4aacb4a93b9c115faa95a042b4ed11 100644 --- a/week1/README.md +++ b/week1/README.md @@ -1 +1,7 @@ -# Week 1 Workshop - Python Basics, Virtual Environments and Numpy \ No newline at end of file +# 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 diff --git a/week1/material/buggy_copy.py b/week1/material/buggy_copy.py new file mode 100644 index 0000000000000000000000000000000000000000..3636bbf33aa2fe321b0d20d5d044833ac6a0a532 --- /dev/null +++ b/week1/material/buggy_copy.py @@ -0,0 +1,13 @@ +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 diff --git a/week1/material/lambda_loop.py b/week1/material/lambda_loop.py new file mode 100644 index 0000000000000000000000000000000000000000..748a285671deebeb2b2116cbd858b5900d6d8461 --- /dev/null +++ b/week1/material/lambda_loop.py @@ -0,0 +1,14 @@ +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 diff --git a/week1/material/my_matrix.py b/week1/material/my_matrix.py new file mode 100644 index 0000000000000000000000000000000000000000..33c1daaea78f92cd3c1a898f948a0a180878bdcb --- /dev/null +++ b/week1/material/my_matrix.py @@ -0,0 +1,8 @@ +import numpy as np + +def main(): + # Place your code for matrix operations here + pass + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/week1/solution/README.md b/week1/solution/README.md deleted file mode 100644 index bd1c2b7a1f1d81acdb2864d0344cc45e71676851..0000000000000000000000000000000000000000 --- a/week1/solution/README.md +++ /dev/null @@ -1 +0,0 @@ -# Week 1 Workshop - Proposed Solutions \ No newline at end of file diff --git a/week1/solution/buggy_copy.py b/week1/solution/buggy_copy.py new file mode 100644 index 0000000000000000000000000000000000000000..ae9a31e5e6b36fc8e14596cc1e0ca9f29fcd2b65 --- /dev/null +++ b/week1/solution/buggy_copy.py @@ -0,0 +1,18 @@ +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 diff --git a/week1/solution/lambda_loop.py b/week1/solution/lambda_loop.py new file mode 100644 index 0000000000000000000000000000000000000000..c93c72afd1b583b9703f801382eda732f107d105 --- /dev/null +++ b/week1/solution/lambda_loop.py @@ -0,0 +1,17 @@ +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 diff --git a/week1/solution/my_matrix.py b/week1/solution/my_matrix.py new file mode 100644 index 0000000000000000000000000000000000000000..71ba108b4088dfa3cbac55ea8904b813e92f2829 --- /dev/null +++ b/week1/solution/my_matrix.py @@ -0,0 +1,84 @@ +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