Welcome! Log In Create A New Profile

Advanced

A Fun Challenge - by Shellshock

Posted by shellshock 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
A Fun Challenge - by Shellshock
July 16, 2010 03:45PM
Hi All, after completing Lessons 1,2 and 3. I did some playing around with loops and arrays etc and Have come up with a fun Challenge - if you up for it!?

The challenge is to print a Diamond to screen - any size

#
##
###
####
###
##
#

you can use hashes or any character you wish.
The trick is to do it programatically with php - so none of this echo "#" then br and echo "##" br echo "###" etc etc
You will need to use loops - And you can do it without using functions.

The Winner is the person who can do it with the least amount of code.

A tip : work on half your diamond first - then the other half.

I look forward to seeing what you guys come up with

--Nicholas Karlsen
Re: A Fun Challenge - by Shellshock
July 16, 2010 03:47PM
Seems like only half my Diamond has come out in my post above !
Buy you all know what a diamond looks like.
avatar Re: A Fun Challenge - by Shellshock
July 25, 2010 02:21PM
Sup shellshock,

jeesh dud3 your ch@lleng3 w@z tough!

It took m3 thr33 dayz to work it out. Not three full days though, but it was tough non the less.

Here's the cod3:

<?php
$n = 0;
$z = 1;

//this loop is to make the script write 3 lines
for($i=1; $i<=3; $i++)
{
//this loop is used to write the actual "#"
for ($b=$i-$n; $b<=$i+$n; $b++)
{
//this loop is to space the "#" into a diamonnd.
//i didn't know what to used for the spacing. I tried " " but the spacing wouldn't show.

for ($c=$i+$z; $c>=$i; $c--)
{
echo "|||";
}
echo "#";
$z--;
}
echo "<br/>";
$n++;
}

$n = 2;
$z = 0;

//this loop is to make the script to write 2 lines
for($i=3; $i<=4; $i++)
{
////this loop is used to write the actual "#"
for ($b=$i; $b>=$i-$n ; $b--)
{
//this loop is to space the "#" into a diamonnd.
//i didn't know what to used for the spacing. I tried " " but the spacing wouldn't show.

for ($c=$i; $c>=$i+$z; $c--)
{
echo "|||";
}
echo "#";
$z = $z + 1;
}
echo "<br/>";
$n = $n - 2;
$z = -1;
}

/* writen by theSinthesizer 2010|07|25 */
?>


Shot for the challenge shellshock! smile

PS: guyz please keep submitting challenge ideaz; it's good practice for everyone.
PPS: i think also if someone can come up with an idea for a project that we can all work on colleboratively - open source stuff. thumbs up

>>>
The One that owns The Technology rulez the world!
avatar Re: A Fun Challenge - by Shellshock
July 25, 2010 02:28PM
You should try coding while listening to drum n bass or dubstep. Mad trip! cool smiley

>>>
The One that owns The Technology rulez the world!
Re: A Fun Challenge - by Shellshock
July 25, 2010 06:45PM
<?PHP
echo "#<br/>##<br/>###<br/>####<br/>###<br/>##<br/>#<br/>"
?>
thumbs up smiley
avatar
Mac
Re: A Fun Challenge - by Shellshock
July 26, 2010 07:50AM
Hey guys - paste code here using the tags generated by formatted code button here above
Language: PHP
<?PHP echo "#<br/>##<br/>###<br/>####<br/>###<br/>##<br/>#<br/>" ?>
avatar Re: A Fun Challenge - by Shellshock
July 26, 2010 10:33AM
Hi Mac,

can you please rephrase your post because i don't get what u mean? smile

>>>
The One that owns The Technology rulez the world!
avatar Re: A Fun Challenge - by Shellshock
July 26, 2010 10:50AM
Yo xmgcoyi,

ur code is not quite a diamond dude. smile

"Be true to the game, because the game will be true to you. If you try to shortcut the game, then the game will shortcut you." - Michael Jordan drinking smiley

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: A Fun Challenge - by Shellshock
July 27, 2010 07:28AM
When you post PHP code to this forum, then use the 4th button from the right on the submission form menu to generate two tags
Language: PHP
[code="php"] and then you paste or type your code here between these tags esnuring that your code is displayed correctly [ /code]
Re: A Fun Challenge - by Shellshock
July 30, 2010 01:19PM
Hey theSinthesizer
Glad you enjoyed the Challenge.
Good Going on your solution.

