Last active
March 8, 2018 15:06
-
-
Save Mk-Etlinger/3902b19491161a78b127045b9c8d3551 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExpressionEvaluator | |
def self.parse(string) | |
complex_exps = string.scan(/-\s\d+\s\d+/) | |
simple_exps = string.gsub(/-\s\d+\s\d+/, '') | |
complex_sum(complex_exps) + simple_sum(simple_exps) | |
end | |
def self.complex_sum(array) | |
array.reduce(0) { |sum, exp| sum + (exp.split[1].to_i - exp.split[2].to_i) } | |
end | |
def self.simple_sum(string) | |
return 0 if string.empty? | |
string.gsub('- ', '-').split.reduce(0) { |sum, num| sum + num.to_i } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment