Skip to content

Instantly share code, notes, and snippets.

@TheMadHau5
TheMadHau5 / max_noparens.rb
Last active April 27, 2020 20:05
With a given input of an expression, this code solves for the bracket-ing that results in the highest result.
def getmax nums, ops, dbg=0, &f
# f is action performed on numbers before comparing (like taking absolute value)
maxn = nil
pat = nil
(0...ops.size).to_a.permutation do |p| # iterate over each possible pattern
begin
n = solve(nums, ops, p, dbg - 1) # attempt to solve (based on given pattern)
if n.infinite?
n = nil
raise ZeroDivisionError, "divided by 0"