Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active February 24, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeGolfScotland/4f026b4a458e69aa9995333912f5bc5b to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/4f026b4a458e69aa9995333912f5bc5b to your computer and use it in GitHub Desktop.
Code Golf February 2017

February 2017 - Print a Diamond Matrix

Task

Print a Diamond Matrix x numbers deep, x will always be greater than 0 and less than 10.

Examples:

(4) => 
        1
       121
      12321
     1234321
      12321
       121
        1

(9) => 
        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321
 123456787654321
  1234567654321
   12345654321
    123454321
     1234321
      12321
       121
        1

(1) => 1

*^You can assume that you will always recieved valid input

About Code Golf Scotland

@joaquinferrero
Copy link

joaquinferrero commented Feb 2, 2017

Language: Perl 6
Length: 55
Solution:

sub d{for 1$^a,$a-1…1 {say [~] " "x$a-$_,1$_,$_-1…1}}

Demo:

perl6 -e 'for 4,9,1 { say "($_) =>"; d($_) }; sub d{for 1…$^a,$a-1…1 {say [~] " "x$a-$_,1…$_,$_-1…1}}'
(4) =>
   1
  121
 12321
1234321
 12321
  121
   1
(9) =>
        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321
 123456787654321
  1234567654321
   12345654321
    123454321
     1234321
      12321
       121
        1
(1) =>
1

@HDudzus
Copy link

HDudzus commented Feb 2, 2017

Language: Haskell
Length: 110 103 93 91 84 (excluding newline)
Solution:

l n=[1..n]++[n-1,n-2..1]
f x=putStr$unlines$(\n->(' '<$[1..x-n])++(l n>>=show))<$>l x

@HDudzus
Copy link

HDudzus commented Feb 14, 2017

Golfing in Lisp-like languages is not a particularly good idea with all these parentheses. :-D Anyway, there are only two solutions...
It's a pity, join has to be referenced explicitly from clojure.string. It could otherwise be used instead of (apply str ..). I'm curious if it can be golfed any further.

Language: Clojure
Length: 150 (excluding newline)
Solution:

(defn l[n](concat(range 1(+ n 1))(range(- n 1)0 -1)))
(defn f[x](print(clojure.string/join"\n"(map #(apply str(concat(repeat(- x %)" ")(l %)))(l x)))))

Copy link

ghost commented Feb 24, 2017

Language: JS
Length: 88
Solution:

x=n=>{for(i=n;--i+n;console.log(s))for(j=n;j;s=j--^n?k>0?k+s+k:" "+s:k+"")k=i<0?j+i:j-i}

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