Return an xmas tree n
rows in length.
n
will always be greater than 0.
Examples:
(1) => *
(3) =>
*
***
*****
(5) =>
*
***
*****
*******
*********
*^You can assume that you will always recieved valid input
Return an xmas tree n
rows in length.
n
will always be greater than 0.
Examples:
(1) => *
(3) =>
*
***
*****
(5) =>
*
***
*****
*******
*********
*^You can assume that you will always recieved valid input
Language: PHP
Length: 111
Solution:
function t($n){$s='';for($i=1;$i<=$n;$i++){$s.=str_repeat(' ', $n-$i).str_repeat('*', 2*$i-1)."\n";}return $s;}
Language: C++
Length: 151
Solution:
std::string x(int n){std::string r="";for(int i=1;i<=n;i++){for(int j=1;j<=n-i;j++)r+=" ";for(int j=1;j<=i+(i-1);j++)r+="*";if(i!=n)r+="\n";}return r;}
readable
std::string x(int n){
std::string r="";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)r+=" ";
for(int j=1;j<=i+(i-1);j++)r+="*";
if(i!=n)r+="\n";
}
return r;
}
Language: Golang
Lenght: 149
Solution:
func p(h int){w:=1+2*(h-1);m:=(w+1)/2;for i:=0;i<h;i++{for y:=0;y<=w;y++{if y>=m-i&&y<=m+i{fmt.Printf("*");}else{fmt.Printf(" ")}};fmt.Printf("\n")}}
Language: Python 2.7
Length: 53
Solution:
def f(r):
for i in range(r):
print" "*(r-i)+"*"*(i+i+1)
Just realised that I need to return a value and not print it.
Language: Python 2.7
Length: 68
Solution:
def f(r):
x=""
for i in range(r):
x+=" "*(r-i)+"*"*(i+i+1)+"\n"
return x
Language: Haskell
Length: 65
A little late for Christmas, but still in December. :-)
t n=unlines$map(\h->replicate(n-h)' '++replicate(2*h-1)'*')[1..n]
Language: PHP
Length: 98
Solution:
more readably;