lehrkraefte:blc:informatik:ffprg1-2020:stringvar

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lehrkraefte:blc:informatik:ffprg1-2020:stringvar [2020/02/04 09:59]
Ivo Blöchliger
lehrkraefte:blc:informatik:ffprg1-2020:stringvar [2022/03/24 13:50] (current)
Ivo Blöchliger [Text Quadrate]
Line 13: Line 13:
 </code> </code>
  
-Einzelne **Bytes** als Strings der Länge 1 auslesen (nicht zwingend Buchstaben):+Einzelne Zeichen als Strings der Länge 1 auslesen:
 <code python> <code python>
 a = "abcde" a = "abcde"
Line 25: Line 25:
 print(len(a)) print(len(a))
 b = "öh" b = "öh"
-print(len(b))   # ergibt 3weil das 'ö' zwei Bytes belegt!+print(len(b))   # ergibt 2  (früher erhielt man 3 weil das 'ö' zwei Bytes belegt und len die Anzahl Bytes gezählt hat).
 </code> </code>
  
 String mit Zahl multiplizieren: String mit Zahl multiplizieren:
 <code python> <code python>
-print("na"*31+" she's got the look!")+print("na"*31 + " she's got the look!")
 </code> </code>
 +
 +Substrings:
 +<code python>
 +a="hello world!"
 +print(a[3:5])   # Von und mit viertem bis ohne sechstem Byte
 +print(a[6:])    # Von siebtem Bytes bis Schluss
 +print(a[:5])    # Von Anfang bis ohne sechstem Byte
 +</code>
 +
 +Loop:
 +<code python>
 +for b in "Hello World":
 +   print(b)
 +</code>
 +
 +====== Aufgaben ======
 +===== Text Quadrate =====
 +Das Programm hat als Eingabe einen String. Damit sollen folgendes Text-Quadrat produziert werden:
 +<code>
 +P Y T H O N 
 +Y T H O N P 
 +T H O N P Y 
 +H O N P Y T 
 +O N P Y T H 
 +N P Y T H O 
 +</code>
 +
 +<hidden Lösungsvorschlag>
 +<code python textquadrat.py>
 +import sys
 +t = "PYTHON"
 +for i in range(len(t)):
 +    for j in range(len(t)):
 +        sys.stdout.write(t[(i+j)%len(t)]+" ")
 +    print
 +</code>
 +<code python textquadrat2.py>
 +a = "QUADRAT"
 +for i in range(len(a)):
 +    wort = ""
 +    for b in a:
 +        wort += b+" "
 +    print(wort)
 +    a = a[-1]+a[:-1]
 +</code>
 +<code python>
 +a = "PYTHON"
 +l = len(a)
 +a+=a
 +for i in range(l):
 +    print(a[i:i+l])
 +</code>
 +</hidden>
 +
 +**Challenge** Produzieren Sie folgende Ausgabe
 +<code>
 +P Y T H O N 
 +Y T H O N O 
 +T H O N O H 
 +H O N O H T 
 +O N O H T Y 
 +N O H T Y P 
 +</code>
 +<hidden Lösungsvorschlag>
 +<code python textquadrat2.py>
 +import sys
 +t = "PYTHON"
 +for i in range(len(t)):
 +    for j in range(len(t)):
 +        p = i+j
 +        if (p>=len(t)):
 +            p = len(t)-p-2
 +        sys.stdout.write(t[p]+" ")
 +    print
 +</code>
 +Die Idee
 +<code python textquadrat3.py>
 +a = "PYTHON"
 +w = a + a[-2::-1]  # Wort plus Wort rückwärts ohne letzten Buchstaben
 +for i in range(len(a)):
 +    wort = ""
 +    for j in range(len(a)):
 +        wort += w[i+j] + " "      # Alle Buchstaben auf einer Diagonalen haben die gleiche Summe i+j
 +    print(wort)
 +</code>
 +</hidden>
 +===== Raster =====
 +Gegeben sind zwei Zahlen $x$ (Breite) und $y$ Höhe. Produzieren Sie dann folgende Ausgabe, Beispiel für x=6 und y=3
 +<code txt>
 ++---+---+---+---+---+---+
 +|             |
 ++---+---+---+---+---+---+
 +|             |
 ++---+---+---+---+---+---+
 +|             |
 ++---+---+---+---+---+---+
 +</code>
 +<hidden Lösungsvorschlag>
 +<code python raster.py>
 +x = 6
 +y = 3
 +
 +h = "+---"*x + "+"
 +v = "  "*x + "|"
 +for i in range(y):
 +    print(h)
 +    print(v)
 +print(h)
 +</code>
 +</hidden>
 +===== ASCII-Art =====
 +Gegeben ist die Grösse des folgenden "Quadrats", wobei $x$ für Anzahl Schrägstriche oberhalb vom X steht. Produzieren Sie damit folgende Ausgabe (für $x=2$):
 +<code>
 ++-----+
 +|\   /|
 +| \ / |
 +|  X  |
 +| / \ |
 +|/   \|
 ++-----+
 +</code>
 +Für $x=3$:
 +<code>
 ++-------+
 +|\     /|
 +| \   / |
 +|  \ /  |
 +|     |
 +|  / \  |
 +| /   \ |
 +|/     \|
 ++-------+
 +</code>
 +Hinweis: Ein Backslash '\' wird in einem String mit "\\" geschrieben. Der Grund ist, weil z.B. "\n" für einen Zeilenumbruch oder "\"" für den String mit einem Anführungszeichen steht.
 +<hidden Lösungsvorschlag>
 +<code python asciiart.py>
 +x = 3
 +h = "+" + "-"*(x*2+1) + "+"
 +print(h)
 +for i in range(1,2*x+2):
 +    l = "|"
 +    for j in range(1,2*x+2):
 +        if (i==x+1 and j==i):
 +            l+="X"
 +        else:
 +            if (j==i):
 +                l+= "\\"
 +            elif(2*x+2-j==i):
 +                l+= "/"
 +            else:
 +                l+=" "
 +    l+="|"
 +    print(l)
 +print(h)
 +</code>
 +</hidden>
 +
  • lehrkraefte/blc/informatik/ffprg1-2020/stringvar.1580806766.txt.gz
  • Last modified: 2020/02/04 09:59
  • by Ivo Blöchliger