Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeGolfScotland/2bb50e11d58dc149e1a303ab87ae45f2 to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/2bb50e11d58dc149e1a303ab87ae45f2 to your computer and use it in GitHub Desktop.
Code Golf December 2016

December 2016 - Oh xmas tree, oh xmas tree

Task

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

About Code Golf Scotland

@billythekid
Copy link

@davyboyhayes, try 3 backticks and the word javascript: ```javascript then three backticks to close;

f=(n,o=0)=>n?f(n-1,o+1)+`
`+' '.repeat(o)+'*'.repeat(2*n-1):''

@joaquinferrero
Copy link

joaquinferrero commented Dec 1, 2016

Language: Perl 6
Length: 57 56 54
Solution: Return the Tree

sub T($n){map({" "x$n-$_-1~"*"x$_*2+1~"\n"},^$n).join}

Demo:

perl6 -e 'for 1, 3, 5 { T($_).print }; sub T($n){map({" "x$n-$_-1~"*"x$_*2+1~"\n"},^$n).join}'

Language: Perl 6
Length: 47 46 43 42
Solution: Print a Tree

sub T{say " "x$^a-$_-1~"*"x$_*2+1 for ^$a}

Demo:

perl6 -e 'for 1, 3, 5 { T($_) }; sub T{say " "x$^a-$_-1~"*"x$_*2+1 for ^$a}'

@ciaranmcnulty
Copy link

ciaranmcnulty commented Dec 15, 2016

Language: PHP

Length: 98

Solution:

function f($i){while($i--){$s=($f=str_pad)($f('',$r=2*$i+1,'*'),$m=$m?:$r,' ',2)."\n$s";}return$s;}

more readably;

    function f($i) {
        while($i--){
             $s = ($f=str_pad)( $f('',$r=2*$i+1,'*'), $m=$m?:$r, ' ', 2) . "\n$s"; 
        }

         return $s;
    }

@chrisshennan
Copy link

chrisshennan commented Dec 15, 2016

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;}

Copy link

ghost commented Dec 15, 2016

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;
}

@alessandrozucca
Copy link

alessandrozucca commented Dec 16, 2016

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")}}

@jordanFagan
Copy link

jordanFagan commented Dec 29, 2016

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

@HDudzus
Copy link

HDudzus commented Dec 31, 2016

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment