Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

WDF Coding challenge #1

Featured Replies

Thought I'd stick my php-only version up here as well - pretty much the same but with a slightly different math. I also have never used str_pad(), but str_repeat seems to work fine as an alternative. Perhaps Jock can explain the pros and cons of str_repeat(" ", $i) vs str_pad(" ", $i) ? :)

 

Now who's gonna be first to sift through everyone's code and come up with the perfect amalgamation?

 

<form method="post" action=""><input type="text" maxlength="1" name="letter" /><input type="submit" name="submit" value="Go" /></form>
<pre>
<?php
//this line just for the sake of the form
If(isset($_POST['letter']) && preg_match('/^[a-zA-Z]/', $_POST['letter'])){$post=strtoupper($_POST['letter']);}else{$post='Z';}

$i = range('A',$post); $count=count($i);
foreach(array_slice($i,1) as $key => $letter){  
$rockOn .= str_repeat(" ", $count-2).$letter.str_repeat(" ",($key)*2+1).$letter."\n";  
$count = $count-1;
}
echo str_repeat(" ",($key+1))."A"."\n".$rockOn;

if(count($i)>2){
$i = range('B',$post);	$count=count($i)-1;
foreach(array_slice(array_reverse($i),1) as $key => $letter){
$count = $count-1;
$Boogie .= str_repeat(" ",($key)+1).$letter.str_repeat(" ", $count*2+1).$letter."\n";
}
echo $Boogie.str_repeat(" ",($key)+2)."A";
}
elseif(count($i)>1){echo $Boogie.str_repeat(" ",($key)+1)."A";}
?>
</pre>

 

By the way Jock...the solution you posted up doesn't seem to work. It outputs the top of the diamond with a letter 'A' underneath.

  • Author

Sorry I edited the code when i was posting it, the correct code is

 

<?php
function kite($largest='Z') {
   echo str_pad('', (ord($largest)-64)) , "A\n";
   for ($i = 1, $l = $ub = (ord($largest)-65); $i < ($ub+1); $i++, $l--)
       echo str_pad('', $l), chr($i+65), str_pad('', ($i*2)-1), chr($i+65), PHP_EOL;     
   for ($i = ord($largest)-64, $x = 2; $i > 2; $i--, $x++)
        echo str_pad('', $x), chr(63+$i), str_pad('', ($i*2)-5), chr(63+$i), PHP_EOL;
   echo str_pad('', ($ub+1)) , "A\n";
}
echo '<pre>' , PHP_EOL ,  kite('Z') , PHP_EOL , '</pre>';

  • Author

Sorry about the CSS thing guys, should have mentioned it in the rules! I didnt think about it because the challenge is an oldskool one from BASIC where it would be in a terminal.

 

str_pad is for padding out a string to a certain length (you can pad to the left, right and middle), str_repeat is for repeating strings a certain number of times. So in this challenge its pretty irrelevant as its spaces we're using. str_pad is fractionally more efficient for our challenge, I guess internally str_repeat must loop and I'm guessing that str_pad calls something like printf("|%-10s|", "String...");

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.