Jump to content
MakeWebGames

Mini Coding Contests


a_bertrand

Recommended Posts

Ok: I have an assignment for you guys:

Write a script which reads a web page and extracts all the URL from the links...

2 criteria to designate the winner: short code, and fully working.

Time: 1 week. Next Sunday I will check the result and designate the winner, ok?

Submission: via email: bertrand (at) nodalideas {dot} com

Language: PHP (4 or 5)

You may submit as many trials you want, I will check them as soon as possible and give you my comment out. At then end I will post the winner name here, and his/her code. So please provide your forum name, and zip the code...

Link to comment
Share on other sites

Re: Mini Coding Contests

 

2nd assignement: (an easy easy one)

Build the following figure with PHP and loops (no HTML / or huge echo block allowed)

*

***

*****

***

*

The shape need to be 10 height / 10 width

Same rules as before for the deadline and the submission

um... following the pattern, it would have to be 11 or 9 (seeing as to how the rows all contain an odd number of asterisks.

would you accept one of those? if so, I'm done.

or is there something I'm missing?

Link to comment
Share on other sites

Re: Mini Coding Contests

for the 2nd assignment,

GD is not allowed, you have to do it via ECHO :-P

And my mistake, it need to be 9 or 11 width... Actually it doesn't really matter as what is important is to see if you can code it, and if yes if you discover some shortcuts. I will give you a few solutions at the end... you will see, some are way easier than what we all think at the beginning :-P

Link to comment
Share on other sites

Re: Mini Coding Contests

Got the first result. It works BUT do not fully qualify as there is a . and not a space before the stars... now I will accept both, but the one which come with space will be voted better than the one with a . or other... I will let you find out how to display it correctly :-P

BTW there is at least 3 or 4 solutions to do it.

Link to comment
Share on other sites

Re: Mini Coding Contests

 

Got the first result. It works BUT do not fully qualify as there is a . and not a space before the stars... now I will accept both, but the one which come with space will be voted better than the one with a . or other... I will let you find out how to display it correctly :-P

BTW there is at least 3 or 4 solutions to do it.

;P

I could change the . to a space, but some servers seem to make the space disappear when I do it.

also, I could make it more efficient... aye caramba, spose I'll do it later. this is kinda fun ;)

and echo... print... same thing, right? :D

finally... instead of adding spaces or periods, could I just add a <center> command? :)

Link to comment
Share on other sites

Re: Mini Coding Contests

echo and print are not exactly the same... nearly but not exactly... echo is slightly faster.

I will not say what you did as it is one option as nobody see the problem directly, but using space is possible... I will give the answer next week in case.

And yes center would be one of the trick but now you spoiled the fun :-P

So nobody will be allowed to use center anymore :-P

 

I could change the . to a space, but some servers seem to make the space disappear when I do it.

also, I could make it more efficient... aye caramba, spose I'll do it later. this is kinda fun ;)

and echo... print... same thing, right? :D

finally... instead of adding spaces or periods, could I just add a <center> command? :)

Link to comment
Share on other sites

Re: Mini Coding Contests

BTW I just made myself the second assignment, and without cheating and counting the <?PHP and ?> I have 6 lines of code (so no multiple code on a single line like "echo '1';echo '2'" that would be 2 lines not 1)

Now who will discover how I did it?

Hint: that's how my code look like:

1 <?PHP

2 for(...)

3 ....;

4 for(...)

5 ....;

6 ?>

And I'm not using center nor dots!

Link to comment
Share on other sites

Re: Mini Coding Contests

And the winners are:

Seanybob for the diamond shaped text

Deception for the link parser

Congratz to both!

So my solution for the diamond shaped text:

change the width value with a even number and your diamond will change.

As you can see you can avoid double loops ;-)

 

<PRE>
<?php
$width=12;
for($i=0;$i < ($width/2);$i++)
echo str_repeat(" ",($width/2)-$i).str_repeat("*",$i*2)."*\n";
for($i=($width/2)-2;$i >= 0;$i--)
echo str_repeat(" ",($width/2)-$i).str_repeat("*",$i*2)."*\n";
?>
</PRE>

 

and why not try to do all with one loop? Well that would be my solution then:

 

<PRE>
<?php
$width=12;
$half=array();
for($i=0;$i < ($width/2);$i++)
$half[]=str_repeat(" ",($width/2)-$i).str_repeat("*",$i*2)."*\n";
echo implode("",$half);
array_pop($half);
echo implode("",array_reverse($half));
?>
</PRE>

 

This second code is a bit longer but maybe executed a bit faster as we don't have to iterate in a second loop. The iteration is done with native PHP functions and therefore could be a bit faster.

BTW if you want to remove the <PRE></PRE> tag you could use the &nbps; tag in order to add spaces.

Link to comment
Share on other sites

Re: Mini Coding Contests

Interesting truth stated earlier about echo being faster than print. From what I read various places around 20% on some benchmarks comparing the two. Anyway thats good ammo to know, something simple over all can make a small but significant difference. Alot of people should remember that lil fact.

Link to comment
Share on other sites

  • 3 weeks later...

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.   Paste as plain text instead

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