neylitalo Posted December 23, 2006 Share Posted December 23, 2006 I actually think thorpe was being sarcastic, trying to point out that the extensions are there for a reason: Because they're useful, and save a lot of time. Which you acknowledged in your edit. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146801 Share on other sites More sharing options...
The Little Guy Posted December 23, 2006 Author Share Posted December 23, 2006 Here is the best I could come up with for converting date to day name:[code]<?phpif(!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. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146813 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 lol I didnt get what you mean... Is this some sort of physics things?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146822 Share on other sites More sharing options...
redbullmarky Posted December 23, 2006 Share Posted December 23, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146835 Share on other sites More sharing options...
neylitalo Posted December 23, 2006 Share Posted December 23, 2006 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]<?phpfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146839 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 @neylitalo- What's the next challenge?Orio Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146859 Share on other sites More sharing options...
neylitalo Posted December 23, 2006 Share Posted December 23, 2006 Ah, my apologies - I thought The Little Guy was going to do a do-over. :)Next challenge:Duplicate the functionality of the str_shuffle function. Without using the str_shuffle function. ;) Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146875 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 With PHP5 it's easy, by using the str_split() fucntion:[code]<?phpfunction 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. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146876 Share on other sites More sharing options...
neylitalo Posted December 23, 2006 Share Posted December 23, 2006 oh, crap... I didn't think of that. I was thinking of more of a "for" loop type of thing... but I suppose that's one skill this thing teaches people: Find shortcuts. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146880 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Share Posted December 23, 2006 I'm not very good at all the different functions but can I offer my train of thought to resolve this one? Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146884 Share on other sites More sharing options...
neylitalo Posted December 23, 2006 Share Posted December 23, 2006 It shouldn't require too many functions, just some simple string manipulation - you're two-thirds of the way there, why not just bump out the code? Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146887 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Share Posted December 23, 2006 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146894 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 Works fine :) One small problem is that it replaces all of the capital letters and all non-alphabetic characters with m's, but nevermind :)What's the next challenge?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146915 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Share Posted December 23, 2006 Are you all familiar with the Ceasers Box cipher as featured in the Da Vinci Code? Where a string of letters are rearranged into a grid etc etc to decipher its encoded message?How about a function/s that will encrypt/decipher a string using this method? Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146920 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 Sorry, never read that book. Can you explain/link/example ?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146923 Share on other sites More sharing options...
.josh Posted December 23, 2006 Share Posted December 23, 2006 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146948 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Share Posted December 23, 2006 This should explain it:http://www.wikihow.com/Decode-a-Caesar-Box-Code Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146965 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 [code]<?phpfunction 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]." "; } $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. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146979 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Share Posted December 23, 2006 I was thinking more along the lines of being able to input a sting such as "bbdiaogdg" and it decipher it to produce "bigbaddog" Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-146982 Share on other sites More sharing options...
monkey_05_06 Posted December 24, 2006 Share Posted December 24, 2006 I was actually hoping someone would solve my challenge mathematically. Oh well. :POrio your code doesn't...decode properly. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-147186 Share on other sites More sharing options...
Daniel0 Posted December 24, 2006 Share Posted December 24, 2006 I'm a bit confused. What's the current challenge? Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-147270 Share on other sites More sharing options...
Orio Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-147293 Share on other sites More sharing options...
.josh Posted January 14, 2007 Share Posted January 14, 2007 Topic inactive for 3 weeks. Unstickying. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-160619 Share on other sites More sharing options...
Barand Posted January 14, 2007 Share Posted January 14, 2007 For fun, I decided to take up the recursive Fibonacci challenge[code]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-160709 Share on other sites More sharing options...
.josh Posted January 15, 2007 Share Posted January 15, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/2/#findComment-160963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.