Created
May 9, 2013 10:09
-
-
Save encodes1/5546687 to your computer and use it in GitHub Desktop.
Week 1 - Challenge 4 - Solution. This is a basic attempt at this solution. It does not take into account brackets. allthough i'm working on finding a nice solution to that. First time i've used ruby as well. some nice language features :)
This file contains 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
nums = [100, 13.2, 92.123, 0.124, 12] | |
ops = ['+','/','-','*'] | |
highest = 0 | |
eq = '' | |
highesteq = '' | |
perms = nums.to_a.permutation.map() | |
perm2 = ops.to_a.permutation.map() | |
perms.each{ | n | | |
perm2.each { | o | | |
eq = '' | |
#put into for each loop | |
i =0 | |
while i < nums.length do | |
eq = eq + n[i].to_s() + o[i].to_s() | |
i +=1 | |
end | |
eq = eq + o[nums.length+1].to_s() | |
v = eval(eq) | |
if v > highest | |
highest = v | |
highesteq = eq | |
end | |
#work out a nice way to include brackets | |
# hard coded wise they will be | |
} | |
} | |
print highesteq + "\n" | |
print highest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment