Aufgabe 1, 4, 5 und 6 erledigt.

main
Eren Saglam 2022-11-07 17:31:11 +01:00
parent 5925b950b8
commit 9ea671e73f
5 changed files with 127 additions and 0 deletions

20
s1_a4.py 100644
View File

@ -0,0 +1,20 @@
import math
def is_equal_to_with_digits(expected, actual, digits):
return round(expected, digits) == round(actual, digits)
expected_value = math.pi ** 2 / 6
n = 1
sum = 0
while not is_equal_to_with_digits(expected_value, sum, 6):
sum += 1 / n ** 2
n += 1
if n % 100000 == 0:
print(n, round(sum, 6), round(expected_value, 6), expected_value - sum)
print("result after", n, "iterations:", round(sum, 6), "; compare value:", round(expected_value, 6), expected_value - sum)

53
s1_a51.py 100644
View File

@ -0,0 +1,53 @@
import numbers
class BetterMatrix:
def __init__(self, matrix):
zero_count = 0
self.matrix = []
self.width = len(matrix)
self.height = len(matrix[0])
for row in matrix:
for col in row:
if col == 0:
zero_count += 1
elif zero_count != 0:
self.matrix.append([zero_count])
self.matrix.append(col)
zero_count = 0
else:
self.matrix.append(col)
if zero_count != 0:
self.matrix.append([zero_count])
print(self.matrix)
def __getitem__(self, row):
if row > self.height or row < 0:
raise ValueError("Value must be between 0 and " + str(self.height - 1))
dest_array = []
dest_amount = (row + 1) * self.width
index = 0
while dest_amount > len(dest_array):
if isinstance(self.matrix[index], numbers.Number):
dest_array.append(self.matrix[index])
else:
for i in range(self.matrix[index][0]):
if dest_amount > len(dest_array):
dest_array.append(0)
index += 1
while len(dest_array) > self.width:
dest_array.pop(0)
return dest_array
converted = BetterMatrix([[3, 0, -2, 11], [0, 0, 9, 0], [0, 7, 0, 0], [0, 0, 0, -3]])
print(converted[0][0])
print(converted[1][1])
print(converted[2][2])
print(converted[3][3])

BIN
s1_a52.pdf 100644

Binary file not shown.

33
s1_a53_6.py 100644
View File

@ -0,0 +1,33 @@
print("Teilaufgabe 5.3")
# Durch das Prinzip von Slicing und Verketten
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newlist = list[7:] + list[:7]
print(newlist)
print("\nTeilaufgabe 5.4")
long_word = "Donaudampfschiffahrtsgesellschaftsstewardess"
char_array = [*long_word]
print(char_array)
print("Das Wort hat", len(char_array), "viele Buchstaben.")
unique_characters = set(char_array)
print(unique_characters)
print("Das Wort hat", len(unique_characters), "eindeutige Buchstaben.")
print("\nTeilaufgabe 5.5")
list = [[1, 2, 3], [2, 1, 3], [4, 0, 1]]
list.sort(key=lambda x: x[1])
print(list)
print("\nTeilaufgabe 5.6")
def tiergeraeusch(tier):
return {"kuh": "muuh", "schwein": "oink", "pferd": "wieher"}[tier]
print(tiergeraeusch("kuh"))
print(tiergeraeusch("schwein"))
print(tiergeraeusch("pferd"))

21
s1_a61_2.py 100644
View File

@ -0,0 +1,21 @@
print("Teilaufgabe 6.1")
text = "This is a test"
ergebnis_text = text.replace(" ", "-")
print(ergebnis_text)
print("\nTeilaufgabe 6.2")
s1 = "Hello, World"
s2 = s1[0:s1.find(" ")]
# zwei alternative lösungen:
# 1
ergebnis_text = s2[::-1]
print(ergebnis_text)
# 2
ergebnis_text = "".join(reversed(s2))
print(ergebnis_text)
s1 = ergebnis_text