Im including my spin on the solving this challenge - let me know what you think : -)


Language: PHP
<?php $character="#"; $startNum="1"; $endNum="20"; for ($l=$startNum ; $l <= $endNum ; $l++ ){ for ($j=$l ; $j <= $endNum ; $j++){ print "&nbsp"; } for ($i=$l ; $i >= $startNum ; $i-- ){ print "$character"; } print "<br>"; } for ($l=$endNum ; $l >= $startNum ; $l-- ){ for ($i=$l ; $i <= $endNum ; $i++ ){ print "&nbsp"; } for ($j=$l ; $j >= $startNum ; $j--){ print "$character"; } print "<br>"; } ?>

Im not sure if this can be done any more efficiently ??
Lets see.
Re: A Fun Challenge - by Shellshock
July 30, 2010 01:30PM
Hey theSinthesizer
By the way - excellent job on the comments for your code.
Made it much easier to read than mine. :-\
Re: A Fun Challenge - by Shellshock
September 06, 2010 07:29AM
I think shellshock has the best solution, i cant come up with anything better.

How about a new challenge? Any ideas Mac?
avatar
Mac
Re: A Fun Challenge - by Shellshock
September 06, 2010 12:36PM
Write code that will predict the lotto winning numbers?
Re: A Fun Challenge - by Shellshock
September 07, 2010 07:11AM
haha, if I could figure that out id have to test the program first! winking smiley
Re: A Fun Challenge - by Shellshock
September 15, 2010 05:01PM
Here you go Mac:

<?php
$lotto = array();
for ($count=0; $count != 6; $count++) {
$numbers = rand(1,50)." ";
if(!in_array($numbers,$lotto)) {
$lotto[] = $numbers;
echo $numbers ;
}
}
?>
avatar
Mac
Re: A Fun Challenge - by Shellshock
September 16, 2010 08:14AM
Hey, I do not need random numbers - I require WINNING numbers smiling smiley
Re: A Fun Challenge - by Shellshock
September 16, 2010 12:01PM
Lol i though about that to, I can try and code luck into it but that might take a while smile
Re: A Fun Challenge - by Shellshock
October 05, 2010 02:10PM
Something I have noticed with php is that you do not have to keep repeating echo "" statements.

you can do an

echo "
Many lines of code here
"; and the end your echo as normal.


There is also something called apples and oranges with php.

Apples and oranges basically refers to starting with an html echo or print and then needing to drop out of html, inject some php and drop back into html, by concatenating php to the print or echo without the breaking the flow.

What do I mean?
Well here is an example.

Language: PHP
echo ("My Name is ". $name ." and I am " .$age . "years old");

Another nifty tip with php is using phpinfo

Try this code

Language: PHP
<?php phpinfo (); ?>

phpinfo prints php server and config specific information about your php installation.

Last tip is the following

Language: PHP
print ($_SERVER[';SERVER_NAME';]."<br>"); print ($_SERVER[';HTTP_USER_AGENT';]."<br>"); print ($_SERVER[';REMOTE_ADDR';]."<br>");

Info retrieved from phpinfo - usefull / powerfull stuff as i am sure you can imagine.
avatar
Mac
Re: A Fun Challenge - by Shellshock
October 06, 2010 07:15AM
There is no need to concatenate - you can write

Language: PHP
echo "My Name is $name and I am $age years old";

PHP scans and parses the contents within double quotation marks for variables or special characters. When using single quotation marks it doesn't parse for variables or special characters, so there may be a slight increase in speed if you do this (or say they say).

Language: PHP
echo ';My Name is ';. $name .'; and I am '; .$age . ';years old';;

No why do I then say there is no need? Well, the speed benefit you derive from using the latter is hardly noticeable judged against the time it will take you to write concatenated code. This does not mean that you should not concatenate, but is is more a matter of preference than anything else. If you are unsure, then do a test page, one concatenated and one not. But it needs to be a long page.

And btw, this is breaking out of HTML smiling smiley

Language: PHP
<body><p><?php echo $name; ?> said that he is tired</p></body>
Re: A Fun Challenge - by Shellshock
October 08, 2010 09:42AM
Also a good tool to use is <pre> </pre> tags it sort off work like print but it outputs your php scripts in easy to read html
Sorry, only registered users may post in this forum.

Click here to login