From 3fddcdfac5ed9f9a3eca67ceff8c240aff8a2364 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Fri, 8 Jul 2022 11:04:45 +0300 Subject: [PATCH] first commit --- .gitignore | 129 ++++++++++++++++++++ fizzbuzz.py | 59 ++++++++++ generalised_monty_hall.py | 123 +++++++++++++++++++ lorenz.py | 241 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 552 insertions(+) create mode 100644 .gitignore create mode 100644 fizzbuzz.py create mode 100644 generalised_monty_hall.py create mode 100644 lorenz.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6e4761 --- /dev/null +++ b/.gitignore @@ -0,0 +1,129 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/fizzbuzz.py b/fizzbuzz.py new file mode 100644 index 0000000..cd9ce45 --- /dev/null +++ b/fizzbuzz.py @@ -0,0 +1,59 @@ +def read(text, tp, conditions, failtext=None): + """Example:\n +read("firsttext", int, lambda x: x < 10, "lasttext")""" + inp = input(text) + while True: + try: + inp = tp(inp) + except: + pass + else: + if conditions(inp): + break + if failtext: + inp = input(failtext) + else: + inp = input(text) + return inp + +def fizzbuzz(dic={"fizz": 3, "buzz": 5}): + n = int(input("Insert the maximum value to play fizzbuzz: ")) + + for i in range(1, n + 1): + word = "" + for d in dic: + word += d if i % dic[d] == 0 else "" + word = i if not word else word + print(word) + + +def userdefined_fizzbuzz(): + dic = {} + print("Please insert fizzbuzz words and multiples.") + + while True: + number = read( + "Please enter a number greater than 1, \ +or enter 0 to finish your inputs: ", int, lambda x: x >= 0) + +# while True: +# number = input( +# "Please enter a number greater than 1, \ +# or enter 0 to finish your inputs: ") +# try: +# val = int(number) +# if val >= 0: +# break +# except ValueError: +# pass +# number = int(number) + if number == 0: + break + word = input("Please enter your word: ") + dic.update({word: number}) + + fizzbuzz(dic) + + +if __name__ == "__main__": + userdefined_fizzbuzz() diff --git a/generalised_monty_hall.py b/generalised_monty_hall.py new file mode 100644 index 0000000..e3e0a06 --- /dev/null +++ b/generalised_monty_hall.py @@ -0,0 +1,123 @@ +import random + + +def generalised_monty_hall(): + MIN_DOORS = 3 + + doors = input("How many doors do you want? \nNumber of Doors: ") + while True: + try: + doors = int(doors) + except: + pass + else: + if doors >= MIN_DOORS: + break + doors = input( + f"\nPlease choose a number larger than {MIN_DOORS - 1}\ +\nNumber of Doors: ") + + cars = input("\nHow many cars do you want? \nNumber of Cars: ") + while True: + try: + cars = int(cars) + except: + pass + else: + if 0 < cars < doors - 1: + break + cars = input( + f"\nPlease choose a number between 1 and {doors - 2}\ +\nNumber of Cars: ") + + goats = doors - cars + + choices = input( + "\nHow many doors would you like to choose?\nNumber of Choices: ") + while True: + try: + choices = int(choices) + except: + pass + else: + if 0 < choices < goats: + break + choices = input( + f"\nPlease choose a number between 1 and {goats - 1}\ +\nNumber of Choices: ") + + opened = input( + "\nHow many doors do you want the presenter to open \ +before you change your doors?\ +\nNumber of Doors to be Opened: ") + while True: + try: + opened = int(opened) + except: + pass + else: + if 0 <= opened < doors - choices - cars + 1: + break + opened = input( + f"\nPlease choose a number between 0 and {doors - choices - cars}\ +\nNumber of Doors to be Opened: ") + + closed = doors - opened + + changes = input( + "\nHow many doors would you like to swap after the presenter opens the doors?\ +\nNumber of Swaps: ") + while True: + try: + changes = int(changes) + except: + pass + else: + if 0 <= changes <= min(choices, doors - choices - opened): + break + changes = input( + f"\nPlease choose a number between 0 and \ +{min(choices, doors - choices - opened)}\ +\nNumber of Swaps: ") + + MAX_WIN = min(cars, choices) + win = [0] * (MAX_WIN + 1) + + trial = 0 + trials = 100_000 + + while trial < trials: + trial += 1 + + _goats = random.sample(range(doors), goats) + + _choices = random.sample(range(doors), choices) + + _opened = random.sample( + [n for n in _goats if n not in _choices], opened) + + _changes = random.sample( + [n for n in range(doors) + if n not in (*_opened, *_choices)], changes) + + _choices = random.sample(_choices, len(_choices) - len(_changes)) + + _choices.extend(_changes) + + _doors = [True] * doors + for goat in _goats: + _doors[goat] = False + + cars = 0 + for choice in _choices: + if _doors[choice]: + cars += 1 + win[cars] += 1 + + print("") + for i in range(len(win)): + print(f"{i}/{MAX_WIN} : {round(win[i] / trials * 100,3)}%") + + +if __name__ == "__main__": + generalised_monty_hall() diff --git a/lorenz.py b/lorenz.py new file mode 100644 index 0000000..322dbf0 --- /dev/null +++ b/lorenz.py @@ -0,0 +1,241 @@ +import numpy as np +import pygame +import sys +# import cv2 +import os +import math + +RED = 255, 0, 0 +GREEN = 0, 255, 0 +BLUE = 0, 0, 255 + +# out = cv2.VideoWriter('videos/Mandel0.avi', +# cv2.VideoWriter_fourcc(*'DIV4'), +# 30, (1920, 1080)) + + +class Lorenz: + def __init__(self, X=0.1, Y=0.0, Z=0.1): + self.X, self.Y, self.Z = X, Y, Z + self.sigma, self.rho, self.beta = 10, 28, 8/3.0 + self.oX, self.oY, self.oZ = self.X, self.Y, self.Z + self.dt = 0.01 + self.allpos = [self.current_pos()] + + def step(self): + self.oX, self.oY, self.oZ = self.X, self.Y, self.Z + self.X = self.X + self.dt * self.sigma * (self.Y - self.X) + self.Y = self.Y + self.dt * (self.X * (self.rho - self.Z) - self.Y) + self.Z = self.Z + self.dt * (self.X * self.Y - self.beta * self.Z) + self.allpos.append(self.current_pos()) + + def current_pos(self): + return self.X, self.Y, self.Z + + def previous_pos(self): + return self.oX, self.oY, self.oZ + + +class Screen: + def __init__(self, maths): + pygame.init() + self.math = maths + self.width = 1920 + self.height = 1080 + + self.speed = 1 + + self.camX = 30 + self.camY = 30 + self.camZ = 1 + + self.cam_rot_X = 0 + self.cam_rot_Y = 0 + self.cam_rot_Z = 0 + + self.frame = 0 + + self.xMin, self.xMax = -30, 30 + self.yMin, self.yMax = -30, 30 + self.zMin, self.zMax = -5, 55 + + self.screen = pygame.display.set_mode((self.width, self.height)) + + def to_screen_units_XZ(self, x, z): + screenX = self.width * (x - self.xMin) / (self.xMax - self.xMin) + screenY = self.height * (self.zMax - z) / (self.zMax - self.zMin) + + return round(screenX), round(screenY) + + def to_screen_units_XY(self, x, y): + screenX = self.width * (x - self.xMin) / (self.xMax - self.xMin) + screenY = self.height * (self.yMax - y) / (self.yMax - self.yMin) + + return round(screenX), round(screenY) + + def to_screen_units_YZ(self, y, z): + screenX = self.width * (y - self.yMin) / (self.yMax - self.yMin) + screenY = self.height * (self.zMax - z) / (self.zMax - self.zMin) + + return round(screenX), round(screenY) + + def to_screen_units(self, x, y, z): + z += self.camZ + x += self.camX + y += self.camY + + screen_pos = self.rot_z(-self.cam_rot_Z) @ self.rot_y(-self.cam_rot_Y) @ self.rot_x(-self.cam_rot_X) @ np.array([x, y, z]) + + screenX = self.width * (1 + screen_pos[0] / 2) / (math.tan(math.pi / 4) * abs(screen_pos[2])) + screenY = self.height * (1 + screen_pos[1] / 2) / (math.tan(9*math.pi / 64) * abs(screen_pos[2])) + + return [(round(screenX), round(screenY)), screen_pos[2]] + + def rot_x(self, angle): + return np.array([[1.0, 0.0, 0.0], + [0.0, math.cos(angle), -math.sin(angle)], + [0.0, math.sin(angle), math.cos(angle)]]) + + def rot_y(self, angle): + return np.array([[math.cos(angle), 0.0, math.sin(angle)], + [0.0, 1.0, 0.0], + [-math.sin(angle), 0.0, math.cos(angle)]]) + + def rot_z(self, angle): + return np.array([[math.cos(angle), -math.sin(angle), 0.0], + [math.sin(angle), math.cos(angle), 0.0], + [0.0, 0.0, 1.0]]) + + def draw_line(self): + self.screen.fill((10,5,30)) + + for m in self.math: + prev_point = self.to_screen_units(m[0].allpos[0][0], m[0].allpos[0][1], m[0].allpos[0][2]) + for point in m[0].allpos: + curr_point = self.to_screen_units(point[0], point[1], point[2]) + try: + if int(100 / max(curr_point[1], 0)) < 300 and prev_point[0][0] < 5000 and prev_point[0][1] < 5000 and curr_point[0][0] < 5000 and curr_point[0][1] < 5000: + pygame.draw.line(self.screen, self.color_intensity(max(curr_point[1], 0), m[1]), prev_point[0], curr_point[0], int(80 / max(curr_point[1], 0))) + except ZeroDivisionError: + if prev_point[0][0] < 5000 and prev_point[0][1] < 5000 and curr_point[0][0] < 5000 and curr_point[0][1] < 5000: + pygame.draw.line(self.screen, self.color_intensity(max(curr_point[1], 0), m[1]), prev_point[0], curr_point[0], 0) + prev_point = curr_point + + try: + pygame.draw.circle(self.screen, self.color_intensity(max(curr_point[1], 0), m[1]), curr_point[0], int(100 / max(curr_point[1], 0))) + except ZeroDivisionError: + pygame.draw.circle(self.screen, self.color_intensity(max(curr_point[1], 0), m[1]), curr_point[0], 0) + + def color_intensity(self, dist, color): + dist = min((abs(dist) / 100), 1) + redness = color[0] + greeness = color[1] + blueness = color[2] + redness = max(1 - dist, 0) * redness + greeness = max(1 - dist, 0) * greeness + blueness = max(1 - dist, 0) * blueness + return int(redness), int(greeness), int(blueness) + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + out.release() + pygame.quit() + sys.exit() + keys = pygame.key.get_pressed() + # if keys[pygame.K_SPACE]: + # self.speed = 3 + # if keys[pygame.K_w]: + # self.camZ -= 0.13 * self.speed + # if keys[pygame.K_s]: + # self.camZ += 0.13 * self.speed + # if keys[pygame.K_q]: + # self.camY -= 0.13 * self.speed + # if keys[pygame.K_e]: + # self.camY += 0.13 * self.speed + # if keys[pygame.K_a]: + # self.camX += 0.13 * self.speed + # if keys[pygame.K_d]: + # self.camX -= 0.13 * self.speed + # if keys[pygame.K_UP]: + # self.cam_rot_X += math.pi / 180 + # if keys[pygame.K_DOWN]: + # self.cam_rot_X -= math.pi / 180 + # if keys[pygame.K_LEFT]: + # self.cam_rot_Y += math.pi / 180 + # if keys[pygame.K_RIGHT]: + # self.cam_rot_Y -= math.pi / 180 + # if keys[pygame.K_RIGHTBRACKET]: + # self.cam_rot_Z += math.pi / 180 + # if keys[pygame.K_LEFTBRACKET]: + # self.cam_rot_Z -= math.pi / 180 + + move = np.array([0.0, 0.0, 0.0]) + + if keys[pygame.K_SPACE]: + self.speed = 3 + if keys[pygame.K_w]: + move[2] = -0.13 * self.speed + if keys[pygame.K_s]: + move[2] = 0.13 * self.speed + if keys[pygame.K_q]: + move[1] = -0.13 * self.speed + if keys[pygame.K_e]: + move[1] = 0.13 * self.speed + if keys[pygame.K_a]: + move[0] = 0.13 * self.speed + if keys[pygame.K_d]: + move[0] = -0.13 * self.speed + + move = self.rot_x(self.cam_rot_X) @ self.rot_y(self.cam_rot_Y) @ self.rot_z(self.cam_rot_Z) @ move + + if keys[pygame.K_UP]: + self.cam_rot_X += math.pi / 180 + if keys[pygame.K_DOWN]: + self.cam_rot_X -= math.pi / 180 + if keys[pygame.K_LEFT]: + self.cam_rot_Y -= math.pi / 180 + if keys[pygame.K_RIGHT]: + self.cam_rot_Y += math.pi / 180 + if keys[pygame.K_RIGHTBRACKET]: + self.cam_rot_Z += math.pi / 180 + if keys[pygame.K_LEFTBRACKET]: + self.cam_rot_Z -= math.pi / 180 + + self.camX += move[0] + self.camY += move[1] + self.camZ += move[2] + + self.speed = 1 + + def update(self): + # for i in range(20): + for m in self.math: + m[0].step() + + # if self.frame % 10 == 0: + # print(self.camX, self.camY, self.camZ) + # print(self.cam_rot_X, self.cam_rot_Y, self.cam_rot_Z) + pygame.display.flip() + + # if self.frame % 5 == 0: + self.draw_line() + #pygame.image.save(self.screen, "images/screen.png") + #img = cv2.imread("images/screen.png") + #os.remove("images/screen.png") + #out.write(img) + self.frame += 1 + + def run(self): + while True: + self.handle_events() + self.update() + + +if __name__ == "__main__": + lorenz1 = Lorenz() + lorenz2 = Lorenz(0.11, 0.0, 0.0) + lorenz3 = Lorenz(0.09, 0.01, -0.01) + lorenz = [(lorenz1, RED), (lorenz2, BLUE), (lorenz3, GREEN)] + screen = Screen(lorenz) + screen.run() -- 2.30.2