Given a number N, how can I print out a Christmas tree of height N using the least number of code characters? N is assumed constrained to a min val of 3, and a max val of 30 (bounds and error checking are not necessary). N is given as the one and only command line argument to your program or script.
All languages appreciated, if you see a language already implemented and you can make it shorter, edit if possible - comment otherwise and hope someone cleans up the mess. Include newlines and whitespace for clarity, but don't include them in the character count.
A Christmas tree is generated as such, with its "trunk" consisting of only a centered "*"
N = 3:
*
***
*****
*
N = 4:
*
***
*****
*******
*
N = 5:
*
***
*****
*******
*********
*
N defines the height of the branches not including the one line trunk.
Merry Christmas SO!
perl: one line version:
print$"x($a-$_),'*'x($_*2+1),$/for 0..($a=pop)-1,0
and now with more whitesapce:
print $" x ( $a - $_ ), #"# Syntax Highlight Hacking Comment
'*' x ( $_ * 2 + 1),
$/
for 0 .. ( $a = pop ) - 1, 0;
$ perl tree.pl 3
*
***
*****
*
$ perl tree.pl 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*
$
# print $Default_List_Seperator ( a space )
# repeated ( $a - $currentloopiterationvalue ) times,
print $" x ( $a - $_ ),
#"# print '*' repeated( $currentloopiteration * 2 + 1 ) times.
'*' x ( $_ * 2 + 1),
# print $Default_input_record_seperator ( a newline )
$/
# repeat the above code, in a loop,
# iterating values 0 to ( n - 1) , and then doing 0 again
for 0 .. ( $a = pop ) - 1, 0;
# prior to loop iteration, set n to the first item popped off the default list,
# which in this context is the parameters passed on the command line.
Language: Brainfuck, Char count: 240
Not yet done. It works, but only with single-digit numbers.
EDIT: Done! Works for interpreters using 0 as EOF. See NOTE
s in commented source for those with -1.
EDIT again: I should note that because Brainfuck lacks a standard method for reading command line arguments, I used stdin (standard input) instead. ASCII, of course.
EDIT a third time: Oh dear, it seems I stripped .
(output) characters when condensing the code. Fixed...
Here's the basic memory management of the main loop. I'm sure it can be heavily optimized to reduce the character count by 30 or so.
Condensed version:
,>++++++++[-<------>],[>++++++++[-<------>]<<[->++++++++++<]>>]<[->+>+>>>>>>>+<<<<<<<<<]>>>>++++++++[-<++++>]>++++++[-<+++++++>]+>>>++[-<+++++>]<<<<<<[-[>.<-]<[-<+>>+<]<[->+<]>>>>>[-<.>>+<]>[-<+>]>.<<++<<<-<->]>>>>>>>-[-<<<<<<.>>>>>>]<<<<<.
And the pretty version:
ASCII to number
,>
++++++++[-<------>] = 48 ('0')
Second digit (may be NULL)
,
NOTE: Add plus sign here if your interpreter uses negative one for EOF
[ NOTE: Then add minus sign here
>++++++++[-<------>]
<<[->++++++++++<]>> Add first digit by tens
]
Duplicate number
<[->+>+>>>>>>>+<<<<<<<<<]>>
Space char
>>++++++++[-<++++>]
Asterisk char
>++++++[-<+++++++>]
Star count
+
New line char
>>>++[-<+++++>]<<<
<<<
Main loop
[
Print leading spaces
-[>.<-]
Undo delete
<[-<+>>+<]
<[->+<]
>>
Print stars
>>>[-<.>>+<]
Add stars and print new line
>[-<+>]
>.<
<++
<<<
-<->
End main loop
]
Print the trunk
>>>>>>>
-[-<<<<<<.>>>>>>]
<<<<<.
Merry Christmas =)
(,{.)(}:@|."1,.])[\'*'$~
(,{.)(}:@|."1,.])[\'*'$~5
*
***
*****
*******
*********
*
Explanation:
'*'$~5
*****
[\'*'$~5
*
**
***
****
*****
Then }:@|."1
reverses each row and strips off the last column, and ,.
staples it to ]
.
Then ,{.
pastes the first column onto the bottom.
Previous entries:
29 characters, no spaces at all.
((\:i.@#),}.)"1$&'*'"0>:0,~i.3 * *** ***** * ((\:i.@#),}.)"1$&'*'"0>:0,~i.11 * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* *
NB. count from 1 to n, then 1 again >:0,~i.3 1 2 3 1 NB. replicate '*' x times each $&'*'"0>:0,~i.3 * ** *** * NB. reverse each row (\:i.@#)"1$&'*'"0>:0,~i.3 * ** *** * NB. strip off leading column }."1$&'*'"0>:0,~i.3 * ** NB. paste together ((\:i.@#),}.)"1$&'*'"0>:0,~i.3 * *** ***** *[1] http://www.jsoftware.com/
c=:[:((\:i.@#),}.)"1[:$&'*'"0[:>:0,~i.
- ephemient
python -c "
n=w=$1
s=1
while w:
print' '*w+'*'*s
s+=2
w-=1
print' '*n+'*'"
$ sh ax6 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*
Language: Windows Batch Script (shocking!)
@echo off
echo Enable delayed environment variable expansion with CMD.EXE /V
rem Branches
for /l %%k in (1,1,%1) do (
set /a A=%1 - %%k
set /a B=2 * %%k - 1
set AA=
for /l %%i in (1,1,!A!) do set "AA=!AA! "
set BB=
for /l %%i in (1,1,!B!) do set BB=*!BB!
echo !AA!!BB!
)
rem Trunk
set /a A=%1 - 1
set AA=
for /l %%i in (1,1,!A!) do set "AA=!AA! "
echo !AA!*
setlocal enabledelayedexpansion
command. - Helen
n=ARGV[0].to_i
((1..n).to_a+[1]).each{|i|puts' '*(n-i)+'*'*(2*i-1)}
n=$*[0].to_i
((1..n).to_a<<1).each{|i|puts' '*(n-i)+'*'*(2*i-1)}
Merry Christmas all!
Edit: Improvements added as suggested by Joshua Swink
n=$*[0].to_i;puts [*1..n,1].map{|i|" "*(n-i)+"*"*(2*i-1)}
brings it down to 58. - manveru
Language: x86 asm 16-bit, Byte count: 50
No assembly version yet? :)
bits 16
org 100h
mov si, 82h
lodsb
aaa
mov cx, ax
mov dx, 1
push cx
mov al, 20h
int 29h
loop $-2
push dx
mov al, 2ah
int 29h
dec dx
jnz $-3
pop dx
mov al, 0ah
int 29h
inc dx
inc dx
pop cx
loop $-23
shr dx, 1
xchg cx, dx
mov al, 20h
int 29h
loop $-2
mov al, 2ah
int 29h
ret
(Note: N is limited to 1 - 9 in this version)
G:\>tree 9
*
***
*****
*******
*********
***********
*************
***************
*****************
*
Download here [1]
[1] http://gulle.se/tree.zipstatic void Main(string[] a)
{
int h = int.Parse(a[0]);
for (int n = 1; n < h + 2; n++)
Console.WriteLine(n <= h ?
new String('*', n * 2 - 1).PadLeft(h + n) :
"*".PadLeft(h + 1));
}
}
Just the code, without formatting (120 characters):
int h=int.Parse(a[0]);for(int n=1;n<h+2;n++)Console.WriteLine(n<=h?new String('*',n*2-1).PadLeft(h+n):"*".PadLeft(h+1));
Version with 109 characters (just the code):
for(int i=1,n=int.Parse(a[0]);i<n+2;i++)Console.WriteLine(new String('*',(i*2-1)%(n*2)).PadLeft((n+(i-1)%n)));
Result for height = 10:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*
A little bit shorter dc version:
dc -e '?d1rdsv[d32r[[rdPr1-d0<a]dsaxszsz]dsbx1-rd42rlbx2+r10Plv1-dsv0<c]dscxszsz32rlbx[*]p' <<<$1
EDIT: changed constant 10 into $1
python, "-c" trick... @61 chars (and one line)
python -c"for i in range($1)+[0]:print' '*($1-i)+'*'*(2*i+1)"
Here's a reasonably space-efficient Haskell version, at 107 characters:
main=interact$(\g->unlines$map(\a->replicate(g-a)' '++replicate(a*2-1)'*')$[1..g]++[1]).(read::[Char]->Int)
running it:
$ echo 6 | runhaskell tree.hs
*
***
*****
*******
*********
***********
*
Merry Christmas, all :)
Just for the obscurity of it :)
dc -e "$1dsnsm"'
[[ ]n]ss
[[*]n]st
[[
]n]sl
[s2s1[l2xl11-ds10<T]dsTx]sR
[lndlslRxlcdltlRxllx2+sc1-dsn0<M]sM
1sclMxlmlslRxltxllx
'
$ sh ax3 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
int n = int.Parse(args[0]);
int i=0;
Console.Write("{0}\n{1}", string.Join("\n",
new int[n].Select(r => new string('*',i * 2 + 1)
.PadLeft(n+i++)).ToArray()),"*".PadLeft(n));
}
}
170 charcters.
int n=int.Parse(a[0]);int i=0;Console.Write("{0}\n{1}",string.Join("\n",Enumerable.Repeat(0,n).Select(r=>new string('*',i*2+1).PadLeft(n+i++)).ToArray()),"*".PadLeft(n));
class T{ /* 219 characters */
public static void main(String[] v){
int n=new Integer(v[0]);
String o="";
for(int r=1;r<=n;++r){
for(int s=n-r;s-->0;)o+=' ';
for(int s=1;s<2*r;++s)o+='*';
o+="%n";}
while(n-->1)o+=' ';
System.out.printf(o+"*%n");}}
For reference, I was able to shave the previous Java solution, using recursion, down to 231 chars, from the previous minimum of 269. Though a little longer, I do like this solution because T
is truly object-oriented. You could create a little forest of randomly-sized T
instances. Here is the latest evolution on that tack:
class T{ /* 231 characters */
public static void main(String[] v){new T(new Integer(v[0]));}}
String o="";
T(int n){
for(int r=1;r<=n;++r){
x(' ',n-r);x('*',2*r-1);o+="%n";}
x(' ',n-1);
System.out.printf(o+"*%n");
}
void x(char c,int x){if(x>0){o+=c;x(c,x-1);}
}
Language: python, no tricks, 78 chars
import sys
n=int(sys.argv[1])
for i in range(n)+[0]:print' '*(n-i)+'*'*(2*i+1)
Improving ΤΖΩΤΖΙΟΥ's answer. I can't comment, so here is a new post. 72 characters.
import sys
n=int(sys.argv[1])
for i in range(n)+[0]:
print ("*"*(2*i+1)).center(2*n)
Using the "python -c" trick, 61 characters.
python -c "
for i in range($1)+[0]:
print ('*'*(2*i+1)).center(2*$1)
"
I learned the center function and that "python -c" can accept more than one line code. Thanks, ΤΖΩΤΖΙΟΥ.
Better C++, around 210 chars:
#include <iostream>
using namespace std;
ostream& ChristmasTree(ostream& os, int height) {
for (int i = 1; i <= height; ++i) {
os << string(height-i, ' ') << string(2*i-1, '*') << endl;
}
os << string(height-1, ' ') << '*' << endl;
return os;
}
Minimized to 179:
#include <iostream>
using namespace std;ostream& xmas(ostream&o,int h){for(int i=1;i<=h;++i){o<<string(h-i,' ')<<string(2*i-1,'*')<<endl;}o<<string(h-1,' ')<<'*'<<endl;return o;}
Improvement of the C-version.
char s[61];
l(a,b){printf("% *.*s\n",a,b,s);}
main(int i,char**a){
int n=atoi(a[1]);memset(s,42,61);
for(i=0;i<n;i++)l(i+n,i*2+1);l(n,1);
}
Works and even takes the tree height as an argument. Needs a compiler that tolerates K&R-style code.
I feel so dirty now.. This is code is ugly.
Groovy 62B
n=args[0]as Long;[*n..1,n].any{println' '*it+'*'*(n-~n-it*2)}
_
n = args[0] as Long
[*n..1, n].any{ println ' '*it + '*'*(n - ~n - it*2) }
Since this is a CW: I don't like that code golfs are always organized in terms of "number of characters" or somesuch. Couldn't they be organized in terms of number of instructions for the compiler/interpreter (or some similar criterion)? Here is the Ruby solution again [1], and it's basically the same, but now for human consumption too:
SPACE = " "
ASTERISK = "*"
height_of_tree=ARGV[0].to_i
tree_lines = (1..height_of_tree).to_a
tree_lines.push 1 # trunk
tree_lines.each do | line |
spaces_before = SPACE*(height_of_tree-line)
asterisks = ASTERISK*(2*line-1)
puts spaces_before + asterisks
end
[1] http://stackoverflow.com/questions/392788/code-golf-christmas-edition-how-to-print-out-a-christmas-tree-of-height-n/392886#392886C# - Recursion
using System;
class A
{
static string f(int n, int r)
{
return "\n".PadLeft(2 * r, '*').PadLeft(n + r)
+ (r < n ? f(n, ++r) : "*".PadLeft(n));
}
static void Main(string[] a)
{
Console.WriteLine(f(int.Parse(a[0]), 1));
}
}
177 chars (not as short the other C# method posted, but a different way of doing it).
AWK, 86 characters on one line.
awk '{s="#";for(i=0;i<$1;i++){printf"%"$1-i"s%s\n","",s;s=s"##"}printf"%"$1"s#\n",""}'
echo "8" | awk '{s="#";for(i=0;i<$1;i++){printf"%"$1-i"s%s\n","",s;s=s"##"}printf"%"$1"s#\n",""}'
#
###
#####
#######
#########
###########
#############
###############
#
cat tree.txt
3
5
awk '{s="#";for(i=0;i<$1;i++){printf"%"$1-i"s%s\n","",s;s=s"##"}printf"%"$1"s#\n",""}' tree.txt
#
###
#####
#
#
###
#####
#######
#########
#
Shell version, 134 characters:
#!/bin/sh
declare -i n=$1
s="*"
for (( i=0; i<$n; i++ )); do
printf "%$(($n+$i))s\n" "$s"
s+="**"
done
printf "%$(($n))s\n" "*"
#include <stdio.h>
#define P(x,y,z) for(x=0;x++<y-1;)printf(z);
main(int c,char **v){int i,j,n=atoi(v[1]);for(i=0;i<n;i++){P(j,n-i," ")P(j,2*i+2,"*")printf("\n");}P(i,n," ")printf("*\n");}
Ridiculously long C++ one:
#include <iostream>
#include <conio.h>
using namespace std;
class CTree
{
// Members
private:
unsigned short m_Lvl;
unsigned short m_Spc;
// Construction
public:
CTree(unsigned short lvl)
: m_Lvl(lvl), m_Spc(0) {}
private:
CTree(unsigned short lvl, unsigned short spc)
: m_Lvl(lvl), m_Spc(spc) {}
// Operations
public:
void Print()
{
// Print the leaves.
if (m_Lvl)
{
CTree(m_Lvl-1 , m_Spc+1).Print();
Line();
}
// Print the trunk.
if (!m_Spc)
CTree(1 , m_Lvl ? (m_Lvl - 1) : 0).Line();
}
private:
void Line()
{
// Indent the current line.
for (unsigned short s = m_Spc; s--; )
cout << ' ';
// Print the leaves.
for (unsigned short l = m_Lvl; --l; )
cout << '*' << '*';
cout << '*' << endl;
}
};
int main(int argc, char** argv)
{
// Validate the command line.
if ((argc != 2) || !isdigit(argv[1][0]))
{
cout << "Command line: tree.exe number_of_lines";
_getch();
exit(1);
}
// Validate the number of lines.
short lvl = (short)atoi(argv[1]);
if (lvl < 0)
{
cout << "Do you really think " << lvl << " lines can be printed?";
_getch();
exit(1);
}
if (lvl > 40)
{
cout << "Sorry, but a " << lvl << "-line tree is too big to be printed.";
_getch();
exit(1);
}
// Print the tree.
CTree((unsigned short)lvl).Print();
// The user must press a key before the program finishes.
_getch();
return 0;
}
EDIT: I must add that this program doesn't follow the specifications exactly. When the input argument is N, it prints a (N+1)-line tree.
Merry X-Mas to the SO community
EDIT: Debugged the program and corrected some nasty errors.
It's ugly but it works:
import sys
n=int(sys.argv[1])
print"\n".join(" "*(n-r-1)+"*"*(r*2+1)for r in range(n)+[0])
...
$ python tree.py 13
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
*
(The very last char should be a newline.)
<?php $n=$argv[1];for($r='str_repeat';$i<$n;$i++)echo $r(' ',$n-$i).$r('*',$i*2+1)."\n";echo $r(' ',$n).'*' ?>
Readable version:
<?php
$n = $argv[1];
for ($r = 'str_repeat'; $i < $n; $i++)
echo $r(' ', $n - $i) . $r('*' , $i * 2 + 1) . "\n";
echo $r(' ', $n) . '*'
?>
Here is an Erlang version, ~181chars:
-module (x).
-export ([t/1]).
t(N) ->
t(N,0).
t(0,N) ->
io:format("~s~s~n",[string:copies(" ",N),"*"]);
t(H,S) ->
io:format("~s~s~n",[string:copies(" ",H),string:copies("*",(S*2)+1)]),
t(H-1,S+1).
(btw, happy Christmas to everyone!)
C version. Not short, not pretty, but it works.
#include <stdio.h>
void printLevel(int level, int width)
{
int i;
int count = level + (level - 1);
int spaces = width - count;
int lowerBound = spaces / 2;
int upperBound = width - lowerBound;
for (i = 0; i < width; i++) {
if (i >= lowerBound && i < upperBound) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
void makeTree(int level)
{
int i;
int width = level * 2 - 1;
for (i = 1; i <= level; i++) {
printLevel(i, width);
}
printLevel(1, width);
}
int main(int argc, char **argv)
{
int level = atoi(argv[1]);
makeTree(level);
}
Nemerle with Nextem:
type s=string;
module t {
public Main(a : array[s]) : void {
def t = int.Parse(a[0]);
def x(i) { print s(' ',t-i) + s('*',i*2+1) }
$[0..t].Iter(x);
x(0)
}
}
Char count: 128
Edit: Made it take an arg Edit2: Imperative now
My Scala version. I'm glad I have found the * operator for strings (String implicitly promoted to RichString).
def tree(n:Int) {
def vals(n:Int,k:Int) = ((1 to n) map { i => (k - i, (i * 2) - 1) }).toList
for(j <- vals(n,n) ::: vals(1,n))
println(" " * j._1 + "*" * j._2)
}
Here's how I would do it in Python, very straightforward, only 103 characters:
import sys
n=int(sys.argv[1])
for i in range(n): print ('*'*(2*i+1)).center(2*n)
print '*'.center(2*n)
First try in LUA
f=string.rep p=print function t(N) for i=0,N do s=f(" ",N-i) p(s..f("+",2*i-1)..s) end p(f(" ",N-1).."+"..f(" ",N-1)) end t(arg[1])
After selecting all of it scite tells me these are 131 chars
For clarities sake without optimizing away the \n etc:
f=string.rep
p=print
function t(N)
for i=0,N do
s=f(" ",N-i)
p(s..f("+",2*i-1)..s)
end
p(f(" ",N-1).."+"..f(" ",N-1))
end
t(arg[1])
For the python version by ΤΖΩΤΖΙΟΥ I get shown 115 chars... Hrm, some more things to optimize
This one gets n from command line.
Thanks to ΤΖΩΤΖΙΟΥ for the * operator
val n=args(0).toInt*2
(1.until(n,2))foreach{i=>println(" "*(n-i)+"* "*i)}
println (" "*(n-1) + "*")
Another take at python. Note that the question requested for a script, not a function.
import sys
n= int(sys.argv[1])
c= lambda s: s.center(2*n)
print "\n".join(c("*"*(2*i+1)) for i in range(n)); print c("*")
$ py ax 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*
Language: C, Char count: 116
I realized I could improve on my original design:
main(int c,char**v){char l[99],i=0;for(c=atoi(1[v]);i<c;printf("%*s%.*s\n",c,l,i++,l))l[i]=42;printf("%*c\n",c,42);}
Different approach (119 characters):
s[99],w,i=0;p(n){printf("%*.*s\n",w+n,n*2+1,s);}main(int c,char**v){w=atoi(v[1]);for(memset(s,42,99);i<w;p(i++));p(0);}
Old version (123 characters):
main(int c,char**v){char*l=calloc(c=atoi(v[1]),2),i=0;for(;i<c;printf("%*s%.*s\n",c,l,i++,l))l[i]=42;printf("%*c\n",c,42);}
(One byte can be saved by putting char *l=...
in the for
loop. That makes it non-standard, however (though gcc
still accepts it).)
Language: JavaScript, Char count: 110 (2 relevant spaces)
function p(l)
{
o=''
for(c=0; c<=n+l; c++)
o += c < n - l ? ' ' : '*'
print(o)
}
n = parseInt(arguments[0])
for(l = 0; l < n; l++)
p(l)
p(0)
Ran using spidermonkey. $ smjs christmas_tree.js 4
PHP (133 relevant characters):
function xmastree($h) {
for($i=0;$i<$h;++$i)
echo str_repeat(' ',$h-$i-1).str_repeat('*',2*$i+1)."\n";
echo str_repeat(' ',$h-1)."*\n";
}
101 Relevant characters
int main (int c, array a) {
int n=(int)a[1], i,l;
for(;i<=n; l = ++i < n ? i : 0)
write(" " *(n-l) + "*" * (l*2+1) +"\n");
}
[1] http://pike.ida.liu.se/Common Lisp, 117 essential characters:
(defun x (n)
(dotimes (v n)
(format t "~v:@<~v{*~}~>~%"
(1- (* 2 n))
(1+ (* 2 v))
'(())))
(format t "~v:@<*~>~%" (1-(* 2 n)))
Are there any format
gurus out there who know a better way to get repeating arbitrary characters?
Java version. 189 character
class P
{
static String p(int n, String s)
{
return --n < 1 ? s : p(n, s) + s;
}
public static void main(String[] a)
{
for (int N = new Integer(a[0]), i = -1; i++ < N;)
System.out.println(p(N - i % N, " ") + p(i % N * 2 + 1, "*"));
}
}
Rhino Javascript shell [1]: 117 chars minified
t=['*'];
for(i=1;i<arguments[0];++i)
{
s='*'+t[i-1]+'*';
for(j in t)
t[j]=' '+t[j];
t[i]=s;
}
t[i]=t[0];print(t.join('\n'));
minified:
t=['*'];for(i=1;i<arguments[0];++i){s='*'+t[i-1]+'*';for(j in t) t[j]=' '+t[j];t[i]=s;}t[i]=t[0];print(t.join('\n'));
results:
c:\>java -jar C:\appl\Java\rhino1_7R1\js.jar c:/tmp/Xtree.js 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*
[1] http://www.mozilla.org/rhino/shell.htmlLanguage: Erlang, Char count: 151
A little bit shorter Erlang version:
-module(x).
-export([t/1]).
t(N)->[H|_]=T=t(N,1),io:format([T,H]).
t(0,_)->[];t(N,M)->[[d(N,32),d(M,42),10]|t(N-1,M+2)].
d(N,C)->lists:duplicate(N,C).
When should run with command line argument
-module(x).
-export([t/1]).
t([N])->[H|_]=T=t(list_to_integer(N),1),io:format([T,H]),init:stop().
t(0,_)->[];t(N,M)->[[d(N,32),d(M,42),10]|t(N-1,M+2)].
d(N,C)->lists:duplicate(N,C).
Invocation:
$ erl -noshell -noinput -run x t 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*
FreePascal:
program xmastree;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var x,y,h:integer;
{$IFDEF WINDOWS}{$R xmastree.rc}{$ENDIF}
procedure printRow(sp,st:integer);
var i:integer;
begin
for i := 1 to sp do begin
write(' ');
end;
for x := 1 to st do begin
write('*');
end;
for x := 1 to sp do begin
write(' ');
end;
writeln();
end;
begin
val(ParamStr(1),h);
for y := 1 to h do begin
printRow(h-y,(y-1)*2+1);
end;
printRow(h-1,1);
end.
Output for xmastree.exe 9
*
***
*****
*******
*********
***********
*************
***************
*****************
*
Language: FoxPro 2.x for DOS (should work with Clipper too), Char count: 62
para n
for h=1 to n
?spac(n-h)+repl('*',2*h-1)
endf
?spac(n-1)+'*'
Language: Euphoria 147 chars (9 relevant spaces):
include get.e
object
a = command_line(),
t = 42
a = value( a[3] )
a = a[2]
for i = 1 to a do
puts(1, repeat( 32, a - i ) & t & 10)
t &= "**"
end for
puts(1, repeat( 32, a - 1 ) & 42 & 10 )
With only relevant whitespace:
include get.e
object a=command_line(),t=42a=value(a[3])a=a[2]for i=1to a do puts(1,repeat(32,a-i)&t&10)t&="**"end for puts(1,repeat(32,a-1)&42&10)
Language C# in verbosity
using System;
namespace ChristmasTree
{
class Program
{
static void Main( string[] args )
{
var buildATree = new BuildATree( int.Parse( args[0] ) );
Console.WriteLine( buildATree.MakeTree() );
Console.ReadLine();
}
}
}
using System.Text;
namespace ChristmasTree
{
public class BuildATree
{
private int TreeHeight
{
get;
set;
}
public BuildATree()
: this( 3 )
{}
public BuildATree( int treeHeight )
{
this.TreeHeight = treeHeight;
if( treeHeight > 30 )
this.TreeHeight = 30;
}
public string MakeTree()
{
StringBuilder sb = new StringBuilder();
for( int i = 0; i < this.TreeHeight; i++ )
{
sb.AppendLine(
new string( ' ', this.TreeHeight - i - 1 )
+ new string( '*', i * 2 + 1 ) );
}
sb.AppendLine( new string( ' ', this.TreeHeight-1 ) + "*" );
return sb.ToString();
}
}
}
Excessively long version in J (97 chars)
t=:3 :0
d=:0
k=:''
while.d<y
do.
k=:k,((d{((y-1)+>:i.y))$!.'*'((d{((|.i.y)))$,' ')),LF
d=:d+1
end.k,y$k
)
Run it this way:
t N
where N is tree height.
And Merry Xmas (a bit late).
Language: Php, Char count: 110 (3 relevant spaces)
<? function x($n,$a,$t){return $n?str_repeat(' ',$n).$a.x($n-1,"*$a"," $t"):$t;}echo x($argv[1],"\n","*\n");
A bit of php recursion to reduce the count of chars to 110.
in c++, the shortest and the fastest way "i think" :)
void tree(int c, char* o)
{
#define _(p,x) t=p;while(t--)*o++=x
if(o&&c>0)
{
int m=c+1,t;
do
{
_(c,' ');
_((m-c)*2-1,'*');
_(c,' ');
*o++='\n';
}while(--c);
_(m-1,' ');
*o++= '*';
*o++='\n';
*o=0;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char t[1024];
tree(5, t);
printf("%s", t);
}
using System;
using System.Linq;
class Program
{
static void Main(string[] a)
{
int n = int.Parse(a[0]);
Console.WriteLine(Enumerable.Range(1, n).Concat(new int[] { 1 })
.Select(x => new string('*', x * 2 - 1).PadLeft(x + n))
.Aggregate((x, y) => x + "\n" + y));
}
}
158 characters:
int n=int.Parse(a[0]);Console.WriteLine(Enumerable.Range(1,n).Concat(new int[]{1}).Select(x=>new string('*',x*2-1).PadLeft(x+n)).Aggregate((x,y)=>x+"\n"+y));
Lua:
113 significant chars:
s=string.rep;n=tonumber(arg[1]);r=print;for i=0,n do l=s(" ", n-i)..s("*",(n-(n-i))*2+1);r(l)end r(s(" ",n).."*")
~:i,0+{.i\-(' '*\.)+'*'*n}%
~:i # eval the command line arg, store in i
,0+ # create a list [0..i-1] add 0 to the end
{}% # map this block over the list
. # make a copy of the list element
i\- # subtract the list element from i
( # decrease by one more
' '* # multiply the result by ' '
\ # swap, so the list element is back on top of stack
.)+ # duplicate, add one, add the two numbers together
'*'* # multiply the result by '*'
n # put a newline here
C++
#include<iostream>
using namespace std;
int main(int a,char**c)
{
int h = atoi(c[1]);
for(int i=0; i<h; i++)
{
cout << string(h-i, ' ') << string(i*2+1, '*') << endl;
}
cout << string(h, ' ') << '*' << endl;
}
golfed to 188 char
#include<iostream>
using namespace std;
#define s string
int main(int a,char**c){int h=atoi(c[1]);for(int i=0;i<h;i++){cout<<s(h-i,' ')<<s(i*2+1,'*')<<endl;}cout<<s(h,' ')<<'*'<<endl;}
Another GolfScript entry:
~:x,-1%[x(]+{." "*x@- 2*("*"*n.},;
Explanation:
~:x #read input, store into x
, #create array [0,1,2,...,x-1]
-1% #reverse it
[x(]+ #append x-1 to the array
{." "*x@- 2*("*"*n.}, #map each element in array to this block, outputs each line
; #remove results of map from stack
The block does:
{. #duplicate value given by map (stack is now: i i)
" "* #push " ", times it by i (stack now: i " ")
x #push x, stack is: (i " " x), and bring i
@ #bring i to the front (stack is: " " x i)
- 2*( #pop x,i, push ((x-i)*2-1)
"*"* #print that many *'s
n.} #print newline, duplicate it so the map doesn't kill it
function k(a){var b="";for(i=0;i<a;i++){for(j=0;j<a*2;j++)b+=j>a-2-i
&&j<a+i?"*":" ";b+="\n"}for(i=0;i<a*2;i++)b+=i==a-1?"*":" ";return b}
Test case (formatting due to first "
marking a String):
k(3)
" *
***
*****
* "
Fortran 90
A bit modified, so the tree trunk looks more proportional to the height of the tree.
write(*,*) 'how high a tree? '; read(*,*)n
write(*,'(a,a)') (repeat(' ',n-i),repeat('*',2*i-1),i=1,n)
write(*,'(a,a)') (repeat(' ',n-1),repeat('*',1),i=1,n/5+1)
end
n = WScript.Arguments(0)
For i = 1 To n
WScript.Echo Space(n-i+1) & String(2*i-1, "*")
Next
WScript.Echo Space(n) & "*"
Usage and output example:
> cscript christmastree.vbs 7 //nologo
*
***
*****
*******
*********
***********
*************
*
Windows batch files have poor support for string operations: they can concatename, extract and replace strings, but generation of arbitrary-length strings according to a certain pattern AFAIK can only be done via loops. This is how Zach Scrivena [1]'s solution works.
However, one can notice that the N+1-th tree line can be generated from the N-th line by cutting one leading space off and adding two traling asterisks, which pretty much simplifies the task. Also, the tree truck repeats the tree top so we can re-use that string to get rid of a few extra loops. So, here's my batch file that uses these two tricks (165 characters):
@echo off
setlocal enableextensions enabledelayedexpansion
set s=
for /l %%i in (1,1,%1)do set s= !s!
set t=!s!*
for /l %%i in (1,1,%1)do echo !t!&set t=!t:~1!**
echo %s%*
Assuming that echo is already off and command extensions and delayed variable expansion are on, we can drop the first two lines and shorten the code down to 108 characters.
Usage:
> xmastree.bat 7 & pause
*
***
*****
*******
*********
***********
*************
*
[1] http://stackoverflow.com/questions/392788/code-golf-christmas-edition-how-to-print-out-a-christmas-tree-of-height-n/393677#393677Here's a Ruby Newbie (ha! It rhymes!) with his first working solution:
Ruby 164 characters (of readable code)
n=gets.to_i-1
(0..n).each do |j|
(n-j).times do
print " "
end
(1+j*2).times do
print "*"
end
print "\n"
end
n.times do
print " "
end
print "*"