TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Programming Challenges (http://www.talkphp.com/general/4862-programming-challenges.html)

codefreek 08-17-2009 11:51 AM

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.

codefreek 08-17-2009 12:16 PM

Programming Challenges one:
make a function that do this:

Get Filename From Domain Path


difficulty rate: easy

sketchMedia 08-17-2009 01:09 PM

PHP Code:

<?php
$domain 
'http://www.talkphp.com/dave/index.html';

$pp pathinfo($domain);
echo 
$pp['basename'];


Salathe 08-17-2009 01:14 PM

Maybe I'm being dumb but it's my understanding that there is no filename in a domain path.

codefreek 08-17-2009 01:42 PM

You name your php scripts right ?
a file(phpfile) name: index.php.. = filename..



PHP Code:

//this works as well ;)
function fileName($path)
{
     return 
substr($pathstrrpos($path'/')+1strlen($path));



codefreek 08-17-2009 01:49 PM

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!

Salathe 08-17-2009 02:33 PM

Quote:

Originally Posted by codefreek (Post 27822)
You name your php scripts right ?
a file(phpfile) name: index.php.. = filename..



PHP Code:

//this works as well ;)
function fileName($path)
{
     return 
substr($pathstrrpos($path'/')+1strlen($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? *!*

Salathe 08-18-2009 09:11 PM

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 output
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?
Go forth and solve: be creative but not silly!

ETbyrne 08-18-2009 09:20 PM

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++;
}


Salathe 08-18-2009 09:38 PM

Quote:

Originally Posted by ETbyrne (Post 27891)
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? 8-)

sketchMedia 08-18-2009 09:51 PM

PHP Code:

echo substr($rhyme0197); 


Enfernikus 08-18-2009 09:57 PM

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);

adamdecaf 08-18-2009 11:11 PM

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] , ' ';



ETbyrne 08-19-2009 12:41 AM

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);


Salathe 08-19-2009 11:04 AM

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. :-D

So who wants to create the next mini-challenge?

ioan1k 08-19-2009 01:32 PM

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 ...

Enfernikus 08-19-2009 01:54 PM

php Code:
<ul><?php array_map(function($Value){ printf('<li>%s</li>', $Value); }, range(0, 100));
array_map(function($Value){ printf('<li>%s</li>', $Value); }, range(100, 0))?></ul>

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 )

ioan1k 08-19-2009 02:04 PM

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
Code:

<ul> </ul>
?

Enfernikus 08-19-2009 02:20 PM

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.

Salathe 08-19-2009 02:43 PM

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! :-D

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.


All times are GMT. The time now is 10:21 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0