main
Thore 2023-03-30 01:29:11 +02:00
parent 808aeba78f
commit 50e720c81a
5 changed files with 50 additions and 95 deletions

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -1,9 +1,10 @@
import math import math
if __name__ == "__main__": if __name__ == "__main__":
row_value = round(math.pi**2/6, 6) # = 1.644934 # 4.1
sum_of_reciprocal_squares = 0 series_value = round(math.pi**2/6, 6)
approximate_value = 0
divider = 1 divider = 1
while row_value != round(sum_of_reciprocal_squares, 6): while series_value != round(approximate_value, 6):
sum_of_reciprocal_squares += 1/divider**2 approximate_value += 1/divider**2
divider += 1 divider += 1
print("After adding the first " + str(divider-1) + " reciprocal square numbers the sum equals " + str(round(sum_of_reciprocal_squares, 6))) print("The sum equals", series_value, "after adding", divider-1, "reciprocal square numbers.")

View File

@ -1,56 +1,24 @@
import collections class SparseMatrix:
import numpy as np
from scipy import sparse
class Matrix:
def __init__(self): def __init__(self):
self.__matrix = collections.defaultdict(dict) self.__matrix = dict()
def set(self, row, column, value): def save(self, matrix) -> None:
if value != 0: for row in range(len(matrix)):
self.__matrix[row][column] = value for col in range(len(matrix[row])):
else: if matrix[row][col] != 0:
if self.__matrix.get(row) and self.__matrix.get(row).get(column): self.__matrix[(row, col)] = matrix[row][col]
del self.__matrix[row][column]
def get(self, row, column): def get(self, row, col) -> int or None:
if self.__matrix.get(row) and self.__matrix.get(row).get(column): if self.__matrix[(row, col)]:
return self.__matrix[row][column] return self.__matrix[(row, col)]
else:
return 0
def print(self): def __str__(self) -> str:
print(self.__matrix) return '\n'.join(map(lambda pair: ': '.join(map(str, pair)), self.__matrix.items()))
if __name__ == "__main__": if __name__ == "__main__":
# Aufgabe 1b # 5.1
input_matrix = [[3, 0, -2, 11], sparse_matrix = SparseMatrix()
[0, 0, 9, 0], sparse_matrix.save([[3, 0, -2, 11], [0, 0, 9, 0], [0, 7, 0, 0], [0, 0, 0, 0]])
[0, 7, 0, 0], print(sparse_matrix)
[0, 0, 0, 0]] print(sparse_matrix.get(0, 0))
# 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("\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
sparse_matrix = sparse.csr_matrix(input_matrix)
print("\nThe sparce matrix computed with numpy\n" + str(sparse_matrix))

View File

@ -1,38 +1,19 @@
if __name__ == "__main__": if __name__ == "__main__":
# Aufgabe 3 # 5.3
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers = numbers[-3:] + numbers[:-3] print(nums[-3:] + nums[:-3])
print(numbers)
# Aufgabe 4 # 5.4
def split_chars_and_remove_duplicates(word: str): def remove_duplicates(word: str):
string_list = list(word) result = "".join(sorted(set(word), key=word.index))
print("Length of the string-list with duplicates: " + str(len(string_list))) print(word, len(word), "\n", result, len(result))
string_set = set(string_list) remove_duplicates("Donaudampfschiffahrtsgesellschaftsstewardess")
string_list = list(string_set)
string_list.sort()
print("Length of the string-list without duplicates: " + str(len(string_list)))
print(string_list)
# 5.5
nums = [[1, 2, 3], [2, 1, 3], [4, 0, 1]]
nums.sort(key=lambda x: x[1])
string = "Donaudampfschiffahrtsgesellschaftsstewardess" # 5.6
split_chars_and_remove_duplicates(string) def get_animal_sound(animal: str) -> str:
animal_dictionary = {"Lion": "Growl", "Dog": "Bark", "Cat": "Meow"}
# Aufgabe 5 return animal_dictionary.get(animal, "Not found.")
list_to_sort = [[1, 2, 3], [2, 1, 3], [4, 0, 1]]
list_to_sort.sort(key=lambda x: x[1]) # in-build-function
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]:
list_to_sort[row], list_to_sort[next_row] = list_to_sort[next_row], list_to_sort[row]
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"))

View File

@ -1,10 +1,7 @@
if __name__ == "__main__": if __name__ == "__main__":
# Aufgabe 6.1 # 6.1
string_with_spaces = "This is a test" print("This is a test".replace(" ", "-"))
string_with_dashes = string_with_spaces.replace(" ", "-")
print(string_with_dashes)
# Aufgabe 6.2 # 6.2
s1 = "Hello, World" s1 = "Hello, World"
s2 = s1[s1.find(","):0:-1] print(s1[s1.find(",")::-1])
print(s2)