 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
08-17-2009, 11:51 AM
|
#1 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Programming Challenges
anyone up for some Programming Challenges ?
If so, post a challenge in here. So people can try it out.
Brain pumping.. ;)
-CF
PS: i am working on a Challenge, will post it soon.
Last edited by codefreek : 08-17-2009 at 12:22 PM.
|
|
|
|
08-17-2009, 12:16 PM
|
#2 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Programming Challenges one:
make a function that do this:
Get Filename From Domain Path
difficulty rate: easy
|
|
|
|
08-17-2009, 01:09 PM
|
#3 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
PHP Code:
<?php $domain = 'http://www.talkphp.com/dave/index.html';
$pp = pathinfo($domain); echo $pp['basename'];
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
08-17-2009, 01:14 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Maybe I'm being dumb but it's my understanding that there is no filename in a domain path.
|
|
|
|
08-17-2009, 01:42 PM
|
#5 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
You name your php scripts right ?
a file(phpfile) name: index.php.. = filename..
PHP Code:
//this works as well ;) function fileName($path) { return substr($path, strrpos($path, '/')+1, strlen($path)); }
|
|
|
|
08-17-2009, 01:49 PM
|
#6 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Programming Challenges two:
Simple HTTP authentication login
make a login script with use of WWW-Authenticate header.
difficulty rate: easy
PS: please keep the game running by posting challenges..
brainPump.. Come on now!
|
|
|
|
08-17-2009, 02:33 PM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by codefreek
You name your php scripts right ?
a file(phpfile) name: index.php.. = filename..
PHP Code:
//this works as well ;)
function fileName($path)
{
return substr($path, strrpos($path, '/')+1, strlen($path));
}
|
You said domain path, not URL. Are we supposed to write silly solutions if, for example, a native PHP function exists for doing whatever the challenge requires? 
|
|
|
|
08-18-2009, 09:11 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
New challenge! Challenge 3
Given the string variable $rhyme (with the contents as below), echo that string except for the last line.
Starting Code
PHP Code:
<?php
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!";
Desired outputPeter Piper picked a peck of pickled peppers;
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where's the peck of pickled peppers Peter Piper picked?
Go forth and solve: be creative but not silly!
Last edited by Salathe : 08-19-2009 at 02:41 PM.
|
|
|
|
08-18-2009, 09:20 PM
|
#9 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
is this what you wanted, or did I misunderstand?
PHP Code:
<?php
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!";
$r = explode("\n",$rhyme); $x = 0;
while($x < 4) { echo "{$r[$x]}\n"; $x++; }
|
|
|
|
08-18-2009, 09:38 PM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by ETbyrne
is this what you wanted, or did I misunderstand?
|
It outputs the desired text, so yes it happily solves the challenge.
Anyone else got any ideas, or would you all normally explode and loop? 
|
|
|
|
08-18-2009, 09:51 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
PHP Code:
echo substr($rhyme, 0, 197);
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
08-18-2009, 09:57 PM
|
#12 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
php Code:
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!"; echo substr($rhyme, 0, strpos($rhyme, '?') + 1);
|
|
|
|
08-18-2009, 11:11 PM
|
#13 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
Just another way, it's pretty pointless.
PHP Code:
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!";
$data = explode(' ', $rhyme, -1);
for ($n = 0; $n < 34; $n++) { echo $data[$n] , ' '; }
|
|
|
|
08-19-2009, 12:41 AM
|
#14 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
Here's another way:
PHP Code:
<?php
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!";
$r = explode("\n",$rhyme); $end = end($r);
foreach($r as $line) { if($line != $end) { echo $line; } }
Also more simply:
PHP Code:
<?php
$rhyme = "Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked? This is the line that needs to be removed!";
echo str_replace("\nThis is the line that needs to be removed!",'',$rhyme);
|
|
|
|
08-19-2009, 11:04 AM
|
#15 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Hmm, it is very interesting to see all of your approaches; for example, I wouldn't have thought to look for the only question mark in there and to use that as a marker!
Well, to conclude this mini-challenge here is what I was thinking of when constructing the challenge:
PHP Code:
$rhyme = "Peter Piper picked a peck of pickled peppers;
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where's the peck of pickled peppers Peter Piper picked?
This is the line that needs to be removed!";
echo preg_replace('/.*\z/', '', $rhyme);
Guess I have an unhealthy predilection for regular expressions.
So who wants to create the next mini-challenge?
|
|
|
|
08-19-2009, 01:32 PM
|
#16 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
Challenge 4 (Salathe: added title so we can keep track)
Here is one that may be a brain teaser ...
Without using the following Control Structures, Operators, print or echo. Create a snippet that while display a list of numbers 0 - 100 and backdown to 100 - 0 using only 3 lines of code in a unordered listed format
Only PHP Counts toward the line count.
Multiple un-nested functions existing on one line are considered a separate line for each function.
All function logic (anonymous and custom) are considered a seperate line from the declaration.
php Code:
function($value){ $logic = 'asd'; }
Would be 2 lines
Have Fun!
Final Result should be - 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7 ... etc etc
- 100
- 100
- 99
- 98 ...
Last edited by ioan1k : 08-19-2009 at 02:55 PM.
Reason: Clarify line count confusion :)
|
|
|
|
08-19-2009, 01:54 PM
|
#17 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
Hope 2 is fine.
P.S. - I'm taking un-nested functions to be something along the lines of
php Code:
this(); is(); a(); cool(); unnested(); string(); of(); functions();
--- And I guess I'll make another little challenge
Challenge 5 (Salathe: added challenge number so we can keep track))
Construct your a looping method to mimic for using only variables inside the scope of the function ( no global keyword )
( I don't expect the break keyword to be respected, that would be a slight more difficult )
Last edited by Salathe : 08-19-2009 at 02:42 PM.
|
|
|
|
08-19-2009, 02:04 PM
|
#18 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
Taking a second look, while your code does produce the output, it would be considered 8 lines. Logic performed for anonymous functions would be coded in the same manner as a any other function ... also per the PHP Doc's this is the manner it is done.
http://us2.php.net/manual/en/functions.anonymous.php
Also what about ?
|
|
|
|
08-19-2009, 02:20 PM
|
#19 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
Posted the update with ul ( though browsers will format it as a list without the ul but for the purpose of being semantic ) - not very elegant but, it does it's thing.
|
|
|
|
08-19-2009, 02:43 PM
|
#20 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
With regards to challenge 4, I'm a little fuzzy on what actually counts as a line so I threw everything onto one long one!
PHP Code:
printf("<ul>\n\t<li>".implode("</li>\n\t<li>", array_merge(range(0,100),range(100,0)))."</li>\n</ul>\n");
Edit: can't echo, use printf instead.
Last edited by Salathe : 08-19-2009 at 03:12 PM.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|