r/dailyprogrammer • u/nottoobadguy • Feb 12 '12
[2/12/2012] Challange #4 [difficult]
today, your challenge is to create a program that will take a series of numbers (5, 3, 15), and find how those numbers can add, subtract, multiply, or divide in various ways to relate to eachother. This string of numbers should result in 5 * 3 = 15, or 15 /3 = 5, or 15/5 = 3. When you are done, test your numbers with the following strings:
4, 2, 8
6, 2, 12
6, 2, 3
9, 12, 108
4, 16, 64
For extra credit, have the program list all possible combinations.
for even more extra credit, allow the program to deal with strings of greater than three numbers. For example, an input of (3, 5, 5, 3) would be 3 * 5 = 15, 15/5 = 3. When you are finished, test them with the following strings.
2, 4, 6, 3
1, 1, 2, 3
4, 4, 3, 4
8, 4, 3, 6
9, 3, 1, 7
6
u/_redka 0 0 Feb 12 '12 edited Feb 12 '12
Ruby, 6 lines. Added modulo just for fun. Takes strings as big as you want. http://pastie.org/3368678
results: http://pastie.org/3368674
4
u/Ttl Feb 12 '12
My incomprehensible python solution:
1
u/SaxSalute Feb 12 '12
Is there a typo in the example for 4 numbers? Where did the 15 come from?
3
u/stevelosh Feb 12 '12 edited Feb 12 '12
First attempt in Clojure:
EDIT: Cleaned up a few things.
2
u/cooper6581 Feb 12 '12
Similar to leobm's solution (using permutations), but in Python http://codepad.org/9dPCDFyE
Now trying to figure out how to do any length is going to keep me up all night...
1
u/wpcq Feb 12 '12
A trivial solution: http://paste.kde.org/398486/ I'm still working on a better solution. The extra credit portion of the problem strikes me as something that Haskell would do elegantly.
1
u/shakra Feb 12 '12
I'm learning Haskell and here is my solution that can handle number lists with up to 4 elements.
1
u/bigmell Feb 12 '12
Perl solution no extra credit. Couldnt think of an elegant one. Maybe later i'll brute force it and keep and operator stack for the extra credit. #!/usr/bin/perl -w
1
u/[deleted] Feb 13 '12
1
u/bigmell Feb 13 '12
Ok here is the full solution with extra credit in Perl. It also accepts an infinite string of numbers. Had to use the Math:BaseArith package from cpan though to keep track of the permutations. Its brute force so each calculation is n4. Not sure it can be done faster.
1
u/drb226 0 0 Feb 13 '12 edited Feb 13 '12
26 lines of Haskell: http://hpaste.org/63601 Output: http://hpaste.org/63602
I was kind of lazy about dealing with duplicates (e.g.
2 + 3 = 5,3 + 2 = 5). Also, I use integer division, so9 / 7 = 1.1
u/stonedcrowza Feb 13 '12
Ugly Linq!
1
u/RazerWolf Feb 14 '12 edited Feb 14 '12
F# - 57 lines (http://pastebin.com/NVt4ywz7)
Apparently I misunderstood the extra credit portion of the problem and created a general solver for strings of operations on all subsets of the supplied operands (e.g. it will solve 1 + 2 + 3 = 6, not just results of operations from any 2 operands). Obviously complexity is 2n, although I'm sure dynamic programming can help here somewhat, I'm too lazy to attempt.
EDIT: was lazy about dealing with duplicates as well, and am also using integer division. Also the output strings are somewhat misleading, since they don't follow order of operations rules: it should always be assumed there are enough parentheses to make every single calculation left-associative, since that's what the program is doing (evaluating subsequent calculations with different operators on every "level")