Pyramid in perl

Lets say you want to print something like this:
                          A
                         ABA
                        ABCBA
                       ABCDCBA
                      ABCDEDCBA
                     ABCDEFEDCBA
                   ABCDEFGFEDCBA
                  ABCDEFGHGFEDCBA
                 ABCDEFGHIHGFEDCBA
                ABCDEFGHIJIHGFEDCBA
               ABCDEFGHIJKJIHGFEDCBA
              ABCDEFGHIJKLKJIHGFEDCBA
             ABCDEFGHIJKLMLKJIHGFEDCBA
            ABCDEFGHIJKLMNMLKJIHGFEDCBA
           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
         ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
        ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
       ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
      ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
     ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
    ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
   ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA

Here is the code I wrote:

# !/usr/bin/perl
@array = (‘A’ .. ‘Z’);
$in = <>;
chomp($in);
if($in < 0)
{
print “Can’t proceed”;
}
else{
for($i = 0 ; $i < $in ; $i++)
{
@tmp = (”);
for($k = scalar(@array); $k > $i; $k–)
{
print ” “;
}
for($j = 0 ; $j < $i; $j++)
{
push(@tmp,@array[$j]);
print “@array[$j]”;
}
pop(@tmp);
@revtmp = reverse(@tmp);
foreach(@revtmp)
{ print “$_” ;}
print “\n”;
}
}

This is a rough attempt at the problem. It can be:
1. Checked for input validity

2. Enhanced with what ever you want to ice on top of it.

Have fun!

Advertisements

3 Responses to “Pyramid in perl”

  1. Bucciarati Says:

    Your piramid is falling! Hold it! Someone think of the children!

  2. vinaychilakamarri Says:

    Yeah I tried to format it for wordpress but apparently the nbsp didn’t jell well with the format of pyramid. But the console showed it kind of OK. Did you mean falling in other sense?

  3. […] defeats the purpose of homework. Google also returns a myrid of results for these questions (like this one), so I'm assuming you haven't googled for this […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: