Bergervei/Python-Balkendiagramm
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Python</language>
== Balkendiagramm ==
<code id="63ac2383f3774">
canvas = window.Canvas
turtle = window.Turtle
rand = 50
maxbreite = 400 - 2 * rand
maxhoehe = 300 - 2 * rand
def minmax(ls):
erg = [0, 0]
for i in range(len(ls)):
if erg[0] > ls[i]: erg[0] = ls[i]
if erg[1] < ls[i]: erg[1] = ls[i]
return erg
def balken(turtle, hoehe, breite):
turtle.forward(breite); turtle.left(90)
turtle.forward(hoehe); turtle.left(90)
turtle.forward(breite); turtle.left(90)
turtle.forward(hoehe); turtle.left(90)
turtle.forward(breite)
def balkendiagramm(turtle, ls):
breite = maxbreite / len(ls)
extrem = minmax(ls)
minwert = extrem[0]; maxwert = extrem[1]
turtle.penUp()
turtle.right(90)
turtle.move(rand, rand + maxwert / (maxwert - minwert) * maxhoehe)
turtle.penDown()
for i in range(len(ls)):
balken(turtle, ls[i] / (maxwert - minwert) * maxhoehe, breite)
</code>
== Zufallszahlen ==
<code id="63ac26b85a91d">
from random import randint
def zufallsliste(von, bis, anz):
ls = []
for i in range(anz):
ls.append(randint(von, bis))
return ls
def bubblesort(ls):
for i in range(len(ls) - 1):
for j in range(i, len(ls)):
if ls[i] > ls[j]:
ls[i], ls[j] = ls[j], ls[i]
return ls
</code>
<table border="0">
<tr>
<td><canvas w="400" h="300"></canvas></td>
<td><canvas w="400" h="300"></canvas></td>
</tr>
</table>
<run id="63ac26b85a975">
c1 = canvas.new(1); c1.clear()
t1 = turtle.new(1); t1.hide()
ls1 = zufallsliste(1, 100, 50)
print(ls1)
balkendiagramm(t1, ls1)
c2 = canvas.new(2); c2.clear()
t2 = turtle.new(2); t2.hide()
ls2 = bubblesort(ls1)
print(ls2)
balkendiagramm(t2, ls2)
</run>