[Challenge] Conditioning

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!

Moderator: General Moderators

User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

[Challenge] Conditioning

Post 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.
Last edited by McInfo on Mon Jun 14, 2010 5:26 pm, edited 1 time in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: [Challenge] Conditioning

Post 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 :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: [Challenge] Conditioning

Post 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.
Last edited by McInfo on Mon Jun 14, 2010 5:28 pm, edited 1 time in total.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: [Challenge] Conditioning

Post 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']);
?>
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: [Challenge] Conditioning

Post 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 :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: [Challenge] Conditioning

Post 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.
Last edited by McInfo on Mon Jun 14, 2010 5:29 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] Conditioning

Post 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'];
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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 ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: [Challenge] Conditioning

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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 ;)
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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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 ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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];
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] Conditioning

Post by Eran »

these logical-gate solutions are pretty neat, vlad :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] Conditioning

Post 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;
}
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply