TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-17-2009, 11:51 AM   #1 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Box 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.
codefreek is offline  
Reply With Quote
Old 08-17-2009, 12:16 PM   #2 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

Programming Challenges one:
make a function that do this:

Get Filename From Domain Path


difficulty rate: easy
codefreek is offline  
Reply With Quote
Old 08-17-2009, 01:09 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
sketchMedia is offline  
Reply With Quote
Old 08-17-2009, 01:14 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Maybe I'm being dumb but it's my understanding that there is no filename in a domain path.
Salathe is offline  
Reply With Quote
Old 08-17-2009, 01:42 PM   #5 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 08-17-2009, 01:49 PM   #6 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

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!
codefreek is offline  
Reply With Quote
Old 08-17-2009, 02:33 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by codefreek View Post
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 is offline  
Reply With Quote
Old 08-18-2009, 09:11 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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!

Last edited by Salathe : 08-19-2009 at 02:41 PM.
Salathe is offline  
Reply With Quote
Old 08-18-2009, 09:20 PM   #9 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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++;
}
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 08-18-2009, 09:38 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
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?
Salathe is offline  
Reply With Quote
Old 08-18-2009, 09:51 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
echo substr($rhyme0197); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 08-18-2009, 09:57 PM   #12 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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);
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 08-18-2009, 11:11 PM   #13 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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

__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 08-19-2009, 12:41 AM   #14 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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);
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 08-19-2009, 11:04 AM   #15 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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?
Salathe is offline  
Reply With Quote
Old 08-19-2009, 01:32 PM   #16 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

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 ...
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday

Last edited by ioan1k : 08-19-2009 at 02:55 PM. Reason: Clarify line count confusion :)
ioan1k is offline  
Reply With Quote
Old 08-19-2009, 01:54 PM   #17 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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 )
__________________
My Blog

Last edited by Salathe : 08-19-2009 at 02:42 PM.
Enfernikus is offline  
Reply With Quote
Old 08-19-2009, 02:04 PM   #18 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

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>
?
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-19-2009, 02:20 PM   #19 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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.
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 08-19-2009, 02:43 PM   #20 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented Programming DizzyD General 2 02-25-2009 03:43 AM
how many programming languages do you know fluently? sarmenhb General 12 12-10-2008 06:26 AM
If a programming language was a boat… Wildhoney The Lounge 9 04-05-2008 07:24 AM
The speed of programming Devels General 1 02-01-2008 10:56 PM
12 common programming mistakes to avoid sunilbhatia79 General 0 11-16-2007 05:59 PM


All times are GMT. The time now is 07:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design