From 13f2322e72b35971195445130abda0419b7ed34a Mon Sep 17 00:00:00 2001 From: Eclypsed Date: Sat, 13 Sep 2025 20:02:35 -0400 Subject: [PATCH] Backtracking first working version --- Backtracking/cs412_hw3_a.py | 65 +++++++++++++++++++++++++++++++++ Backtracking/inputs/sample1.txt | 2 + Backtracking/inputs/sample2.txt | 2 + Backtracking/inputs/sample3.txt | 2 + Backtracking/inputs/sample4.txt | 2 + Backtracking/inputs/sample5.txt | 2 + 6 files changed, 75 insertions(+) create mode 100644 Backtracking/cs412_hw3_a.py create mode 100644 Backtracking/inputs/sample1.txt create mode 100644 Backtracking/inputs/sample2.txt create mode 100644 Backtracking/inputs/sample3.txt create mode 100644 Backtracking/inputs/sample4.txt create mode 100644 Backtracking/inputs/sample5.txt diff --git a/Backtracking/cs412_hw3_a.py b/Backtracking/cs412_hw3_a.py new file mode 100644 index 0000000..5abea5e --- /dev/null +++ b/Backtracking/cs412_hw3_a.py @@ -0,0 +1,65 @@ +""" +name: Nicholas Tamassia + +Honor Code and Acknowledgments: + + This work complies with the JMU Honor Code. + + Comments here on your code and submission. +""" + + +# I'm going to pat myself on the back, I wrote this in one shot +# and it worked first try for the full credit +def construct(sections: list[int], target: int) -> dict[int, int]: + # We want to start with the largest sections first + rev_sections = sorted(sections, reverse=True) + + # Initialize to a worse-than-worst-case scenario + best_sections: dict[int, int] = {i: target for i in sections} + best_num: int = target * len(sections) + + def recurse( + cur_sections: dict[int, int], cur_height: int, num_sections: int + ) -> None: + nonlocal best_sections, best_num + + if num_sections >= best_num: + return + + if cur_height == target: + best_sections = cur_sections + best_num = num_sections + return + + for i in rev_sections: + if cur_height + i > target: + continue + + new_sections = cur_sections.copy() + new_sections[i] += 1 + + recurse(new_sections, cur_height + i, num_sections + 1) + + recurse({i: 0 for i in sections}, 0, 0) + return best_sections + + +# All modules for CS 412 must include a main method that allows it +# to imported and invoked from other python scripts +def main(): + sections: list[int] = list(map(int, input().split(" "))) + target: int = int(input()) + + best_sections = construct(sections, target) + best_num = 0 + + for i, n in sorted(best_sections.items()): + best_num += n + print(f"{n} of length {i}") + + print(f"{best_num} rocket sections minimum") + + +if __name__ == "__main__": + main() diff --git a/Backtracking/inputs/sample1.txt b/Backtracking/inputs/sample1.txt new file mode 100644 index 0000000..143e2d9 --- /dev/null +++ b/Backtracking/inputs/sample1.txt @@ -0,0 +1,2 @@ +1 2 5 7 +15 diff --git a/Backtracking/inputs/sample2.txt b/Backtracking/inputs/sample2.txt new file mode 100644 index 0000000..6ff696f --- /dev/null +++ b/Backtracking/inputs/sample2.txt @@ -0,0 +1,2 @@ +1 2 3 5 7 14 +15 diff --git a/Backtracking/inputs/sample3.txt b/Backtracking/inputs/sample3.txt new file mode 100644 index 0000000..052aed0 --- /dev/null +++ b/Backtracking/inputs/sample3.txt @@ -0,0 +1,2 @@ +1 9 +17 diff --git a/Backtracking/inputs/sample4.txt b/Backtracking/inputs/sample4.txt new file mode 100644 index 0000000..35c18cf --- /dev/null +++ b/Backtracking/inputs/sample4.txt @@ -0,0 +1,2 @@ +1 9 10 +18 diff --git a/Backtracking/inputs/sample5.txt b/Backtracking/inputs/sample5.txt new file mode 100644 index 0000000..c65fbba --- /dev/null +++ b/Backtracking/inputs/sample5.txt @@ -0,0 +1,2 @@ +1 9 10 90 100 +198