Skip to content

Instantly share code, notes, and snippets.

@encodes1
Created May 9, 2013 10:09
Show Gist options
  • Save encodes1/5546687 to your computer and use it in GitHub Desktop.
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 :)
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