Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

asterisk pattern

by Monk_perl
on Oct 27, 2011 at 10:49 UTC ( #934101=perlquestion: print w/ replies, xml ) Need Help??
Monk_perl has asked for the wisdom of the Perl Monks concerning the following question:

How to print the following pattern:

* ** *** ** *

Comment on asterisk pattern
Download Code
Re: asterisk pattern
by GrandFather (Cardinal) on Oct 27, 2011 at 10:54 UTC
    print <<PATTERN; * ** *** ** * PATTERN
    True laziness is hard work
      But he wants it in a loop! So, I propose:
      do {1;} until print <<"*"; * ** *** ** * *
Re: asterisk pattern
by davido (Archbishop) on Oct 27, 2011 at 10:54 UTC

    perl -e 'print qq/  *\n **\n***\n **\n  *\n/;'

    Dave

      I already know that
        and its not a mirror image
Re: asterisk pattern
by GrandFather (Cardinal) on Oct 27, 2011 at 11:03 UTC

    Oh! I see from the CB that you want to use a for loop:

    print ' ' x (abs(2 - $_)), '*' x (3 - abs(2 - $_)), "\n" for 0 .. 4;
    True laziness is hard work
Re: asterisk pattern
by choroba (Abbot) on Oct 27, 2011 at 11:06 UTC
    say' 'x(3-$_),'*'x$_ for 1..3,2,1
      I think this code is in perl
        I think this site is about Perl! I do not see anything about C in your spec here...
        I think this code is in perl

        No problem. If you don't mind falling ill, try MUMPS:

        F I=1,2,3,2,1 W $E("  ***",I,I+2),!

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: asterisk pattern
by davido (Archbishop) on Oct 27, 2011 at 11:12 UTC

    Would your teacher accept this?

    perl -E 'say for ("  *", " **", "***" )[0,1,2,1,0]'

    (Probably not, but it might be worth the 'F' just to see the look on your teacher's face.)


    Dave

      but it might be worth the 'F' just to see the look on your teacher's face

      Obligatory link: Niels Bohr and the barometer (not true, unfortunately.)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: asterisk pattern
by choroba (Abbot) on Oct 27, 2011 at 11:15 UTC
    No explicit loop, just recursion and repetition:
    $p=1;sub p{my$x=shift;$x||exit;say" "x(3-$x),"*"x$x;$p=-1if$x>2;p($x+$ +p)}p 1
    Update: Not so funny in C:
    #include<stdio.h> int p,n;void q(int x){int j;if(!x)return;for(j=1;j<=n-x;j++)printf(" " +);for(j=1;j<=x;j++)printf("*");printf("\n");if(x>=n)p=-1;q(x+p);}void + main(){printf("Enter the number of columns: ");scanf("%d",&n);p=1;q( +1);}
    (I've never really used C, anyway.)
Re: asterisk pattern
by BrowserUk (Pope) on Oct 27, 2011 at 11:23 UTC

    sub la{ my $n = shift; print ' ' x ( $n - $_ ), '*' x $_ for 1 .. $n, reverse 0 .. $n - 1; };; la(3);; * ** *** ** * la(6);; * ** *** **** ***** ****** ***** **** *** ** *

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      #include<stdio.h> #include<conio.h> main() { int n,r=1,val,j; clrscr(); printf("enter the number of rows in the pyramid\t"); scanf("%d",&n); while(r<=n) { val=r; for(j=1;j<=n-r;j++) printf("\t"); for(j=1;j<=2*r-1;j++) if(j<=(2*r-1)/2) printf("%d\t",val++); else if(j==(2*r-1)/2+1) printf("%d\t",val); else printf("%d\t",--val); printf("\n"); r++; } getch(); }

        This site is about Perl programming, not C programming unless in close relation to Perl. If you want to translate your code to Perl, you will find that the code is a valid Perl program with very little modifications.

        From Wikipedia

        conio.h is a C header file used in old MS-DOS compilers to create text user interfaces. It is not described in The C Programming Language book, and it is not part of the C standard library, ISO C nor is it required by POSIX.

        Not only is your code not Perl, it is even totally obsolete C, written for MS-DOS.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        Here's the how to embed perl in your C program tutorial. perlembed (update: initially pulled wrong tut)
Re: asterisk pattern
by Corion (Pope) on Oct 27, 2011 at 11:35 UTC

    The following works for me:

    $b=this_code_is_from_perlmonks_org; for my $a(some_program_was_written_for_perlmonks_org=~/[st]/g){ print $a,sprintf '%4s','*'x(2+($a cmp $b)), qq(\n); $b=$a }

    Update: I didn't get the requirement of outputting different depths, so here is an improved variation on the same theme:

    $level=12; my $i=12; my $l=$level-12; $_ = #$; this_program_source_was_written_for_the_site_perlmonks_org. please_do_not_give_this_code_to_your_teacher_without_understanding. We_value_learning_but_the_work_has_to_be_done_by_yourself. kthxbai; $b=a_program_written_by_Corion; for $a(($_.reverse)=~/[sx]/g){ $d=($a cmp $b)||$d; $l+=$d; $b=$a; print sprintf '%'.$i.'s', '*'x$l, qq(\n) unless $l<0 }
Re: asterisk pattern
by moritz (Cardinal) on Oct 27, 2011 at 11:48 UTC

    I'd go with this solution:

    use 5.014; use warnings; use Mojo::UserAgent; my $text = Mojo::UserAgent ->new ->get('http://www.perlmonks.org/?node_id=934101') ->res ->dom ->at('tt') ->all_text; chomp $text; print $text;
Re: asterisk pattern
by Ratazong (Prior) on Oct 27, 2011 at 12:45 UTC
    So many possibilities! The next one, however requires some human interaction:
    use strict; use warnings; print "Ssssshhh! Please turn your monitor first (otherwise the pyramid + might not point to the correct direction).\n"; my $Ss = '*'; my $ss = 2; my $sS = $Ss x $ss; for (0..$ss) { print "\n". $sS. $Ss. reverse $sS; $sS =~ s/\S/ /; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://934101]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-08-20 23:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (125 votes), past polls