Alternative zur Klasse Matrix erstellt mit Dictionary.
parent
5b70aa1ac7
commit
2190682401
41
s1_a51.py
41
s1_a51.py
|
@ -1,5 +1,4 @@
|
|||
import collections
|
||||
|
||||
import numpy as np
|
||||
from scipy import sparse
|
||||
|
||||
|
@ -8,16 +7,16 @@ class Matrix:
|
|||
def __init__(self):
|
||||
self.__matrix = collections.defaultdict(dict)
|
||||
|
||||
def set(self, column, row, value):
|
||||
if value == 0:
|
||||
if self.__matrix.get(column) and self.__matrix.get(column).get(row):
|
||||
del self.__matrix[column][row]
|
||||
def set(self, row, column, value):
|
||||
if value != 0:
|
||||
self.__matrix[row][column] = value
|
||||
else:
|
||||
self.__matrix[column][row] = value
|
||||
if self.__matrix.get(row) and self.__matrix.get(row).get(column):
|
||||
del self.__matrix[row][column]
|
||||
|
||||
def get(self, column, row):
|
||||
if self.__matrix.get(column) and self.__matrix.get(column).get(row):
|
||||
return self.__matrix[column][row]
|
||||
def get(self, row, column):
|
||||
if self.__matrix.get(row) and self.__matrix.get(row).get(column):
|
||||
return self.__matrix[row][column]
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
@ -31,15 +30,25 @@ if __name__ == "__main__":
|
|||
[0, 0, 9, 0],
|
||||
[0, 7, 0, 0],
|
||||
[0, 0, 0, 0]]
|
||||
sparse_matrix = Matrix()
|
||||
for column in range(len(input_matrix)):
|
||||
for row in range(len(input_matrix[0])):
|
||||
sparse_matrix.set(column, row, input_matrix[row][column])
|
||||
|
||||
sparse_matrix.print()
|
||||
# sparse_matrix = Matrix()
|
||||
#
|
||||
# for row in range(len(input_matrix)):
|
||||
# for column in range(len(input_matrix[0])):
|
||||
# sparse_matrix.set(row, column, input_matrix[row][column])
|
||||
|
||||
sparce_matrix = {}
|
||||
rows = len(input_matrix)
|
||||
columns = len(input_matrix[0])
|
||||
|
||||
for row in range(rows):
|
||||
for column in range(columns):
|
||||
if input_matrix[row][column] != 0:
|
||||
sparce_matrix[(row, column)] = input_matrix[row][column]
|
||||
|
||||
# Aufgabe 1c
|
||||
# print("\nThe sparce matrix computed without numpy\n" + str(sparse_matrix))
|
||||
|
||||
print("\n The sparce matrix without numpy\n" + str(sparce_matrix))
|
||||
# sparse_matrix.print()
|
||||
# faster way with numpy
|
||||
input_matrix = np.array(input_matrix)
|
||||
rows, columns = input_matrix.shape
|
||||
|
|
|
@ -16,6 +16,7 @@ if __name__ == "__main__":
|
|||
print("Length of the string-list without duplicates: " + str(len(string_list)))
|
||||
print(string_list)
|
||||
|
||||
|
||||
string = "Donaudampfschiffahrtsgesellschaftsstewardess"
|
||||
split_chars_and_remove_duplicates(string)
|
||||
|
||||
|
@ -28,9 +29,11 @@ if __name__ == "__main__":
|
|||
|
||||
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"))
|
||||
|
|
Loading…
Reference in New Issue