init()
parent
808aeba78f
commit
50e720c81a
|
@ -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
|
11
s1_a4.py
11
s1_a4.py
|
@ -1,9 +1,10 @@
|
|||
import math
|
||||
if __name__ == "__main__":
|
||||
row_value = round(math.pi**2/6, 6) # = 1.644934
|
||||
sum_of_reciprocal_squares = 0
|
||||
# 4.1
|
||||
series_value = round(math.pi**2/6, 6)
|
||||
approximate_value = 0
|
||||
divider = 1
|
||||
while row_value != round(sum_of_reciprocal_squares, 6):
|
||||
sum_of_reciprocal_squares += 1/divider**2
|
||||
while series_value != round(approximate_value, 6):
|
||||
approximate_value += 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)))
|
||||
print("The sum equals", series_value, "after adding", divider-1, "reciprocal square numbers.")
|
||||
|
|
66
s1_a51.py
66
s1_a51.py
|
@ -1,56 +1,24 @@
|
|||
import collections
|
||||
import numpy as np
|
||||
from scipy import sparse
|
||||
|
||||
|
||||
class Matrix:
|
||||
class SparseMatrix:
|
||||
def __init__(self):
|
||||
self.__matrix = collections.defaultdict(dict)
|
||||
self.__matrix = dict()
|
||||
|
||||
def set(self, row, column, value):
|
||||
if value != 0:
|
||||
self.__matrix[row][column] = value
|
||||
else:
|
||||
if self.__matrix.get(row) and self.__matrix.get(row).get(column):
|
||||
del self.__matrix[row][column]
|
||||
def save(self, matrix) -> None:
|
||||
for row in range(len(matrix)):
|
||||
for col in range(len(matrix[row])):
|
||||
if matrix[row][col] != 0:
|
||||
self.__matrix[(row, col)] = matrix[row][col]
|
||||
|
||||
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
|
||||
def get(self, row, col) -> int or None:
|
||||
if self.__matrix[(row, col)]:
|
||||
return self.__matrix[(row, col)]
|
||||
|
||||
def print(self):
|
||||
print(self.__matrix)
|
||||
def __str__(self) -> str:
|
||||
return '\n'.join(map(lambda pair: ': '.join(map(str, pair)), self.__matrix.items()))
|
||||
|
||||
|
||||
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 = 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))
|
||||
# 5.1
|
||||
sparse_matrix = SparseMatrix()
|
||||
sparse_matrix.save([[3, 0, -2, 11], [0, 0, 9, 0], [0, 7, 0, 0], [0, 0, 0, 0]])
|
||||
print(sparse_matrix)
|
||||
print(sparse_matrix.get(0, 0))
|
||||
|
|
49
s1_a53_6.py
49
s1_a53_6.py
|
@ -1,38 +1,19 @@
|
|||
if __name__ == "__main__":
|
||||
# Aufgabe 3
|
||||
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
numbers = numbers[-3:] + numbers[:-3]
|
||||
print(numbers)
|
||||
# 5.3
|
||||
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
print(nums[-3:] + nums[:-3])
|
||||
|
||||
# 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)
|
||||
# 5.4
|
||||
def remove_duplicates(word: str):
|
||||
result = "".join(sorted(set(word), key=word.index))
|
||||
print(word, len(word), "\n", result, len(result))
|
||||
remove_duplicates("Donaudampfschiffahrtsgesellschaftsstewardess")
|
||||
|
||||
# 5.5
|
||||
nums = [[1, 2, 3], [2, 1, 3], [4, 0, 1]]
|
||||
nums.sort(key=lambda x: x[1])
|
||||
|
||||
string = "Donaudampfschiffahrtsgesellschaftsstewardess"
|
||||
split_chars_and_remove_duplicates(string)
|
||||
|
||||
# Aufgabe 5
|
||||
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"))
|
||||
# 5.6
|
||||
def get_animal_sound(animal: str) -> str:
|
||||
animal_dictionary = {"Lion": "Growl", "Dog": "Bark", "Cat": "Meow"}
|
||||
return animal_dictionary.get(animal, "Not found.")
|
||||
|
|
11
s1_a61_2.py
11
s1_a61_2.py
|
@ -1,10 +1,7 @@
|
|||
if __name__ == "__main__":
|
||||
# Aufgabe 6.1
|
||||
string_with_spaces = "This is a test"
|
||||
string_with_dashes = string_with_spaces.replace(" ", "-")
|
||||
print(string_with_dashes)
|
||||
# 6.1
|
||||
print("This is a test".replace(" ", "-"))
|
||||
|
||||
# Aufgabe 6.2
|
||||
# 6.2
|
||||
s1 = "Hello, World"
|
||||
s2 = s1[s1.find(","):0:-1]
|
||||
print(s2)
|
||||
print(s1[s1.find(",")::-1])
|
||||
|
|
Loading…
Reference in New Issue