From 6aff4a2b878a6a87e5108fc30058f20caf53f40a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 7 Feb 2022 10:03:39 +0100 Subject: [PATCH] lin --- laboration3/uppgift_3.py | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 laboration3/uppgift_3.py diff --git a/laboration3/uppgift_3.py b/laboration3/uppgift_3.py new file mode 100644 index 0000000..e263aed --- /dev/null +++ b/laboration3/uppgift_3.py @@ -0,0 +1,63 @@ +# Uppgift 3 + +def sum_of_ints2(value_list): + tot = 0 + for inner in value_list: + for item in inner: + if isinstance(item, int): + tot += item + return tot + + +def flatten_list1(value_list): + return [item for inner in value_list for item in inner] + + +def flatten_list2(value_list): + tot = [] + for inner in value_list: + if isinstance(inner, list): + tot.extend(inner) + else: + tot.append(inner) + + return tot + + +def get_first_column(matrix): + tot = [] + for row in matrix: + tot.append(row[0]) + return tot + + +def get_nth_column(n, matrix): + tot = [] + for row in matrix: + tot.append(row[n]) + return tot + + +def get_all_columns(matrix): + cols = [[] for _ in matrix] + + for row in matrix: + for idx, item in enumerate(row): + cols[idx].append(item) + + return cols + + +def scalar_product(vec1, vec2): + prod = 0 + for row in vec1: + for item1, item2 in zip(vec1, vec2): + prod += item1*item2 + return prod + + +def matrix_square(matrix): + tot = [[] for _ in matrix] + for idx, (row, col) in enumerate(zip(matrix, get_all_columns(matrix))): + tot[idx].append(scalar_product(row, col)) + return tot -- 2.30.2