yo
This commit is contained in:
commit
a8ee8bad99
7 files changed
+458
No files matched your search
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
Teilt den String an gewünschter Position und gibt ihn geteilt aus 👌
|
||||
"""
|
||||
|
||||
def substring(text:str, start:int, length:int|None=None)->str:
|
||||
|
||||
if start < 0 or start > len(text):
|
||||
raise ValueError(f"Invalid position: {start}")
|
||||
#Fehlerausgabe, wenn die Startposition außerhalb des Bereichs der Länge liegt
|
||||
|
||||
if length is not None:
|
||||
if length < 0:
|
||||
raise ValueError(f"Invalid: {length}")
|
||||
#Fehlerausgabe, wenn Länge kleiner 0 ist
|
||||
return text[start: start + length]
|
||||
#Gibt den String ab der Startposition bis zum Ende wieder
|
||||
return text[start:]
|
||||
|
||||
|
||||
|
||||
text = "GEEKSFORGEEKS"
|
||||
print(substring(text, 5))
|
||||
print(substring(text, 0, 5))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import sys
|
||||
import pygame
|
||||
from moon import Moon
|
||||
|
||||
|
||||
class Apollo(Moon):
|
||||
def __init__(self, moon: Moon, distance: float = 40.0, radius: int = 5):
|
||||
# Geschwindigkeit: doppelt so schnell wie der Mond, aber entgegengesetzte Richtung (-2 * moon.speed)
|
||||
speed = -2.0 * moon.speed
|
||||
self.moon = moon
|
||||
# Initialisierung über super(): Farbe rot, kleinerer Radius
|
||||
super().__init__(
|
||||
center=(moon.x, moon.y),
|
||||
distance=distance,
|
||||
speed=speed,
|
||||
radius=radius,
|
||||
color=(255, 0, 0), # Roter Kreis
|
||||
)
|
||||
|
||||
|
||||
def update_position(self) -> None:
|
||||
"""
|
||||
Aktualisiert das dynamische Drehzentrum (Mondposition)
|
||||
und ruft anschließend die Positionsberechnung von Moon auf.
|
||||
"""
|
||||
# Drehzentrum ist der aktuelle Mittelpunkt des Mondes
|
||||
self.center = (self.moon.x, self.moon.y)
|
||||
|
||||
# Verwendet die bestehende Logik der Basisklasse Moon
|
||||
super().update_position()
|
||||
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((800, 600))
|
||||
pygame.display.set_caption("Mond- und Apollo-Simulation")
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
earth_pos = (400, 300)
|
||||
moon = Moon(center=earth_pos, distance=180.0, speed=0.015, radius=16)
|
||||
apollo = Apollo(moon=moon, distance=45.0, radius=5)
|
||||
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# Erst Mond aktualisieren, dann Apollo (da Apollo auf die Mond-Position zugreift)
|
||||
moon.update_position()
|
||||
apollo.update_position()
|
||||
|
||||
# Render-Schritt
|
||||
screen.fill((10, 10, 25))
|
||||
|
||||
# Erde
|
||||
pygame.draw.circle(screen, (40, 120, 230), earth_pos, 40)
|
||||
|
||||
# Mond und Kapsel zeichnen
|
||||
moon.draw(screen)
|
||||
apollo.draw(screen)
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
from collections.abc import Iterable
|
||||
|
||||
Number = float | int
|
||||
|
||||
|
||||
def _is_square_matrix(matrix: Iterable[Iterable[Number]]) -> bool:
|
||||
"""Return True if the supplied iterable is a square matrix.
|
||||
|
||||
A proper matrix must contain at least one row and every row must be
|
||||
the same length, and the number of rows must equal that length.
|
||||
"""
|
||||
rows = list(matrix)
|
||||
if not rows:
|
||||
return False
|
||||
row_len = len(rows[0])
|
||||
return all(len(row) == row_len for row in rows) and len(rows) == row_len
|
||||
|
||||
|
||||
def _iter_to_list(matrix: Iterable[Iterable[Number]]) -> list[list[Number]]:
|
||||
"""Convert the supplied iterable of iterables into a list of lists."""
|
||||
return [list(row) for row in matrix]
|
||||
|
||||
|
||||
def transpose(matrix: Iterable[Iterable[Number]]) -> list[list[Number]]:
|
||||
"""Return the transpose of a 2D matrix."""
|
||||
rows = _iter_to_list(matrix)
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
row_len = len(rows[0])
|
||||
if not all(len(r) == row_len for r in rows):
|
||||
raise ValueError("All rows must have the same length.")
|
||||
|
||||
return [list(col) for col in zip(*rows)]
|
||||
|
||||
|
||||
def rotate(
|
||||
matrix: Iterable[Iterable[Number]], clockwise: bool = True
|
||||
) -> list[list[Number]]:
|
||||
"""Rotate a 2D matrix by 90 degrees.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
matrix : Iterable[Iterable[Number]]
|
||||
Input matrix.
|
||||
clockwise : bool, default=True
|
||||
Rotate 90 degrees clockwise if True, counter-clockwise if False.
|
||||
|
||||
Returns
|
||||
-------
|
||||
list[list[Number]]
|
||||
The rotated matrix.
|
||||
"""
|
||||
rows = _iter_to_list(matrix)
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
row_len = len(rows[0])
|
||||
if not all(len(r) == row_len for r in rows):
|
||||
raise ValueError("All rows must have the same length.")
|
||||
|
||||
if clockwise:
|
||||
# 90° im Uhrzeigersinn: Zeilen umkehren, dann transponieren
|
||||
return [list(col) for col in zip(*rows[::-1])]
|
||||
else:
|
||||
# 90° gegen den Uhrzeigersinn: transponieren, dann Zeilen umkehren
|
||||
return [list(col) for col in zip(*rows)][::-1]
|
||||
|
||||
|
||||
def matmul(a: Iterable[Iterable[Number]], b: Iterable[Iterable[Number]]) -> list[list[Number]]:
|
||||
"""Multiply two matrices."""
|
||||
a_rows = _iter_to_list(a)
|
||||
b_rows = _iter_to_list(b)
|
||||
|
||||
# Validate dimensions
|
||||
if not a_rows or not b_rows:
|
||||
raise ValueError("Input matrices cannot be empty.")
|
||||
|
||||
a_cols = len(a_rows[0])
|
||||
b_rows_count = len(b_rows)
|
||||
if a_cols != b_rows_count:
|
||||
raise ValueError(
|
||||
f"Incompatible matrix dimensions: {len(a_rows)}x{a_cols} cannot be "
|
||||
f"multiplied with {b_rows_count}x{len(b_rows[0])}."
|
||||
)
|
||||
|
||||
b_cols = len(b_rows[0])
|
||||
|
||||
if not all(len(row) == a_cols for row in a_rows):
|
||||
raise ValueError("All rows of matrix a must have the same length.")
|
||||
if not all(len(row) == b_cols for row in b_rows):
|
||||
raise ValueError("All rows of matrix b must have the same length.")
|
||||
|
||||
# Skalarprodukt
|
||||
result: list[list[Number]] = []
|
||||
for i in range(len(a_rows)):
|
||||
result_row: list[Number] = []
|
||||
for j in range(b_cols):
|
||||
dot = sum(a_rows[i][k] * b_rows[k][j] for k in range(a_cols))
|
||||
result_row.append(dot)
|
||||
result.append(result_row)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# --- New functionality: rotating a 2D vector (e.g. moon around earth) ---
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def rotate_vector(vec: list[Number], angle_rad: Number) -> list[Number]:
|
||||
"""Return a new 2‑D vector rotated by *angle_rad*.
|
||||
``vec`` must be a list/tuple of two numbers ``[x, y]``.
|
||||
The rotation is counter‑clockwise and follows the standard
|
||||
rotation matrix:
|
||||
``[x', y'] = [x cosθ - y sinθ, x sinθ + y cosθ]``.
|
||||
"""
|
||||
if len(vec) != 2:
|
||||
raise ValueError("rotate_vector expects a 2‑D vector.")
|
||||
x, y = vec
|
||||
cos_a = math.cos(angle_rad)
|
||||
sin_a = math.sin(angle_rad)
|
||||
return [x * cos_a - y * sin_a, x * sin_a + y * cos_a]
|
||||
|
||||
|
||||
def orbit_earth_moon(
|
||||
earth_pos: list[Number],
|
||||
moon_pos: list[Number],
|
||||
angular_speed_rad_per_time: Number,
|
||||
dt: Number,
|
||||
) -> tuple[list[Number], list[Number]]:
|
||||
"""Rotate *moon_pos* around *earth_pos*.
|
||||
|
||||
Both *earth_pos* and *moon_pos* are ABSOLUTE [x, y] coordinates.
|
||||
The moon is moved by applying a rotation of ``omega * dt`` radians
|
||||
around the Earth. The Earth stays fixed, so repeatedly calling this
|
||||
function makes the moon orbit the Earth on a circle of constant radius.
|
||||
|
||||
Returns the new positions ``(new_earth_pos, new_moon_pos)``.
|
||||
"""
|
||||
# Translate moon to origin relative to earth
|
||||
relative = [moon_pos[0] - earth_pos[0], moon_pos[1] - earth_pos[1]]
|
||||
# Rotate the relative vector
|
||||
new_rel = rotate_vector(relative, angular_speed_rad_per_time * dt)
|
||||
# Translate back
|
||||
new_moon = [earth_pos[0] + new_rel[0], earth_pos[1] + new_rel[1]]
|
||||
return earth_pos, new_moon
|
||||
|
||||
|
||||
def point_on_circle(
|
||||
center: list[Number], radius: Number, angle_rad: Number
|
||||
) -> list[Number]:
|
||||
"""Return the absolute [x, y] position on a circle.
|
||||
|
||||
*center* is the absolute center, *radius* the distance from the center
|
||||
and *angle_rad* the angle in radians (0 points to the right,
|
||||
increasing angles rotate counter-clockwise).
|
||||
"""
|
||||
return [
|
||||
center[0] + radius * math.cos(angle_rad),
|
||||
center[1] + radius * math.sin(angle_rad),
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test matmul
|
||||
m1 = [[1, 2], [3, 4]]
|
||||
m2 = [[5, 6], [7, 8]]
|
||||
res1 = matmul(m1, m2)
|
||||
assert res1 == [[19, 22], [43, 50]]
|
||||
|
||||
m3 = [[1, 2, 3], [4, 5, 6]]
|
||||
m4 = [[7, 8], [9, 1], [2, 3]]
|
||||
res2 = matmul(m3, m4)
|
||||
assert res2 == [[31, 19], [85, 55]]
|
||||
|
||||
# Test transposnieren
|
||||
m_square = [[1, 2], [3, 4]]
|
||||
assert transpose(m_square) == [[1, 3], [2, 4]]
|
||||
|
||||
m_rect = [[1, 2, 3], [4, 5, 6]]
|
||||
assert transpose(m_rect) == [[1, 4], [2, 5], [3, 6]]
|
||||
|
||||
# Test rotate mit Uhr
|
||||
assert rotate([[1, 2], [3, 4]]) == [[3, 1], [4, 2]]
|
||||
assert rotate([[1, 2, 3], [4, 5, 6]]) == [[4, 1], [5, 2], [6, 3]]
|
||||
|
||||
# Testf rotate gegen Uhr
|
||||
assert rotate([[1, 2], [3, 4]], clockwise=False) == [[2, 4], [1, 3]]
|
||||
@@ -0,0 +1,58 @@
|
||||
import random
|
||||
import pygame
|
||||
|
||||
class Game:
|
||||
|
||||
SIZE = (500, 500)
|
||||
FPS = 30
|
||||
|
||||
def __init__(self, title="Game", fps=FPS, size=SIZE):
|
||||
self.title = title
|
||||
self.fps = fps
|
||||
self.size = size
|
||||
self.screen = None
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
def run(self):
|
||||
self.init_game()
|
||||
self.game_loop()
|
||||
self.exit_game()
|
||||
|
||||
def init_game(self):
|
||||
random.seed()
|
||||
pygame.init()
|
||||
pygame.display.set_caption(self.title)
|
||||
self.set_screen()
|
||||
|
||||
def set_screen(self):
|
||||
if self.screen is None:
|
||||
self.screen = pygame.display.set_mode(self.size)
|
||||
self.screen.fill((0, 0, 0))
|
||||
return self.screen
|
||||
|
||||
def game_loop(self):
|
||||
while True:
|
||||
if not self.event_handling():
|
||||
break
|
||||
if not self.update_game():
|
||||
break
|
||||
self.draw_game()
|
||||
self.clock.tick(self.fps)
|
||||
|
||||
def event_handling(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
keys = pygame.key.get_pressed()
|
||||
# TODO: implement key events
|
||||
return True
|
||||
|
||||
def update_game(self):
|
||||
# TODO: implement game logic
|
||||
return True
|
||||
|
||||
def draw_game(self):
|
||||
pygame.display.flip()
|
||||
|
||||
def exit_game(self):
|
||||
pygame.quit()
|
||||
@@ -0,0 +1,85 @@
|
||||
import math
|
||||
import sys
|
||||
import pygame
|
||||
|
||||
|
||||
class Moon:
|
||||
def __init__(
|
||||
self,
|
||||
center: tuple[float, float] = (400, 300),
|
||||
distance: float = 180.0,
|
||||
speed: float = 0.02,
|
||||
radius: int = 15,
|
||||
color: tuple[int, int, int] = (200, 200, 200),
|
||||
):
|
||||
self.center = center # Drehzentrum (standardmäßig die Erde)
|
||||
self.distance = distance # Bahnradius
|
||||
self.angle = 0.0 # Aktueller Winkel
|
||||
self.speed = speed # Winkelgeschwindigkeit
|
||||
self.radius = radius # Darstellungsradius
|
||||
self.color = color # Farbe (Mond: grau)
|
||||
self.x = 0.0
|
||||
self.y = 0.0
|
||||
self.update_position()
|
||||
|
||||
def update_angle(self) -> None:
|
||||
"""Erhöht den Drehwinkel um die eigene Geschwindigkeit."""
|
||||
self.angle += self.speed
|
||||
|
||||
def compute_relative_position(self) -> tuple[float, float]:
|
||||
"""Berechnet die relative Position zum Mittelpunkt über Polar- zu Kartesischen Koordinaten."""
|
||||
dx = self.distance * math.cos(self.angle)
|
||||
dy = self.distance * math.sin(self.angle)
|
||||
return dx, dy
|
||||
|
||||
def update_position(self) -> None:
|
||||
"""Setzt die absolute Position basierend auf dem Zentrum und dem relativen Offset."""
|
||||
self.update_angle()
|
||||
dx, dy = self.compute_relative_position()
|
||||
self.x = self.center[0] + dx
|
||||
self.y = self.center[1] + dy
|
||||
|
||||
def draw(self, screen: pygame.Surface) -> None:
|
||||
"""Zeichnet das Objekt auf den Bildschirm."""
|
||||
pygame.draw.circle(
|
||||
screen,
|
||||
self.color,
|
||||
(int(self.x), int(self.y)),
|
||||
self.radius,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((800, 600))
|
||||
pygame.display.set_caption("Mond-Simulation")
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
earth_pos = (400, 300)
|
||||
moon = Moon(center=earth_pos)
|
||||
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# Aktualisierung
|
||||
moon.update_position()
|
||||
|
||||
# Zeichnen
|
||||
screen.fill((10, 10, 25)) # Weltraum-Hintergrund
|
||||
# Erde zeichnen
|
||||
pygame.draw.circle(screen, (30, 100, 220), earth_pos, 40)
|
||||
# Mond zeichnen
|
||||
moon.draw(screen)
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"$schema": "https://opencode.ai/config.json",
|
||||
"provider": {
|
||||
"thn": {
|
||||
"npm": "@ai-sdk/openai-compatible",
|
||||
"options": {
|
||||
"baseURL": "http://141.75.33.244:8000/v1",
|
||||
"apiKey": "sk-QXj1p40XOwNSze_5-eYrBzVh3qIgAfqoWPkAjNVqOS8"
|
||||
},
|
||||
"models": {
|
||||
"gpt-oss:20b": { "name": "GPT-OSS 20B" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"model": "thn/gpt-oss:20b"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"""
|
||||
Reverse string utility.
|
||||
|
||||
This script prompts the user for a string and prints the reversed version.
|
||||
"""
|
||||
|
||||
def main():
|
||||
user_input = input("Please enter a string to reverse: ")
|
||||
reversed_string = user_input[::-1]
|
||||
print("Reversed string: ", reversed_string)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user