treffen:codegolfing:004_gc

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
Letzte Überarbeitung Beide Seiten, nächste Überarbeitung
treffen:codegolfing:004_gc [2022/07/01 18:54]
marc_weber [Lösungen]
treffen:codegolfing:004_gc [2022/07/07 13:24]
max
Zeile 19: Zeile 19:
 ===== Lösungen ===== ===== Lösungen =====
 Perl, nopx, 191B, {{ :treffen:004_gc_solution_nopx.pdf |Beschreibung}} Perl, nopx, 191B, {{ :treffen:004_gc_solution_nopx.pdf |Beschreibung}}
 +Eine lesbare Lösung mit diesem Ansatz gibt es ganz unten auf dieser Seite. ;) 
 <code Perl> <code Perl>
 $m=$ARGV[0]-1;sub p{($h,$s)=@_;$_=$"x($m**2+$m+2-$s-$h)."#"x$h.$"x$s;print;chop;print~~reverse.$/}for(0..$m){$w=$m*$_;p(1,$w+$_)for(0..$m);$b=($w+1)*($_!=$m);p($m+2+$w-$b,$b)}p($m)for(0..$m) $m=$ARGV[0]-1;sub p{($h,$s)=@_;$_=$"x($m**2+$m+2-$s-$h)."#"x$h.$"x$s;print;chop;print~~reverse.$/}for(0..$m){$w=$m*$_;p(1,$w+$_)for(0..$m);$b=($w+1)*($_!=$m);p($m+2+$w-$b,$b)}p($m)for(0..$m)
Zeile 65: Zeile 66:
 </code> </code>
  
 +Python, nopx, 224B (nachgereicht)
 +<code Python>
 +import sys
 +n=int(sys.argv[1])
 +m=n-1
 +w=m*m+m+2
 +r=range(n)
 +def p(h,s=0):
 +    l=" "*(w-h-s)+"#"*h+" "*s
 +    print(l[:-1]+l[::-1])
 +for e in r:
 +    for l in r:
 +        p(1,m*e+l)
 +    p(w)if e==m else p(m+1,m*e+1)
 +for _ in r:p(m)
 +</code>
  
 Python, v0tti, 226B Python, v0tti, 226B
Zeile 103: Zeile 120:
 <code Java> <code Java>
 public class X{public static void main(String[]b){int n=Integer.valueOf(b[0]);int s=n*(n-1)+1;int r=s*2;char a[]=new char[r+1];for(int k=0;k<n;k++){for(int j=0;j<n;j++){for(int i=0;i<r+1;i++)a[i]=' ';int t=k*n+j-k;a[s-t]='#';a[s+t]='#';p(s,a);l();}if(k==n-1){for(int i=0;i<r+1;i++)a[i]='#';p(s,a);}else{int x=0;for(int i=0;i<r+1;i++){while(x<n){a[s-n-(k*n-k)+x]='#';a[s+n+(k*n-k)-x]='#';x++;}}p(s,a);l();}}for(int i=0;i<r+1;i++)a[i]=' ';int x=0;while(x<n){l();for(int i=s-(n-2);i<=s+(n-2);i++)a[i]='#';p(s,a);x++;}}static void p(int s,char a[]){for(int i=0;i<(s*2)+1;i++){System.out.print(a[i]);}}static void l(){System.out.println();}} public class X{public static void main(String[]b){int n=Integer.valueOf(b[0]);int s=n*(n-1)+1;int r=s*2;char a[]=new char[r+1];for(int k=0;k<n;k++){for(int j=0;j<n;j++){for(int i=0;i<r+1;i++)a[i]=' ';int t=k*n+j-k;a[s-t]='#';a[s+t]='#';p(s,a);l();}if(k==n-1){for(int i=0;i<r+1;i++)a[i]='#';p(s,a);}else{int x=0;for(int i=0;i<r+1;i++){while(x<n){a[s-n-(k*n-k)+x]='#';a[s+n+(k*n-k)-x]='#';x++;}}p(s,a);l();}}for(int i=0;i<r+1;i++)a[i]=' ';int x=0;while(x<n){l();for(int i=s-(n-2);i<=s+(n-2);i++)a[i]='#';p(s,a);x++;}}static void p(int s,char a[]){for(int i=0;i<(s*2)+1;i++){System.out.print(a[i]);}}static void l(){System.out.println();}}
 +</code>
 +
 +Python in schön, nopx
 +<code Python>
 +#!/usr/bin/python
 +
 +import sys
 +
 +# Read command line argument:
 +n = int(sys.argv[1])
 +
 +# We often use n-1, so lets use a variable for that:
 +m = n-1
 +
 +# Calculate length of left part
 +l_left = m**2 + m + 2
 +
 +# Helper function to plot a row
 +def plotline(num_hashes,num_spaces :int =0):
 +
 +    line = " "*(l_left - num_hashes - num_spaces) + "#"*num_hashes + " "*num_spaces
 +    # Remove last column, mirror, concatenate together and plot:
 +    print(line[:-1] + line[::-1])
 +
 +# Print tree part
 +for etagenumber in range(n):
 +    # You may set w = m * etagenumber as we often use that in the following.
 +    for linenumber in range(n):
 +        # Plot lines with on #
 +        plotline(1, m*etagenumber + linenumber)
 +
 +    # Plot line with multiple #
 +    if etagenumber == m:
 +        # Something special for the last etage:
 +        plotline( l_left )
 +    else:
 +        # For all other etages:
 +        plotline( m+1, m*etagenumber + 1)
 +
 +# Print trunk
 +for _ in range(n): plotline( m ) 
 </code> </code>
  • treffen/codegolfing/004_gc.txt
  • Zuletzt geändert: 2022/07/07 13:26
  • von max