[thechat] Programming Problem

Beau Hartshorne beau at members.evolt.org
Fri Nov 9 01:35:10 CST 2001


Thanks for your tips Paul. So it can be done.

Here's the script:
http://www.cubeinc.ca/twenty_four.php

Here's the code:
http://www.cubeinc.ca/twenty_four.txt

Now if I was a truly clever programmer, I'd figure out a way to make it
work for any number of operators, for any number of elements (cards),
etc. but I think I'll take the beer instead. It looked like more work to
try to get the computer to do the permutations of ABCD than it was to
write out all 24 of them myself. I might have screwed up, but it doesn't
really matter.

There's no error checking or anything, I just wanted to see how it could
be done. If you enter all 1's, for example, you get a bunch of divide by
zero warnings. I know how try / catch blocks work in Java, but have no
idea how to do something like this in php. Is it possible? For now, I
just did this:

error_reporting(0);

Well, that's it for tonight.

Beau

> some handsome fool wrote:
> > Rather ugly, but would work. Might try doing it over the weekend.. 
> > interesting idea.
> 
> Hmm.. just hit me: good idea here would be to use 
> JavaScript.. then you can build a string and call eval() (PHP 
> might have similar.. don't know)
> 
> thus.. my loop from earlier, then
> 	str1 = a + op1 + b + op2 + c + op3 + d
> 	str2 = '(' + a + op1 + b + ')' op2 + c + op3 + d
> 	str3 = a + op1 + '(' + b + op2 + c + ')' + op3 + d
> 
> 	if (eval(str1) == 24) {
> 		// print str1	
> 	}
> 
> 	...
> 
> use arrays or something to make it nicer, obviously.





More information about the thechat mailing list