From 81a2c615bcadf6e25b0db111b67840bc54c7b2db Mon Sep 17 00:00:00 2001 From: cheezedoodle2 Date: Fri, 31 Mar 2023 12:03:53 -0700 Subject: [PATCH] Added sample files for day 2 and 3 --- test10.py | 7 +++++++ test11.py | 26 ++++++++++++++++++++++++++ test4.py | 18 ++++++++++++++++++ test5.py | 16 ++++++++++++++++ test6.py | 9 +++++++++ test7.py | 6 ++++++ test8.py | 17 +++++++++++++++++ test9.py | 24 ++++++++++++++++++++++++ 8 files changed, 123 insertions(+) create mode 100644 test10.py create mode 100644 test11.py create mode 100644 test4.py create mode 100644 test5.py create mode 100644 test6.py create mode 100644 test7.py create mode 100644 test8.py create mode 100644 test9.py diff --git a/test10.py b/test10.py new file mode 100644 index 0000000..06ce54d --- /dev/null +++ b/test10.py @@ -0,0 +1,7 @@ +for i in range(31): + print(f"{i}: ", end='') + if i % 3 == 0: + print("fizz", end='') + if i % 5 == 0: + print("buzz", end='') + print() \ No newline at end of file diff --git a/test11.py b/test11.py new file mode 100644 index 0000000..3e536d2 --- /dev/null +++ b/test11.py @@ -0,0 +1,26 @@ +class TomatoPrice: + price: float + + def __init__(self, value): + self.price = value + + def __str__(self): + return f"USD${self.price}" + + def __radd__(self, operand): + self.price += operand.price + +b'abcdefg' +str = b.encode('UTF-8') +for c in str: + if ord(c) >= 32 and ord(c) < 127: + print(c) + +vietnam = TomatoPrice(3.0) +japan = TomatoPrice(4.0) +korea = TomatoPrice(5.0) + +vietnam + japan + +output = "price of tomatoes in countries A, B, C: " + str(vietnam) + " " + str(japan) + " " + str(korea) + " " +print(output) \ No newline at end of file diff --git a/test4.py b/test4.py new file mode 100644 index 0000000..c86e315 --- /dev/null +++ b/test4.py @@ -0,0 +1,18 @@ +import http.server +import socketserver +from urllib.parse import urlparse, parse_qs + +class MyRequestHandler(http.server.SimpleHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header('Content-type', 'text/plain') + self.end_headers() + output_string = "abcdefg\rhijklmnop" + self.wfile.write(output_string.encode()) + +PORT = 8000 +Handler = MyRequestHandler +httpd = socketserver.TCPServer(("", PORT), Handler) +print(f"Serving on port {PORT}") + +httpd.serve_forever() \ No newline at end of file diff --git a/test5.py b/test5.py new file mode 100644 index 0000000..f02d4f3 --- /dev/null +++ b/test5.py @@ -0,0 +1,16 @@ +def validate(str): + for char in str: + if REPLACE ME: + raise ValueError("Crime detected!") + +try: + validate("abcdefg") + print("✅ test1 pass!") +except: + print("❌ test1 pass!") + +try: + validate("abcdefg\rhijklmnop") + print("❌ test2 fail!") +except ValueError as err: + print("✅ test2 pass!") diff --git a/test6.py b/test6.py new file mode 100644 index 0000000..0e07e21 --- /dev/null +++ b/test6.py @@ -0,0 +1,9 @@ +import pickle +data = { + '🍅': 3, + '🌭': 4, + '🍔': 5 +} + +with open('food.pickle', 'wb') as f: + pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) diff --git a/test7.py b/test7.py new file mode 100644 index 0000000..c6a04ed --- /dev/null +++ b/test7.py @@ -0,0 +1,6 @@ +import pickle + +with open('food.pickle', 'rb') as f: + data = pickle.load(f) + +pass \ No newline at end of file diff --git a/test8.py b/test8.py new file mode 100644 index 0000000..8dc86fb --- /dev/null +++ b/test8.py @@ -0,0 +1,17 @@ +from time import sleep + +def animate(): + i:int = 0 + + print("==================================", end='') + while True: + print(f"\r{i} ", end='') + i += 1 + sleep(1) + +def print_newline(): + print("\r", end='') + +print("1000", end='') +print_newline() +print("100") \ No newline at end of file diff --git a/test9.py b/test9.py new file mode 100644 index 0000000..85bd5a3 --- /dev/null +++ b/test9.py @@ -0,0 +1,24 @@ +class Temperature: + temperature: float + + def __init__(self, temperature): + self.temperature = temperature + + def __add__(self, operand): + if operand.__class__ != '__main__.Temperature': + raise ValueError(f"That's not a temperature object! It's a {operand.__class__}!") + self.temperature += operand.temperature + + def __str__(self): + return f"{self.temperature=}" + def insert_object(self, obj): + self.temperature += obj.temperature + +if __name__ == "__main__": + oven = Temperature(180.0) + chicken = Temperature(5.0) + print(f"{chicken + oven=}") + oven.insert_object(chicken) + + +