From 3200a382ba3e6124a0a42ccf0e00cd22be8ac64a Mon Sep 17 00:00:00 2001 From: Jon Fincher Date: Tue, 22 Apr 2025 12:02:26 -0500 Subject: [PATCH 1/5] Initial code checkin after linting --- skip-ahead-with-continue/README.md | 3 + skip-ahead-with-continue/aoc_2022_d7.py | 166 ++++++++++++++++++ skip-ahead-with-continue/example1.py | 6 + .../example1_continued.py | 8 + .../example1_continued_instaviz.py | 15 ++ skip-ahead-with-continue/example1_instaviz.py | 15 ++ 6 files changed, 213 insertions(+) create mode 100644 skip-ahead-with-continue/README.md create mode 100644 skip-ahead-with-continue/aoc_2022_d7.py create mode 100644 skip-ahead-with-continue/example1.py create mode 100644 skip-ahead-with-continue/example1_continued.py create mode 100644 skip-ahead-with-continue/example1_continued_instaviz.py create mode 100644 skip-ahead-with-continue/example1_instaviz.py diff --git a/skip-ahead-with-continue/README.md b/skip-ahead-with-continue/README.md new file mode 100644 index 0000000000..a93ce54f64 --- /dev/null +++ b/skip-ahead-with-continue/README.md @@ -0,0 +1,3 @@ +# Skip Ahead with Continue + +This folder provides the code examples for the Real Python tutorial [Skip Ahead with `continue`](https://realpython.com/skip-ahead-with-continue/). diff --git a/skip-ahead-with-continue/aoc_2022_d7.py b/skip-ahead-with-continue/aoc_2022_d7.py new file mode 100644 index 0000000000..7e3990a4de --- /dev/null +++ b/skip-ahead-with-continue/aoc_2022_d7.py @@ -0,0 +1,166 @@ +# AOC 2022 Day 07 + +import pathlib + +root_path = pathlib.Path.home() / "git" / "AOC2022" / "day07" / "day07" + + +def get_path_name(folder_name): + if len(folder_name) > 1: + return "/".join(folder_name)[1:] + else: + return "/".join(folder_name) + + +def parse(lines): + + # We read each line and create some structures + # - We maintain a path list stack, which tells us how deep the structure is + # - It starts with ["/"] + # - We can do a "/".join(path)[1:] to get the full path, and drop the "/" + # - We maintain a dictionary of paths to files + # - The key is the full path + # - The value is a list of tuples. + # - Each tuple contains the name and file size + # + # Here's the plan when we get to each line. + # - if it's a cd command + # - If it's a directory name, we push that onto the end of the path list + # - If it's a .., we pop the last item off the path list + # - if it's an ls command, or a dir + # - skip to the next line + # - if it's a number and file + # - construct the tuple (file, size) + # - Get the full path with the code above + # - Is this path in the dictionary? + # - If not, create it with a blank list + # - Append the tuple to the path list + + # Where do we keep the folder names + folder_name = ["/"] + + # Where do we keep the files and sizes + folder_files = {} + + # Start reading at the second line - we know the first two line + for line in lines[2:]: + # Split the line so we can parse it + part = line.split() + + # What kind of line is it? + # Are we changing into a folder + if part[1] == "ls": + # We can skip these lines + continue + + # Is it a folder? We need to ready for it + if part[0] == "dir": + # Get the current path, and append this path to it + path = get_path_name(folder_name) + if path.endswith("/"): + path += part[1] + else: + path += "/" + part[1] + + # Add a blank to the dictionary + # If we don't do this, we miss empty folders in our output + # They may contribute to the answer later. + folder_files[path] = [] + + # Keep going + continue + + # Are we changing folders + if part[1] == "cd": + # Backing up? Remove this folder + if part[2] == "..": + folder_name.pop() + # Heading down? Add this folder + else: + folder_name.append(part[2]) + + else: + # It's a file, so we need the full path + path = get_path_name(folder_name) + + if path not in folder_files.keys(): + folder_files[path] = [] + + # Append this tuple to that list + folder_files[path].append((part[1], int(part[0]))) + + # We're done, return the dictionary + return folder_files + + +def get_file_sizes(tree): + + # We've got the tree, so we need all the keys + paths = sorted([k for k in tree.keys()], reverse=True) + + # We need a dictionary to store the file sizes for each path + files_sizes = {} + + # And a place for the total + for current_path in paths: + for containing_path in tree.keys(): + if containing_path.startswith(current_path): + # Do we have a current path + if current_path not in files_sizes.keys(): + files_sizes[current_path] = 0 + + # Add the files in this folder + for _, size in tree[containing_path]: + files_sizes[current_path] += size + + # print(f"Checking {check_path}... {current_path} is in it") + + # DEBUG: Print the files sizes + # for k, v in files_sizes.items(): + # print(f"Folder {k} contains {v} bytes of files") + return files_sizes + + +def part1(file_sizes): + + total = 0 + + # Now we can go through them all in this order + for _, total_size in file_sizes.items(): + if total_size <= 100_000: + total += total_size + + return total + + +def part2(file_sizes): + # How much free space do we have? + free_space = 70_000_000 - file_sizes["/"] + + # How much more do we need? + needed_space = 30_000_000 - free_space + + # Let's find the one folder which gets us to enough free space + space = 70_000_000 + for _, proposed in file_sizes.items(): + # Not enough space? + if proposed < needed_space: + continue + + # Found one, let's make sure it's the smallest + space = min(space, proposed) + + return space + + +if __name__ == "__main__": + + with open(root_path / "input", "r") as f: + # with open(root_path / "sample", "r") as f: + lines = [line.strip() for line in f.readlines()] + + tree = parse(lines) + file_sizes = get_file_sizes(tree) + + print(f"Part 1: Answer: {part1(file_sizes)}") + print(f"Part 2: Answer: {part2(file_sizes)}") diff --git a/skip-ahead-with-continue/example1.py b/skip-ahead-with-continue/example1.py new file mode 100644 index 0000000000..83afa2fb04 --- /dev/null +++ b/skip-ahead-with-continue/example1.py @@ -0,0 +1,6 @@ +total = 0 + +for number in range(-10, 10): + total += number + +print(total) diff --git a/skip-ahead-with-continue/example1_continued.py b/skip-ahead-with-continue/example1_continued.py new file mode 100644 index 0000000000..663edd3435 --- /dev/null +++ b/skip-ahead-with-continue/example1_continued.py @@ -0,0 +1,8 @@ +total = 0 + +for number in range(-10, 10): + if number < 0: + continue + total += number + +print(total) diff --git a/skip-ahead-with-continue/example1_continued_instaviz.py b/skip-ahead-with-continue/example1_continued_instaviz.py new file mode 100644 index 0000000000..287b8f8f9b --- /dev/null +++ b/skip-ahead-with-continue/example1_continued_instaviz.py @@ -0,0 +1,15 @@ +import instaviz + + +def add_numbers(): + total = 0 + + for number in range(-10, 10): + if number < 0: + continue + total += number + + return total + + +instaviz.show(add_numbers) diff --git a/skip-ahead-with-continue/example1_instaviz.py b/skip-ahead-with-continue/example1_instaviz.py new file mode 100644 index 0000000000..056ac1876c --- /dev/null +++ b/skip-ahead-with-continue/example1_instaviz.py @@ -0,0 +1,15 @@ +import instaviz + + +def add_numbers(): + total = 0 + + for number in range(-10, 10): + # if number < 0: + # continue + total += number + + return total + + +instaviz.show(add_numbers) From c6f3ff719036f8e666d6767cd4c80c1d667439e8 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Thu, 24 Apr 2025 18:34:33 +0200 Subject: [PATCH 2/5] Update project code Add pyproject.toml, Add named scripts, Rename script, Remove initial versions of scripts --- ...ontinued_instaviz.py => code_disassembly.py} | 0 skip-ahead-with-continue/continue_finally.py | 17 +++++++++++++++++ skip-ahead-with-continue/example1.py | 6 ------ skip-ahead-with-continue/example1_continued.py | 8 -------- skip-ahead-with-continue/example1_instaviz.py | 15 --------------- skip-ahead-with-continue/pyproject.toml | 7 +++++++ skip-ahead-with-continue/sum_whole_numbers.py | 11 +++++++++++ 7 files changed, 35 insertions(+), 29 deletions(-) rename skip-ahead-with-continue/{example1_continued_instaviz.py => code_disassembly.py} (100%) create mode 100644 skip-ahead-with-continue/continue_finally.py delete mode 100644 skip-ahead-with-continue/example1.py delete mode 100644 skip-ahead-with-continue/example1_continued.py delete mode 100644 skip-ahead-with-continue/example1_instaviz.py create mode 100644 skip-ahead-with-continue/pyproject.toml create mode 100644 skip-ahead-with-continue/sum_whole_numbers.py diff --git a/skip-ahead-with-continue/example1_continued_instaviz.py b/skip-ahead-with-continue/code_disassembly.py similarity index 100% rename from skip-ahead-with-continue/example1_continued_instaviz.py rename to skip-ahead-with-continue/code_disassembly.py diff --git a/skip-ahead-with-continue/continue_finally.py b/skip-ahead-with-continue/continue_finally.py new file mode 100644 index 0000000000..afd2e97721 --- /dev/null +++ b/skip-ahead-with-continue/continue_finally.py @@ -0,0 +1,17 @@ +for number in range(2): + try: + print(f"Iteration {number}: start of try block") + + if number == 1: + print(f" Executing `continue` in iteration {number}...") + continue + + print(f" Normal flow in iteration {number}...") + + except Exception as e: + print(f"Iteration {number}: Exception: {e}") + + finally: + print(f"Iteration {number}: finally block") + + print(f"Iteration {number}: rest of loop body", end="\n\n") diff --git a/skip-ahead-with-continue/example1.py b/skip-ahead-with-continue/example1.py deleted file mode 100644 index 83afa2fb04..0000000000 --- a/skip-ahead-with-continue/example1.py +++ /dev/null @@ -1,6 +0,0 @@ -total = 0 - -for number in range(-10, 10): - total += number - -print(total) diff --git a/skip-ahead-with-continue/example1_continued.py b/skip-ahead-with-continue/example1_continued.py deleted file mode 100644 index 663edd3435..0000000000 --- a/skip-ahead-with-continue/example1_continued.py +++ /dev/null @@ -1,8 +0,0 @@ -total = 0 - -for number in range(-10, 10): - if number < 0: - continue - total += number - -print(total) diff --git a/skip-ahead-with-continue/example1_instaviz.py b/skip-ahead-with-continue/example1_instaviz.py deleted file mode 100644 index 056ac1876c..0000000000 --- a/skip-ahead-with-continue/example1_instaviz.py +++ /dev/null @@ -1,15 +0,0 @@ -import instaviz - - -def add_numbers(): - total = 0 - - for number in range(-10, 10): - # if number < 0: - # continue - total += number - - return total - - -instaviz.show(add_numbers) diff --git a/skip-ahead-with-continue/pyproject.toml b/skip-ahead-with-continue/pyproject.toml new file mode 100644 index 0000000000..ce84b81bba --- /dev/null +++ b/skip-ahead-with-continue/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "skip-ahead-with-continue" +version = "0.1.0" +description = "Code for the Real Python tutorial on Python's `continue` keyword" +readme = "README.md" +requires-python = ">=3.8" +dependencies = ["instaviz"] diff --git a/skip-ahead-with-continue/sum_whole_numbers.py b/skip-ahead-with-continue/sum_whole_numbers.py new file mode 100644 index 0000000000..6cc52d4543 --- /dev/null +++ b/skip-ahead-with-continue/sum_whole_numbers.py @@ -0,0 +1,11 @@ +print("Enter one whole number per input.") +print("Type 0 to stop and display their sum:") + +total = 0 + +while (user_int := int(input("+ "))) != 0: + if user_int < 0: + continue + total += user_int + +print(f"{total=}") From 321adde585f160c7109715c50788a6d09d2c892a Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:49:14 -0600 Subject: [PATCH 3/5] Update README.md --- skip-ahead-with-continue/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skip-ahead-with-continue/README.md b/skip-ahead-with-continue/README.md index a93ce54f64..437c1045f8 100644 --- a/skip-ahead-with-continue/README.md +++ b/skip-ahead-with-continue/README.md @@ -1,3 +1,3 @@ -# Skip Ahead with Continue +# Skip Ahead in Loops With Python's Continue Keyword -This folder provides the code examples for the Real Python tutorial [Skip Ahead with `continue`](https://realpython.com/skip-ahead-with-continue/). +This folder provides the code examples for the Real Python tutorial [Skip Ahead in Loops With Python's Continue Keyword](https://realpython.com/skip-ahead-with-continue/). From f86bd1c7ea8a9d8ef00d4fc65facfcc5860e00e3 Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:56:51 -0600 Subject: [PATCH 4/5] Update aoc_2022_d7.py --- skip-ahead-with-continue/aoc_2022_d7.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skip-ahead-with-continue/aoc_2022_d7.py b/skip-ahead-with-continue/aoc_2022_d7.py index 7e3990a4de..c62734f959 100644 --- a/skip-ahead-with-continue/aoc_2022_d7.py +++ b/skip-ahead-with-continue/aoc_2022_d7.py @@ -20,10 +20,10 @@ def parse(lines): # - We can do a "/".join(path)[1:] to get the full path, and drop the "/" # - We maintain a dictionary of paths to files # - The key is the full path - # - The value is a list of tuples. + # - The value is a list of tuples # - Each tuple contains the name and file size # - # Here's the plan when we get to each line. + # Here's the plan when we get to each line # - if it's a cd command # - If it's a directory name, we push that onto the end of the path list # - If it's a .., we pop the last item off the path list From 2020fc785535c5c2b580decb1d7d4e84b8c55d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Zaczy=C5=84ski?= Date: Tue, 29 Jul 2025 10:02:30 +0200 Subject: [PATCH 5/5] Post final QA fixes --- {skip-ahead-with-continue => python-continue}/README.md | 2 +- {skip-ahead-with-continue => python-continue}/aoc_2022_d7.py | 0 .../code_disassembly.py | 0 .../continue_finally.py | 0 {skip-ahead-with-continue => python-continue}/pyproject.toml | 4 ++-- .../sum_whole_numbers.py | 0 6 files changed, 3 insertions(+), 3 deletions(-) rename {skip-ahead-with-continue => python-continue}/README.md (80%) rename {skip-ahead-with-continue => python-continue}/aoc_2022_d7.py (100%) rename {skip-ahead-with-continue => python-continue}/code_disassembly.py (100%) rename {skip-ahead-with-continue => python-continue}/continue_finally.py (100%) rename {skip-ahead-with-continue => python-continue}/pyproject.toml (72%) rename {skip-ahead-with-continue => python-continue}/sum_whole_numbers.py (100%) diff --git a/skip-ahead-with-continue/README.md b/python-continue/README.md similarity index 80% rename from skip-ahead-with-continue/README.md rename to python-continue/README.md index 437c1045f8..09bf059d9e 100644 --- a/skip-ahead-with-continue/README.md +++ b/python-continue/README.md @@ -1,3 +1,3 @@ # Skip Ahead in Loops With Python's Continue Keyword -This folder provides the code examples for the Real Python tutorial [Skip Ahead in Loops With Python's Continue Keyword](https://realpython.com/skip-ahead-with-continue/). +This folder provides the code examples for the Real Python tutorial [Skip Ahead in Loops With Python's Continue Keyword](https://realpython.com/python-continue/). diff --git a/skip-ahead-with-continue/aoc_2022_d7.py b/python-continue/aoc_2022_d7.py similarity index 100% rename from skip-ahead-with-continue/aoc_2022_d7.py rename to python-continue/aoc_2022_d7.py diff --git a/skip-ahead-with-continue/code_disassembly.py b/python-continue/code_disassembly.py similarity index 100% rename from skip-ahead-with-continue/code_disassembly.py rename to python-continue/code_disassembly.py diff --git a/skip-ahead-with-continue/continue_finally.py b/python-continue/continue_finally.py similarity index 100% rename from skip-ahead-with-continue/continue_finally.py rename to python-continue/continue_finally.py diff --git a/skip-ahead-with-continue/pyproject.toml b/python-continue/pyproject.toml similarity index 72% rename from skip-ahead-with-continue/pyproject.toml rename to python-continue/pyproject.toml index ce84b81bba..13cd3f6454 100644 --- a/skip-ahead-with-continue/pyproject.toml +++ b/python-continue/pyproject.toml @@ -1,7 +1,7 @@ [project] -name = "skip-ahead-with-continue" +name = "python-continue" version = "0.1.0" description = "Code for the Real Python tutorial on Python's `continue` keyword" readme = "README.md" -requires-python = ">=3.8" +requires-python = ">=3.9" dependencies = ["instaviz"] diff --git a/skip-ahead-with-continue/sum_whole_numbers.py b/python-continue/sum_whole_numbers.py similarity index 100% rename from skip-ahead-with-continue/sum_whole_numbers.py rename to python-continue/sum_whole_numbers.py