Page 1 of 2

[Challenge] Conditioning

Posted: Tue Jan 26, 2010 4:51 pm
by McInfo
Write a script that accepts two inputs (X and Y), modifies X according to the following rules, then displays the values of X and Y.

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"
  • "switch"
Edit: Added "switch"

Edit: This post was recovered from search engine cache.

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 5:00 pm
by Darhazer

Code: Select all

 
function modifyX(&$x, &$y) {
  switch (true) {
     case ($y < 1)
            $x = 0;
            break;
     case ($y < 50)
            $x += $y ;
            break;
     case ($y < 100)
            $x += 50;
            break;
      default:
           $x+=100;
           break;
  }
       echo 'X: ', $x, PHP_EOL, 'Y: ', $y, PHP_EOL
}
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 :)

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 5:13 pm
by McInfo
Darhazer wrote:

Code: Select all

function modifyX(&$x, &$y) {
  switch (true) {
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.

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 6:08 pm
by flying_circus

Code: Select all

<?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&#058;
 
    "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']);
?>

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 6:17 pm
by Darhazer
McInfo wrote:
Darhazer wrote:

Code: Select all

function modifyX(&$x, &$y) {
  switch (true) {
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 :)

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 7:42 pm
by McInfo
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.

Re: [Challenge] Conditioning

Posted: Tue Jan 26, 2010 8:34 pm
by Eran
My attempt:

Code: Select all

function modify($x,$y) {
    $states = array(1 => 0,50 => ($x + $y),100 => ($x + 50));
    foreach($states as $boundary => $value)
        if($y < $boundary) 
            return $value;
    return $x + 100;        
}
 
if(isset($_GET['x']) && isset($_GET['y']))
    echo 'x: ' . modify($_GET['x'],$_GET['y']) . ' y: ' . $_GET['y'];

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 4:58 am
by VladSun
That's too easy ;)

Code: Select all

function lim($x, $y)
{
    if ($y < 1)
        return 0;
    if ($y < 50)
        return $x + $y;
    if ($y < 100)
        return $x + 50;
    return $x + 100;
} 
I suggest to add the "if" keyword into the forbidden words list ;)

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 5:05 am
by Darhazer
VladSun wrote:That's too easy ;)

Code: Select all

function lim($x, $y)
{
    if ($y < 1)
        return 0;
    if ($y < 50)
        return $x + $y;
    if ($y < 100)
        return $x + 50;
    return $x + 100;
} 
I suggest to add the "if" keyword into the forbidden words list ;)
You cannot use if more than twise, and your solution have 3 if statements.

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 5:09 am
by VladSun
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 ;)

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 5:16 am
by VladSun

Code: Select all

function limit($y, $x)
{
     return (($y > 0 && $y < 50)*($x + $y) + ($y >= 50 && $y < 100)*(50+$x) + ($y >= 100)*(100+$x));
}
Simple binary logic ;)

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 5:29 am
by VladSun
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.

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 6:45 am
by VladSun
A kind of lookup table solution:

Code: Select all

function limit($y, $x)
{
    $lt = array
    (
        $y < 1                  => 0,
        $y >= 1 && $y < 50      => $y + $x,
        $y >= 50 && $y < 100    => 50 + $x,
        $y >= 100               => 100 + $x,
    );
 
    return $lt[1];
}

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 8:00 am
by Eran
these logical-gate solutions are pretty neat, vlad :)

Re: [Challenge] Conditioning

Posted: Wed Jan 27, 2010 8:29 am
by VladSun
:) thanks
I also tried to use the min()/max() functions but it got too complicated ...

PS: Anyway - here it is:

Code: Select all

function limit($y, $x)
{
    $y = max(0, $y);
    $y = min(100, max(floor($y/50)*50, $y%50));
 
    return $x*min(1, $y) + $y;
}