From fecf7c747fbb04d1fb34524362b20ce3e554ec11 Mon Sep 17 00:00:00 2001 From: Thore <2120752@stud.hs-mannheim.de> Date: Sat, 5 Nov 2022 15:33:32 +0100 Subject: [PATCH] init --- s1_a4.py | 9 +++++++++ s1_a51.py | 26 ++++++++++++++++++++++++++ s1_a53_6.py | 38 ++++++++++++++++++++++++++++++++++++++ s1_a61_2.py | 13 +++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 s1_a4.py create mode 100644 s1_a51.py create mode 100644 s1_a53_6.py create mode 100644 s1_a61_2.py diff --git a/s1_a4.py b/s1_a4.py new file mode 100644 index 0000000..e29f4dc --- /dev/null +++ b/s1_a4.py @@ -0,0 +1,9 @@ +import math +if __name__ == "__main__": + row_value = round(math.pi**2/6, 6) # = 1.644934 + sum_of_reciprocal_squares = 0 + divider = 1 + while row_value != round(sum_of_reciprocal_squares, 6): + sum_of_reciprocal_squares += 1/divider**2 + divider += 1 + print("After adding the first " + str(divider-1) + " reciprocal square numbers the sum equals " + str(round(sum_of_reciprocal_squares, 6))) diff --git a/s1_a51.py b/s1_a51.py new file mode 100644 index 0000000..9b9bf30 --- /dev/null +++ b/s1_a51.py @@ -0,0 +1,26 @@ +import numpy as np +from scipy import sparse + +if __name__ == "__main__": + # Aufgabe 1b + input_matrix = [[3, 0, -2, 11], + [0, 0, 9, 0], + [0, 7, 0, 0], + [0, 0, 0, 0]] + sparse_matrix = [] + for row in range(len(input_matrix)): + for column in range(len(input_matrix[0])): + if input_matrix[row][column] != 0: + # Aufgabe 1d + print("Index: ["+str(row)+"]"+"["+str(column)+"] = "+str(input_matrix[row][column])) + triplet = [row, column, input_matrix[row][column]] # saving index of not null values + sparse_matrix.append(triplet) + + # Aufgabe 1c + print("\nThe sparce matrix computed without numpy\n" + str(sparse_matrix)) + + # faster way with numpy + input_matrix = np.array(input_matrix) + rows, columns = input_matrix.shape + sparse_matrix = sparse.csr_matrix(input_matrix) + print("\nThe sparce matrix computed with numpy\n" + str(sparse_matrix)) diff --git a/s1_a53_6.py b/s1_a53_6.py new file mode 100644 index 0000000..bd797ec --- /dev/null +++ b/s1_a53_6.py @@ -0,0 +1,38 @@ +if __name__ == "__main__": + # Aufgabe 3 + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + for i in range(0, 3): + numbers.insert(0, numbers.pop()) + print(numbers) + + + # Aufgabe 4 + def split_chars_and_remove_duplicates(word: str): + string_list = list(word) + print("Length of the string-list with duplicates: " + str(len(string_list))) + string_set = set(string_list) + string_list = list(string_set) + string_list.sort() + print("Length of the string-list without duplicates: " + str(len(string_list))) + print(string_list) + + string = "Donaudampfschiffahrtsgesellschaftsstewardess" + split_chars_and_remove_duplicates(string) + + # Aufgabe 5 + list_to_sort = [[1, 2, 3], [2, 1, 3], [4, 0, 1]] + for row in range(len(list_to_sort)): + for next_row in range(row + 1, len(list_to_sort)): + if list_to_sort[row][1] > list_to_sort[next_row][1]: + tmp = list_to_sort[row] + list_to_sort[row] = list_to_sort[next_row] + list_to_sort[next_row] = tmp + + print("The sorted list" + str(list_to_sort)) + + # Aufgabe 6 + def get_the_animal_sound(name_of_animal: str): + animal_dictionary = {"Lion": "Growl!", "Dog": "Barks!", "Cat": "Meow!"} + return animal_dictionary.get(name_of_animal, "Animal sound not available") + + print(get_the_animal_sound("Dog")) diff --git a/s1_a61_2.py b/s1_a61_2.py new file mode 100644 index 0000000..c5c9f95 --- /dev/null +++ b/s1_a61_2.py @@ -0,0 +1,13 @@ +if __name__ == "__main__": + # Aufgabe 6.1 + string_with_spaces = "This is a test" + string_with_dashes = string_with_spaces.replace(" ", "-") + print(string_with_dashes) + + # Aufgabe 6.2 + s1 = "Hello, World" + tmp = "" + for current_char in range(s1.find(","), -1, -1): + tmp += s1[current_char] + s1 = tmp + print(s1)