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 [2021/05/05 07:35]
max
treffen:codegolfing:004_gc [2022/07/07 13:24]
max
Zeile 9: Zeile 9:
 | Serie | [[treffen:codegolfing|CodeGolfings]] | | Serie | [[treffen:codegolfing|CodeGolfings]] |
 | Von | [[user:nopx|nopx]] | | Von | [[user:nopx|nopx]] |
-| Datum | 28.Dez 2019 |+| Datum | 28. Dezember 2019 |
 | Download | {{ :treffen:004.pdf |Aufgabenbeschreibung [pdf]}} | | Download | {{ :treffen:004.pdf |Aufgabenbeschreibung [pdf]}} |
-| Schweirigkeit | 1/5 +| Schweirigkeit | {{:treffen:problemsolving:progress2.png?nolink&160 |}} 
  
 CodeGolfing bis zu den [[treffen:lightningtalks20191228|Lightningtalks II]] am 28.Dez 2019. CodeGolfing bis zu den [[treffen:lightningtalks20191228|Lightningtalks II]] am 28.Dez 2019.
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 59: Zeile 60:
 } }
 a.each{|i|p(0,m)} a.each{|i|p(0,m)}
 +</code>
 +Hatte Tomaten auf den Augen. Nachgereicht 2022-07-01 (190B):
 +<code Ruby>
 +n=ARGV[0].to_i;m=n-1;$b=n*n-n+2;a=(0..m);def p(f,t);s=" "*f+"#"*t+" "*($b-f-t);puts s.reverse+s[1..-1]end;a.each{|i|c=(n-1)*i;a.each{|j|p(c+j,1)};p(*i==m ?[0,c+n+1]:[c+1,n])};a.each{p(0,m)}
 +</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> </code>
  
Zeile 98: 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