The Challenge
Print a nice Christmas tree with it's own star at the top using the shortest code possible. The tree star is an asterisk (*) and the tree body is made out of 0 The tree must be 10 rows high. Every row should be properly indented in the way that the previous row are centered over the next one. Any given row must have 2 more 0s than the previous, except for the first one that is the star and the second, which has only one 0. The result is something like this:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Tie break for resizable height trees without software changes (except changing height parameter)
Please, paste the resulting tree of your code too!
Leaderboard
var QUESTION_ID=4114,OVERRIDE_USER=73772;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
-
3\$\begingroup\$ Not exactly a duplicate but there's this one on SO: Code Golf Christmas Edition: How to print out a Christmas tree of height N \$\endgroup\$Hasturkun– Hasturkun2011年12月05日 16:39:56 +00:00Commented Dec 5, 2011 at 16:39
88 Answers 88
Java, 141
class t{public static void main(String a[]){char s[]=new char[21];s[9]='*';for(int i=0; i<10;System.out.println(s),s[9+i]=s[9-i]='0',i++);}}
Using a char array ..
Lua: 83, 89 or 97
A fixed-height 10-layer tree can be done in 83
a=string.rep
for i=0,9 do
print(i<1 and a(" ",8).."*" or a(" ",9-i)..a(0,i+i-1))end
Providing a height parameter takes it to 89
h=9
a=string.rep
for i=0,h do
print(i<1 and a(" ",h-1).."*" or a(" ",h-i)..a(0,i+i-1))end
Changing the first line to h=io.read() takes it up to 97, but allows the program to read the height from stdio.
Prints:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Note that the value of h is the size of the tree itself, excluding the star.
Whitespace, 2155 bytes
Program (replace S,T,L with Space,Tab,Linefeed characters respectively):
SSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSTSTSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTSTSLTLSSSSSTSSSSSLTLSSSSSTSSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSSSSTTSSSSLTLSSLLL
or download the whitespace-only program text-file christmas_tree.ws.
Output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
This program simply outputs the 164 successive characters that make up the ten rows of the tree (averaging just over 13 program characters per output character). No doubt there's a much shorter Whitespace program that uses some logic to generate the blocks of repeated tree characters.
T-SQL, (削除) 151 (削除ここまで) 142 bytes
declare @ int
set @=10;with t as(select space(@-1)+'*'z,1 i
union all
select space(@-i)+replicate('0',i*2-1),i+1 from t)
select top(@)z from t
The height of the tree can be adjusted (just change the value of the @h variable)
The query uses a Recursive CTE.
Test link: http://sqlfiddle.com/#!3/d41d8/1695 (from the "Run SQL" dropdown button, select "Plaintext Output")
If run from the SQL Management Studio select "Results to Text" and use this query, as SSMS displays the data left aligned:
declare @h int
set @h=10;with t as(select space(@h-1)+'*'as z,1 as i
union all
select space(@h-i)+replicate('0',i*2-1),i+1 from t)
select top(@h)z from t
Sample SQL Server Management Studio output:
z
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
EDIT: Saved 9 chars thanks to BradC!
-
\$\begingroup\$ Nice technique with the recursive CTE. You can save a few bytes by using
@as the variable instead of@h, and by dropping theAScolumn keywords on z and i (although you'll still need it for table t). Also, in SQL 2008 and later you can set the value as part of the declare:declare @ int=10\$\endgroup\$BradC– BradC2020年04月01日 14:00:09 +00:00Commented Apr 1, 2020 at 14:00 -
\$\begingroup\$ @BradC Wow, thanks! I don' t know how I forgot about that
as, but the@var name - I didn't think that was possible. :) \$\endgroup\$Cristian Lupascu– Cristian Lupascu2020年04月03日 08:47:53 +00:00Commented Apr 3, 2020 at 8:47
Java, 177 Chars
import java.util.Arrays;class C{public static void main(String[]a){
char[]r=new char[17];r[8]='*';int c=10;while(c-->0){if(c<9)Arrays.fill(r,c,17-c,'0');System.out.println(r);}}}
(Line break after main(...){ only for better readability.)
Uses API method Arrays.fill() to reduce number of while loops.
Prints this tree:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Java, 147 chars (or 153)
class V{public static void main(String[]a){int c=10,i=-1,j;while(++i<c){for(j=0;++j<c+i;)System.out.print(j>c+i-2?i<1?"*\n":"\n":j<c-i?" ":"0");}}}
I based this on Daniel Schneller's solution, although I changed a lot. In line with Daniels solution, this one can be parameterized as well, where the number of command line parameters represents the height of the tree:
class V{public static void main(String[]a){int c=a.length,i=-1,j;while(++i<c){for(j=0;++j<c+i;)System.out.print(j>c+i-2?i<1?"*\n":"\n":j<c-i?" ":"0");}}}
Output (how surprising :):
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
PHP, 136 Characters
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
;)
-
1\$\begingroup\$ Now make the size adjustable. :) \$\endgroup\$Ilmari Karonen– Ilmari Karonen2011年12月06日 21:45:28 +00:00Commented Dec 6, 2011 at 21:45
-
\$\begingroup\$ Well, I can't make this size adjustable, but I could write meta-programs which can generate PHP programs like these of arbitrary size ;). \$\endgroup\$Weston C– Weston C2011年12月07日 00:55:45 +00:00Commented Dec 7, 2011 at 0:55
Haskell, 83 chars
r=replicate
main=mapM_ putStrLn$(r 8' '++"*"):map(\x->r(9-x)' '++r(x*2-1)'0')[1..9]
...not as short as I thought it might be.
Haskell, 73 characters
main=mapM_ putStrLn$" *":take 9(iterate((++"00").tail)" 0")
And the output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Python, 64
Yet another python answer.
def f(i,c='0'):print' '*(9-i)+c*(i*2+1)
f(0,'*')
map(f,range(9))
Java, (削除) 155 (削除ここまで) 127
enum t{_;{for(char c=42,i=9,j;--i<0;c=48){j=i;String b="";while(--j>0)b+=" ";while(++j<(10-i)*2)b+=c;System.out.println(b);}}}
and a tree
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
with sed:
$ echo $'\n\n\n\n\n\n\n\n\n'|sed -e '1{ s/^/ */;h;}' -e '2,${x;/ 0/s//000/;/\*/s//0/;h;}'
Output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
VALA, 181
void main(string[] argv){int i,h=int.parse(argv[1]);stdout.printf(string.nfill(h-1,' ')+"*\n");for(i=0;i<h;i++)stdout.printf(string.nfill(h-i-1,' ')+string.nfill(2*i+1,'0')+"\n");}
The result :
[damien@caturday ~]$ ./christmas_tree 5
*
0
000
00000
0000000
000000000
C#, 181 Characters
using System;
class T{static void Main(string[]a){
int h=int.Parse(a[0])-2,i=0;
Console.WriteLine("*".PadLeft(h+1));
while(i<=h)
Console.WriteLine(new string('O',i++*2+1).PadLeft(h+i));
}}
Output:
*
O
OOO
OOOOO
OOOOOOO
OOOOOOOOO
OOOOOOOOOOO
OOOOOOOOOOOOO
OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOO
-
\$\begingroup\$ The Main method does not need a string[] input parameter. It can be declared as: static void Main() \$\endgroup\$m0sa– m0sa2012年03月21日 19:15:01 +00:00Commented Mar 21, 2012 at 19:15
-
\$\begingroup\$ @m0sa It's used to pick up the height from the command line arguments. You can of course make it shorter by hardcoding the height. \$\endgroup\$ICR– ICR2012年03月26日 14:41:28 +00:00Commented Mar 26, 2012 at 14:41
-
\$\begingroup\$ Hmm, looks like my BF answer at least beats something... \$\endgroup\$Esolanging Fruit– Esolanging Fruit2016年12月08日 06:38:16 +00:00Commented Dec 8, 2016 at 6:38
Scala, 73 bytes
var(h,c)=(10,"*");0+:(0 to h-2)map{i=>println(" "*(h-i)+c*(i*2+1));c="0"}
Output
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
-
\$\begingroup\$ Welcome to Programming Puzzles and Code Golf. This is a very well golfed answer (+1), however it could benefit from a code breakdown and explanation. \$\endgroup\$wizzwizz4– wizzwizz42016年01月10日 10:04:33 +00:00Commented Jan 10, 2016 at 10:04
Java, 230 characters
My own take.
class p{public static void main(String[]a){new p().m();}void m(){int h=0;x(20,1,"*");while(h<9)x(20-h,2*h+++1,"0");}void x(int s,int f,String c){for(;s>0;s--)d(" ");while(f-->0)d(c);d("\n");}void d(String s){System.out.print(s);}}
And the output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
The code indented:
class p {
public static void main(String[] a) {
new p().m();
}
void m() {
int h = 0;
x(20, 1, "*");
while (h < 9)
x(20 - h, 2 * h++ + 1, "0");
}
void x(int s, int f, String c) {
for (; s > 0; s--)
d(" ");
while (f-- > 0)
d(c);
d("\n");
}
void d(String s) {
System.out.print(s);
}
}
Adding parameters for height should take only a few more characters, but I am very far way of winning :P
Ruby, 51 bytes
puts' '*8+'*';9.times{|a|puts' '*(8-a)+'0'*(a*2+1)}
Output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
-
\$\begingroup\$ Alas, there's already a shorter Ruby solution. \$\endgroup\$Ilmari Karonen– Ilmari Karonen2011年12月10日 17:43:03 +00:00Commented Dec 10, 2011 at 17:43
F#, 167 bytes
let r n s=String.replicate n s
let rec x h i=
let w=r i" "
match h with|0->w+"*\n"|1->(x(h-1)i)+w+"0\n"|z->(x(h-1)(i+1))+w+(r(z+z-1)"0")+"\n"
printfn "%s"(x 8 0)
PHP, 109 characters
for($i=10,$j=1,$k=0;$i;$i--,$j+=2,$k=2)echo str_pad(str_repeat($j<2?'*':0,$j-$k),$i+$j-(!$k?1:2),' ',0)."\n";
Not the shortest code possible, but easily resizable with changing only the value of $i.
Powershell, 38 bytes
' '*8+'*'
8..0|%{' '*$_+'0'*(17-2*$_)}
Output:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Powershell with parameter, 49 bytes
param($h)' '*$h+'*'
$h..0|%{' '*$_+'0'*(17-2*$_)}
BATCH, (削除) 96 90 83 73 (削除ここまで) 71 bytes
Fixed height=10:
@set[= 0
@echo%[:0=*%
:0
@echo%[%
@set[=%[:~1%00
@goto:0%[:~1%
Saved 6 bytes by replacing \r\n line terminators with \n (still works)
(削除) P.S I'm new to golf, so I'm not sure if Removed pause is required. (削除ここまで)@pause, saved 7 bytes.
2020年4月30日 - Abused token delimiters [, golfed 2 bytes.
-
\$\begingroup\$ I would say you do not need
\n@pause, since it does not change your program's output. \$\endgroup\$Jonathan Frech– Jonathan Frech2020年03月31日 10:17:16 +00:00Commented Mar 31, 2020 at 10:17 -
\$\begingroup\$ @JonathanFrech thanks, it's removed \$\endgroup\$Jeff Wu– Jeff Wu2020年04月01日 10:48:14 +00:00Commented Apr 1, 2020 at 10:48
Notepad++, 27 keystrokes
0000<Ctrl-A><Ctrl-D><Ctrl-A><Ctrl-D><Home>0<Ctrl-Shift-R><Ctrl-D><Del><Backsp><Space>
→<Ctrl-Shift-R><Ctrl-Shift-P><Ctrl-Shift-P><Ctrl-Shift-P><Ctrl-Shift-P>
<Ctrl-Shift-P><Ctrl-Shift-P><Ctrl-Shift-P><Ctrl-D><Backsp>*
(Newlines included only for readability.)
Explanation
We'll build the tree from the bottom up:
The setup
Starting with four 0s, we CtrlA CtrlD (select all, duplicate) twice to get sixteen 0s. We need seventeen 0s on the bottom row, so we go Home and add another one. The cursor is now positioned right after the first 0 on the line.
The macro
CtrlShiftR starts recording a macro, which we'll use to generate each line from the one below it:
- CtrlD duplicates the line, leaving the cursor on the upper of the two copies
- Since the cursor is just after the first
0on the line, Del Backsp Space removes two0s and prepends a space - Finally, → moves one character rightward, so that we are once again right after the first
0on the line
A second CtrlShiftR ends the macro, and then CtrlShiftP seven times calls the macro repeatedly to generate the rest of the body of the tree.
The finishing touch
For the star, we need only duplicate the top row with CtrlD, remove the 0 with Backsp, and replace it with a *.
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Rockstar, 72 bytes
say " "*8+"*"
X's0
while 9-X
let Y be 8-X
say " "*Y+0+"0"*X*2
let X be+1
Try it here (Code will need to be pasted in)
Charcoal, fixed height: (削除) 10 (削除ここまで) 8 bytes
↓*G↙→→90
-2 bytes thanks to @Neil.
Try it online (verbose) or try it online (pure).
Explanation:
Print the "*", and move the cursor down:
Print(:Down, "*");
↓*
Draw a polygon in the shape of a triangle, with the initial arm as size 9 and using the character "0" as filler:
Polygon(:DownRight, :Left, :Left, 9, "0");
G↙→→90
Variable height through input: (削除) 14 (削除ここまで) 9 bytes :
↓*G↘←←⊖θ0
-5 bytes thanks to @Neil.
Try it online (verbose) or try it online (pure).
Explanation:
Print the "*", and move the cursor down:
Print(:Down, "*");
↓*
Draw a polygon in the shape of a triangle, with the initial arm as size input-1 and using the character "0" as filler:
Polygon(:DownRight, :Left, :Left, Decremented(q), "0");
G↘←←⊖θ0
-
1\$\begingroup\$ By reversing the order of drawing, you get
↓*G↙→→90for 8 bytes. \$\endgroup\$Neil– Neil2020年11月18日 14:49:42 +00:00Commented Nov 18, 2020 at 14:49 -
\$\begingroup\$ @Neil Thanks! And the same golfs 3 bytes in the variable answer, + 2 more because the input can be done implicitly for integers as well now. (This wasn't the case prior, was it? I can remember it wasn't when I posted my first Charcoal answer a few years back.) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年11月18日 15:38:53 +00:00Commented Nov 18, 2020 at 15:38
-
\$\begingroup\$ As far as I can remember, I think Decremented has always cast from string to number first. \$\endgroup\$Neil– Neil2020年11月18日 15:41:53 +00:00Commented Nov 18, 2020 at 15:41
-
\$\begingroup\$ I think you forgot to update
G↗↘←⊖θ0M⊖θ↑*to your latest linked code in your explanation? \$\endgroup\$Neil– Neil2020年11月18日 15:44:07 +00:00Commented Nov 18, 2020 at 15:44 -
\$\begingroup\$ @Neil Oops, thanks for noticing. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年11月18日 15:48:10 +00:00Commented Nov 18, 2020 at 15:48
Jelly, (削除) 17 (削除ここまで) 16 bytes
6ẋ8)*¶9μ>þa6UŒBY
This is my first time golfing in Jelly; I'm sure this could be golfed further. Advice welcome. :^)
Explanation
6 Space character
ẋ8 Repeated 8 times
Since this niladic chain doesn't connect to anything, just output it
)*¶ Two-character string containing * and newline
Since this niladic chain doesn't connect to anything, just output it
9μ Apply the following monadic chain to a starting value of 9:
>þ Make a 9x9 table, where each entry is 1 if the column number is
greater than the row number, 0 if not
a6 Logical AND with space (replaces the 1's with spaces)
U Reverse each row
ŒB Bounce, mirroring each row to the right
Y Join with newlines
Vyxal, 18 bytes
9\*꘍0(,9n-0円n›*꘍øm
Fairly well golfed.
9 # 9
\* # '*'
꘍ # ' ' * 9 + '*'
0( # 10 times
, # output
9n- # 9 - (iteration number)
0円 # String '0'
n› # iteration number + 1
* # That many 0s
꘍ # (9 - (iteration number)) spaces + 0s
øm # Palindromised
Tree:
*
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
Python 2, 49 bytes
n=-1
exec"print' '*(8-n/2%n),n*'0'or'*';n+=2;"*10
Python 3, 47 bytes
n=-1
while n<19:print(f"{n*'0'or'*':^19}");n+=2
V, 13 bytes
17é0òÄlxx>òr*
It's fun to beat the accepted answer by a huge margin. :D
Explanation:
17é0 " Insert 17 '0's
ò ò " Recursively:
Ä " Copy this line upwards
l " Move one char to the right
xx " Delete two characters
> " Indent this line
r* " Replace the last character with an asterisk
Vyxal C, 9 bytes
×ばつp
17~ # the range 1..17 filtered by
∷ # is odd
0円* # "0" repeated n times for n in those odd nums
×ばつp # with an asterisk prepended
# C flag joins by newlines and centers output
C 145
Takes its heigh param from argc, so set the number of rows by setting the number of arguments:
#define x printf
int main(int c){int i=-1,j=0;while(j++<c)x(" ");x("*");while(++i<c){for(j=-2;++j<c-i;)x(" ");for(j=0;++j<2*i;)x("0");x("\n");}}
To generate the ten line spec output, you call:
christmastree 1 2 3 4 5 6 7 8 9
Or if you'd prefer to hardcode the height, change c in this version:
#define x printf
int main(){int i=-1,j=0,c=10;while(j++<c)x(" ");x("*");while(++i<c){for(j=-2;++j<c-i;)x(" ");for(j=0;++j<2*i;)x("0");x("\n");}}
Explore related questions
See similar questions with these tags.