Jump to content

The PHP Challenge


The Little Guy

Recommended Posts

  • Replies 88
  • Created
  • Last Reply

Top Posters In This Topic

Here is the best I could come up with for converting date to day name:

[code]<?php
if(!isset($_POST['submit'])){
?>
<form action="<?echo$_SERVER['PHP_SELF']?>" method="post">
Enter date :<br>
Day: <input type="text" name="day" maxlength="2" size="4"><br>
Month: <select name="month">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>


</select>
<br>
year: <input type="text" name="year" maxlength="4" size="4"><br>
<input type="submit" name="submit" value="Go!">

</form>
<?php
}else{
$mkeys = array("1" => 1, "2" => 4, "3" => 4, "4" => 0, "5" => 2, "6" => 5, "7" => 0, "8" => 3, "9" => 6, "10" => 1, "11" => 4, "12" => 6);
function convert_to_week_day($date,$month,$year){
$day = ( $year + 1900 ) + ( $year + 1900 ) / 4 + $mkeys[$month] + $date + 2;
/* The above counts the leap day even if it occurs later in the year */
if(( $year > 1900 ) && ( $year % 4 == 0 ) && ( $month < 2 )){
$day--;
}
if($date == date("j")){
$day--;
}
if($year < date("Y")){
$day++;
}
if($date < date("d")){
$day--;
}
$day %= 7;
return $day;
}

$num = convert_to_week_day($_POST['day'],$_POST['month'],$_POST['year']);
$dayName = array("0" => "Sunday", "1" => "Monday", "2" => "Tuesday", "3" => "Wednesday", "4" => "Thursday", "5" => "Friday", "6" => "Saturday");
echo $dayName[$num].'<br>';
}
?>[/code]

Challenge:

From a form get the drop height, get the crumpled up paper weight, the the bowling ball weight, calculate if the ball will hit the ground first, or the bowling ball from those three things.
Link to comment
Share on other sites

LittleGuy - excluding factors such as aerodynamics, which would be far too complicated, the item that would hit the floor first would be the one that is dropped from the lower height.
Theory has it that two objects dropped from an equal height, regardless of size or weight, will hit the ground at the same time.
So me thinks you need to come up with a new challenge :)
Link to comment
Share on other sites

Mark, you're kinda correct - it's not a theory, it's a law. ;)

But I felt like doing it properly, so here it is. :P Ahh, fun with physics and sarcasm.

[code]<?php
function getFirstToHit($bowlingBallMass, $paperBallMass, $dropHeight)
{
    /*
        The masses are in g and the dropheight is in m.
        However, the object's mass is not a factor in
        calculating vertical acceleration, so don't be
        alarmed if you don't see $bowlingBallMass or $paperBallMass
        anywhere in the rest of this function.
    */
 
    // Acceleration due to gravity, in m/s/s
    $acceleration = 9.81;

    // The equation to use in this case is t = sqrt(2y)/sqrt(g). t is the time taken to fall, g is acceleration due to gravity, and y is the vertical distance the object falls.
 
    // Since the equations are exactly the same, I'm only going to calculate it once...
    $timeToHit = sqrt(2*$dropHeight)/sqrt($acceleration);

    return "\n\tSince mass has nothing at all to do with calculating
    vertical acceleration or velocity, we're going to make a few
    assumptions which will lead us to the conclusion that they
    hit at the exact same time. We'll assume that:
        A)  They have the same surface area. This is required due
            to the fact that we cannot easily calculate vertical
            acceleration with compensation for surface area.
        B)  They have no initial velocity.\n
   
    With these assumptions, they hit the ground after ".number_format($timeToHit, 3)." seconds.\n\n";
}

?>[/code]


And I, too, think that he needs to come up with something valid. ;D
Link to comment
Share on other sites

With PHP5 it's easy, by using the str_split() fucntion:

[code]<?php

function str_shuff($string)
{
$split = str_split($string);
shuffle($split);
return implode("", $split);
}

?>[/code]


[b]Next challenge:[/b]
I'll use the "format" neylitalo gave- make a function that duplicates the functionalitly of str_rot13(). Do it nicely- not with with a few lines of replacements ;)

Orio.
Link to comment
Share on other sites

I have tried to put together a function, but it is untested as I have not got PHP on the machine I am using.  Perhaps somebody could check it for me?

[code]
<?php

function str_rot13($string) {

$alpha = array(
1 => 'a',
2 => 'b',
3 => 'c',
4 => 'd',
5 => 'e',
6 => 'f',
7 => 'g',
8 => 'h',
9 => 'i',
10 => 'j',
11 => 'k',
12 => 'l',
13 => 'm',
14 => 'n',
15 => 'o',
16 => 'p',
17 => 'q',
18 => 'r',
19 => 's',
20 => 't',
21 => 'u',
22 => 'v',
23 => 'w',
24 => 'x',
25 => 'y',
26 => 'z'
);

$numa = array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
'e' => 5,
'f' => 6,
'g' => 7,
'h' => 8,
'i' => 9,
'j' => 10,
'k' => 11,
'l' => 12,
'm' => 13,
'n' => 14,
'o' => 15,
'p' => 16,
'q' => 17,
'r' => 18,
's' => 19,
't' => 20,
'u' => 21,
'v' => 22,
'w' => 23,
'x' => 24,
'y' => 25,
'z' => 26
);

