From f549db5bff9835dd78b2d492add5a50741467392 Mon Sep 17 00:00:00 2001 From: Jakob Hogan Date: Fri, 18 Oct 2024 16:44:25 +1100 Subject: [PATCH] fix bug where numbers not getting generated if they 1 below target --- findnum.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/findnum.py b/findnum.py index e951821..9f807ee 100644 --- a/findnum.py +++ b/findnum.py @@ -16,6 +16,10 @@ def process_code(code): else: return f"ord({code})" +def wrap_max_range(number): + """Wrap the number with max(range())""" + return f"max(range({number}))" + def find_shortest_combination(target, codes, max_length=4): """Find the shortest combination of codes that evaluates to the target number.""" ascii_values = {} @@ -66,8 +70,13 @@ def update_shortest(expression): if combo_sum == target: expression = "+".join(process_code(code) for code, _ in combo) update_shortest(f'chr({expression})') - elif combo_sum > target: - break # Stop checking this length if sum exceeds target + elif combo_sum == target + 1: + # If the combination sum is 1 more than the target, wrap it with max(range()) + expression = "+".join(process_code(code) for code, _ in combo) + wrapped_expression = wrap_max_range(expression) + update_shortest(f'chr({wrapped_expression})') + elif combo_sum > target + 1: + break # Stop checking this length if sum exceeds target + 1 if shortest_result is None: print(f"\nNo combination found for target {target}") @@ -87,8 +96,6 @@ def process_file(filename): codes.append(row[1]) # Assuming the second column contains the code snippets return codes -# Main execution logic remains the same... - def find_combinations_in_range(n, codes): """Find combinations for all numbers from 0 to n."""