PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
It was typed directly in the forum and not run by a PHP interpreter.
By the way, I love returning from my function to avoid unnecessary else-s, but since I had to display the values at the end, returning is not an option.
Not sure if reference in the parameters is needed, it depends of what you mean by modify X
I had a feeling I had forgotten something in the constraints. I'll have to add "switch" because that is not what I had in mind. Fortunately (for me), your solution is in conflict with a different constraint, so I don't feel so bad about saying "denied."
McInfo wrote:"{" more than once
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 5:28 pm, edited 1 time in total.
<?php
/**
If Y is...
...less than 1, set X to 0.
...in the range 1-49, add Y to X.
...in the range 50-99, add 50 to X.
...greater than 99, add 100 to X.
Here's the catch. The following cannot appear in your script:
"if" more than twice
"{" more than once
"else"
"?"
"eval"
*/
function challenge($x, $y)
{
$array = array(0 => $x + ($y % 50),
1 => $x + 50);
if($y < 1)
return "0";
$key = 0;
for($i = 0; $i <= ($y / 50); $i++)
$key = $i;
if(array_key_exists($key, $array))
return $array[$key];
return $x + 100;
}
print challenge((int) @$_GET['x'], (int) @$_GET['y']);
?>
I had a feeling I had forgotten something in the constraints. I'll have to add "switch" because that is not what I had in mind. Fortunately (for me), your solution is in conflict with a different constraint, so I don't feel so bad about saying "denied."
McInfo wrote:"{" more than once
Yeah, but it's said script, and not a function,so if I just move this in a global scope, with $_GET variables, it won't conflict any constrain
Anyway, the interesting thing in any challenge is not just achieving the goal, but the different ways to do this.
Congratulations, flying_circus, this is really interesting solution
Darhazer wrote:Yeah, but it's said script, and not a function,so if I just move this in a global scope, with $_GET variables, it won't conflict any constrain
Anyway, the interesting thing in any challenge is not just achieving the goal, but the different ways to do this.
That's true on both points. Your solution (minus the function) is valid with the constraints you were given, and I agree that it is interesting to see the results of different interpretations of the challenge.
My intention with this challenge is to eventually share a pattern that I think is somewhat clever. I tried to design the constraints in a way that would coax challengers into discovering the pattern without revealing too much. Any failure to do that is due to a lack of planning on my part.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 5:29 pm, edited 1 time in total.
Ooops, I misread that one - the italic font makes the "if" to appear like a "{" at first look
Never mind, I'm almost done with the if-free, single line version
Last edited by VladSun on Wed Jan 27, 2010 7:58 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
McInfo wrote:My intention with this challenge is to eventually share a pattern that I think is somewhat clever. I tried to design the constraints in a way that would coax challengers into discovering the pattern without revealing too much. Any failure to do that is due to a lack of planning on my part.
What's the importance of the N (in this case 50) period? Is it a must for your design or not?
If there is no such requirement (i.e. the "periodic thing"), I think there are only two approaches available - the raw NAND/NOR and a lookup table (not a "raw NAND/NOR"). In this particular case, the NAND/NOR solution is easier because input data is given in intervals, which is much more expensive to be done with lookup tables.
There are 10 types of people in this world, those who understand binary and those who don't