Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
|
treffen:codegolfing:004_gc [2021/05/05 07:12] max |
treffen:codegolfing:004_gc [2022/07/07 13:26] (aktuell) max [Lösungen] |
||
|---|---|---|---|
| Zeile 5: | Zeile 5: | ||
| - | ====== 004 CodeGolfing | + | ====== 004: XMAS Tree ====== |
| - | | Serie: | [[treffen: | + | | Serie | [[treffen: |
| + | | Von | [[user: | ||
| + | | Datum | 28. Dezember 2019 | | ||
| + | | Download | {{ : | ||
| + | | Schweirigkeit | {{: | ||
| CodeGolfing bis zu den [[treffen: | CodeGolfing bis zu den [[treffen: | ||
| Zeile 15: | Zeile 19: | ||
| ===== Lösungen ===== | ===== Lösungen ===== | ||
| Perl, nopx, 191B, {{ : | Perl, nopx, 191B, {{ : | ||
| + | Eine lesbare Lösung mit diesem Ansatz gibt es ganz unten auf dieser Seite. ;) | ||
| <code Perl> | <code Perl> | ||
| $m=$ARGV[0]-1; | $m=$ARGV[0]-1; | ||
| Zeile 55: | Zeile 60: | ||
| } | } | ||
| a.each{|i|p(0, | a.each{|i|p(0, | ||
| + | </ | ||
| + | Hatte Tomaten auf den Augen. Nachgereicht 2022-07-01 (190B): | ||
| + | <code Ruby> | ||
| + | n=ARGV[0].to_i; | ||
| + | </ | ||
| + | |||
| + | 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=" " | ||
| + | print(l[: | ||
| + | for e in r: | ||
| + | for l in r: | ||
| + | p(1,m*e+l) | ||
| + | p(w)if e==m else p(m+1, | ||
| + | for _ in r:p(m) | ||
| </ | </ | ||
| Zeile 94: | Zeile 120: | ||
| <code Java> | <code Java> | ||
| public class X{public static void main(String[]b){int n=Integer.valueOf(b[0]); | public class X{public static void main(String[]b){int n=Integer.valueOf(b[0]); | ||
| + | </ | ||
| + | |||
| + | Python in schön, nopx | ||
| + | <code 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 :int ,num_spaces :int =0): | ||
| + | |||
| + | line = " " | ||
| + | # Remove last column, mirror, concatenate together and plot: | ||
| + | print(line[: | ||
| + | |||
| + | # 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 ) | ||
| </ | </ | ||