$split = str_split($string);

$num = count($split);

$string = "";

for ($x=0;$x<$num;$x++) {

$a = $split[$x];

$b = $numa[$a];

if ($b < 14) {

$c = $b + 13;

$string .= $alpha[$c];

}

if ($b > 13) {

$c = $b - 13;

$string .= $alpha[$c];

}

}

return $string;

}

?>
[/code]
Link to comment
Share on other sites

okay i know i'm kinda late on this because I just read the challenge not too long ago, but here's my version of the rot13:

[code]
<?php
function rot13($string) {
  $length = strlen($string); // get length of string
  $newstring = "";          // start of converted string

// loop through each letter and shift 13 places
  for ($x = 0; $x < $length; $x++) {
      // if the letter is a capital letter...
if (((ord($string[$x]) >= 65) && (ord($string[$x]) <= 90))) {
        // figure out if shifting 13 spaces involves starting over at 'A'
$newstring .= ((ord($string[$x]) + 13) > 90) ? chr(((ord($string[$x]) + 12) - 90) + 65) : chr(ord($string[$x]) + 13);
      // if the letter is a lowercase letter...
} elseif (((ord($string[$x]) >= 97) && (ord($string[$x]) <= 122))) {
        // figure out if shifting 13 spaces involves starting over at 'a' 
  $newstring .= ((ord($string[$x]) + 13) > 122) ? chr(((ord($string[$x]) + 12) - 122) + 97) : chr(ord($string[$x]) + 13);
      // if it is anything else...
      } else {
        // use what it is, since rot13 only shifts alpha chars
    $newstring .= $string[$x];  
      } // end else
  } // end for loop
  return $newstring;
} // end function rot13

// example
$blah = "Crayon Violent is Teh Shiz!";
$rotblah = rot13($blah);
echo "$blah <br /> $rotblah";
?>
[/code]
Link to comment
Share on other sites

[code]<?php

function form($text="")
{
if(!empty($text))
echo "<center>".$text."</center>";
echo '<hr>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
echo '<input type="text" name="str">';
echo '<br><br><input type="submit" value="Encode!" name="submit">';
echo '</form>';
echo '<hr>';
die();
}


if(!isset($_POST['submit']))
form();





$str = stripslashes($_POST['str']); //magic quotes ;)
$str = str_replace(" ", "", $str);
$size = ceil(sqrt(strlen($str)));
$letters = str_split($str);
while (count($letters) < $size*$size)
$letters[] = " ";

$result = "";
for($i = 0; $i < $size; $i++)
{
for($j = 0; $j < $size; $j++)
{
$result .= $letters[($j*$size)+$i]."&nbsp;";
}
$result .= "<br />";
}

form($result."<br>");

?>[/code]

You can test it [url=http://www.oriosriddle.com/test.php]here[/url]

[b]Next challenge:[/b]
Similar to my previous one (with rot13), but this time you need to receive both a string and the number of shifts!

Orio.
Link to comment
Share on other sites

I dont have time right now to fix/upgrade my previous code, so if you skip it- it's what I gave, if are not skipping it- it's what I had to do.
The challenge I gave was:
[quote author=Orio link=topic=119299.msg490756#msg490756 date=1166902682]
[b]Next challenge:[/b]
Similar to my previous one (with rot13), but this time you need to receive both a string and the number of shifts![/quote]

Orio.
Link to comment
Share on other sites

  • 3 weeks later...
For fun, I decided to take up the recursive Fibonacci challenge

[code]
<?php
function fib($n) {
    if ($n < 3) return 1;
    return fib ($n-2) + (fib($n-1));
}

for ($i = 1; $i <= 40; $i++) echo fib($i). ', ';
?>
[/code]

-->[pre]
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,
10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155
[/pre]
Don't try this at home, it takes 33 minutes. The first 33 numbers took a minute, the rest were the killer.

My challenge is a problem I first came across 40 years ago in the pre-microcomputer era.

[b]What is the smallest integer such that when the first digit is moved to the end the new number formed is exactly 1.5 times the original?[/b]
Link to comment
Share on other sites

okay I give up. Here's my code:
[code]
<?php
  set_time_limit(0);
  $num = 1;
  $found = false;
      while ($found == false) {
        $newnum = substr($num, 1) . substr($num, 0, 1);
if (($newnum / $num) == 1.5) {
            $found = true;
        } else {
    $num++;
}
  }
  echo "number is $num<br>"; 
?>
[/code]

And it just goes on and on and on.... did I take a wrong turn or is this some kind of trick/riddle...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.