Re: asterisk pattern
by GrandFather (Saint) on Oct 27, 2011 at 06:54 UTC
|
print <<PATTERN;
*
**
***
**
*
PATTERN
True laziness is hard work
| [reply] [d/l] |
|
But he wants it in a loop! So, I propose:
do {1;} until print <<"*";
*
**
***
**
*
*
| [reply] [d/l] |
Re: asterisk pattern
by moritz (Cardinal) on Oct 27, 2011 at 07:48 UTC
|
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;
| [reply] [d/l] |
Re: asterisk pattern
by davido (Cardinal) on Oct 27, 2011 at 07: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.)
| [reply] [d/l] |
|
| [reply] |
Re: asterisk pattern
by Corion (Patriarch) on Oct 27, 2011 at 07:35 UTC
|
$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
}
| [reply] [d/l] [select] |
Re: asterisk pattern
by davido (Cardinal) on Oct 27, 2011 at 06:54 UTC
|
perl -e 'print qq/ *\n **\n***\n **\n *\n/;'
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: asterisk pattern
by Ratazong (Monsignor) on Oct 27, 2011 at 08: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/ /;
}
| [reply] [d/l] |
Re: asterisk pattern
by choroba (Cardinal) on Oct 27, 2011 at 07:06 UTC
|
say' 'x(3-$_),'*'x$_ for 1..3,2,1 | [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: asterisk pattern
by BrowserUk (Patriarch) on Oct 27, 2011 at 07: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.
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: asterisk pattern
by GrandFather (Saint) on Oct 27, 2011 at 07:03 UTC
|
print ' ' x (abs(2 - $_)), '*' x (3 - abs(2 - $_)), "\n" for 0 .. 4;
True laziness is hard work
| [reply] [d/l] |
Re: asterisk pattern
by choroba (Cardinal) on Oct 27, 2011 at 07: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.) | [reply] [d/l] [select] |