Skip to content

Commit

Permalink
cleaned output a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lakazatong committed Nov 25, 2024
1 parent 91006bb commit 6358276
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def load(self, user_input):
self.best_size = self.best_size_upper_bond() + self.size_offset
gcd = compute_gcd(*source_values, *self.target_values)
self.gcd_incompatible = get_gcd_incompatible(gcd)
print(f"\nSolutions' size upper bound: {self.best_size}, {gcd = }\n")
print()

return True

Expand Down Expand Up @@ -398,11 +398,13 @@ def conclude(self):
print_standing_text(f"Saving solutions... {i+1}/{self.solutions_count}")
tree.attach_leaves(self.leaves)
tree.save(os.path.join(self.problem_str, config.solutions_filename(i)), self.unit_flow_ratio)
print(f"\n\nSolutions saved at {self.problem_str}")
else:
print("Saving solution...")
tree = self.solutions[0]
tree.attach_leaves(self.leaves)
tree.save(os.path.join(self.problem_str, config.solutions_filename(0)), self.unit_flow_ratio)
print(f"\nSolution saved at {self.problem_str}")
print()

# simple state machine
Expand Down
17 changes: 12 additions & 5 deletions src/utils/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ def with_count(count, frac_str):

def parse_fraction(fraction_str):
from fractions import Fraction
import re
if '/' in fraction_str:
numerator, denominator = fraction_str.split('/')
return Fraction(int(numerator), int(denominator))
slash_symbols = ['/', '⧸']
for slash_symbol in slash_symbols:
if slash_symbol in fraction_str:
numerator, denominator = fraction_str.split(slash_symbol)
return Fraction(int(numerator), int(denominator))

import re
match = re.match(r'(\d*)\.(\d*)\((\d+)\)', fraction_str)
if match:
# supports notation like 2.(3) for 2.3333333333333...
# the () notation is widely accepted and is non ambiguous, for example
# 4.787878(457) means 4.787878457457457457457457... 457 repeating even tho 787878 was before
whole_part = match.group(1)
non_repeating = match.group(2)
repeating = match.group(3)
Expand All @@ -110,7 +115,9 @@ def parse_fraction(fraction_str):

return Fraction(numerator, denominator)

# whereas, 8.123(456)7 wouldnt match, so it would fall down to float(8.1234567) basically
if '.' in fraction_str:
return Fraction(str(float(fraction_str))).limit_denominator()
return Fraction(fraction_str.replace('(', '').replace(')', '')).limit_denominator()

# if there is no '.' it requires the string to match \d+ basically and will be interpreted as an integer
return Fraction(int(fraction_str), 1)

0 comments on commit 6358276

Please sign in to comment.