def main(argv): r1 = int(argv[1]) g1 = int(argv[2]) b1 = int(argv[3]) c1 = color.Color(r1, g1, b1) r2 = int(argv[4]) g2 = int(argv[5]) b2 = int(argv[6]) c2 = color.Color(r2, g2, b2) stddraw.createWindow() stddraw.setPenColor(c1) stddraw.filledSquare(.25, .5, .2) stddraw.setPenColor(c2) stddraw.filledSquare(.25, .5, .1) stddraw.setPenColor(c2) stddraw.filledSquare(.75, .5, .2) stddraw.setPenColor(c1) stddraw.filledSquare(.75, .5, .1) stddraw.show() stddraw.wait()
def main(argv): M = int(argv[1]) N = int(argv[2]) S = int(argv[3]) # Create a RandomQueue object containing Queue objects. servers = randomqueue.RandomQueue() for i in range(M): servers.enqueue(linkedlistqueue.Queue()) for j in range(N): # Assign an item to a server. min = servers.sample() for k in range(1, S): # Pick a random server, update if new min. q = servers.sample() if len(q) < len(min): min = q # min is the shortest server queue. min.enqueue(j) lengths = [] for q in servers: lengths += [len(q)] stddraw.createWindow() stddraw.setYscale(0, 2.0*N/M) stdstats.plotBars(lengths) stddraw.show() stddraw.wait()
def main(argv): fileName = argv[1] w = int(argv[2]) h = int(argv[3]) source = picture.Picture() source.load(fileName) target = picture.Picture(w, h) for ti in range(w): for tj in range(h): si = ti * source.width() // w sj = tj * source.height() // h target.set(ti, tj, source.get(si, sj)) maxHeight = max(source.height(), target.height()) stddraw.createWindow(source.width() + target.width(), maxHeight) stddraw.setXscale(0, source.width() + target.width()) stddraw.setYscale(0, maxHeight) stddraw.picture(source, source.width() / 2, maxHeight / 2) stddraw.picture(target, source.width() + target.width() / 2, maxHeight / 2) stddraw.show() stddraw.wait()
def main(argv): M = int(argv[1]) N = int(argv[2]) S = int(argv[3]) # Create a RandomQueue object containing Queue objects. servers = randomqueue.RandomQueue() for i in range(M): servers.enqueue(linkedlistqueue.Queue()) for j in range(N): # Assign an item to a server. min = servers.sample() for k in range(1, S): # Pick a random server, update if new min. q = servers.sample() if len(q) < len(min): min = q # min is the shortest server queue. min.enqueue(j) lengths = [] for q in servers: lengths += [len(q)] stddraw.createWindow() stddraw.setYscale(0, 2.0 * N / M) stdstats.plotBars(lengths) stddraw.show() stddraw.wait()
def main(argv): H = float(argv[1]) stddraw.createWindow() s = 2 ** (2*H) # or this: s = math.pow(2, 2*H) curve(0, .5, 1.0, .5, .01, s) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() step = 1.0 / (3.0 ** n) tur = turtle.Turtle(0.0, 0.0, 0.0) koch(n, step, tur) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() turtle = Turtle(.5, .0, 180.0 / n) for i in range(n): step = math.sin(math.radians(180.0 / n)) turtle.goForward(step) turtle.turnLeft(360.0 / n) stddraw.wait()
def main(argv): var = float(argv[1]) n = int(argv[2]) stddraw.createWindow() stddraw.clear() stddraw.setXscale(-1, +1) stddraw.setYscale(-1, +1) midpoint(0, 0, 0, 0, var / math.sqrt(2), n) stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() turtle = Turtle(.5, .0, 180.0/n) for i in range(n): step = math.sin(math.radians(180.0/n)) turtle.goForward(step) turtle.turnLeft(360.0/n) stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() x = 0.5 # center of square y = 0.5 # center of square size = 0.5 # side length of square draw(n, x, y, size) stddraw.show() stddraw.wait()
def main(args): t = int(args[1]) step = float(args[2]) stddraw.createWindow() myTurtle = turtle.Turtle(0.5, 0.5, 0.0) for t1 in range(t): myTurtle.turnLeft(360.0 * random.random()) myTurtle.goForward(step) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) # number of discs # Set size of window and sale stddraw.createWindow(4*WIDTH, (n+3)*HEIGHT) stddraw.setXscale(-1, 3) stddraw.setYscale(0, n+3) # Solve the Towers of Hanoi with N discs hanoi(n) stddraw.wait()
def main(argv): n = int(argv[1]) # number of discs # Set size of window and sale stddraw.createWindow(4 * WIDTH, (n + 3) * HEIGHT) stddraw.setXscale(-1, 3) stddraw.setYscale(0, n + 3) # Solve the Towers of Hanoi with N discs hanoi(n) stddraw.wait()
def main(argv): import bernoulli coinCount = int(argv[1]) trialCount = int(argv[2]) histogram = Histogram(coinCount + 1) for trial in range(trialCount): histogram.addDataPoint(bernoulli.binomial(coinCount)) stddraw.createWindow(500, 100) histogram.draw() stddraw.show() stddraw.wait()
def main(args): n = int(args[1]) t = int(args[2]) step = float(args[3]) stddraw.createWindow() turtles = [] for i in range(n): turtles += \ [turtle.Turtle(random.random(), random.random(), 0.0)] for t1 in range(t): for i in range(n): turtles[i].turnLeft(360.0 * random.random()) turtles[i].goForward(step) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) p = float(argv[2]) t = int(argv[3]) stddraw.createWindow() for i in range(t): open = percolation.random(n, p) stddraw.clear() stddraw.setPenColor(stddraw.BLACK) percolation.show(open, False) stddraw.setPenColor(stddraw.BLUE) full = percolation.flow(open) percolation.show(full, True) stddraw.sleep(1000) stddraw.show() stddraw.wait()
def main(): stddraw.createWindow() stddraw.setXscale(0, 15) stddraw.setYscale(0, 15) for i in range(16): for j in range (16): #val = i + 16*j val = 8*i + 8*j #c1 = color.Color(0, 0, 255-val) c1 = color.Color(255-val, 255-val, 255) c2 = color.Color(val, val, val) stddraw.setPenColor(c1) stddraw.filledSquare(i, j, 0.5) stddraw.setPenColor(c2) stddraw.filledSquare(i, j, 0.25) stddraw.show() stddraw.wait()
def main(argv): n = float(argv[1]) decay = float(argv[2]) stddraw.createWindow() stddraw.setPenRadius(0) angle = 360.0 / n step = math.sin(math.radians(angle/2.0)) t = turtle.Turtle(0.5, 0, angle/2.0) i = 0 while i < 10.0 * 360.0 / angle: step /= decay t.goForward(step) t.turnLeft(angle) i += 1 stddraw.show() stddraw.wait()
def main(): stddraw.createWindow() stddraw.setXscale(0, 15) stddraw.setYscale(0, 15) for i in range(16): for j in range(16): #val = i + 16*j val = 8 * i + 8 * j #c1 = color.Color(0, 0, 255-val) c1 = color.Color(255 - val, 255 - val, 255) c2 = color.Color(val, val, val) stddraw.setPenColor(c1) stddraw.filledSquare(i, j, 0.5) stddraw.setPenColor(c2) stddraw.filledSquare(i, j, 0.25) stddraw.show() stddraw.wait()
def main(argv): n = int(sys.argv[1]) stddraw.createWindow() cx = [0.000, 1.000, 0.500] cy = [0.000, 0.000, 0.866] x = 0.0 y = 0.0 for i in range(n): r = stdrandom.uniformInt(0, 3) x = (x + cx[r]) / 2.0 y = (y + cy[r]) / 2.0 stddraw.setPenRadius(0.001) stddraw.point(x, y) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) t = int(argv[2]) stddraw.createWindow() stddraw.setYscale(0, 0.2) freq = [0] * (n+1) for t in range(t): freq[binomial(n)] += 1 norm = [0.0] * (n+1) for i in range(n+1): norm[i] = float(freq[i]) / float(t) stdstats.plotBars(norm) stddev = math.sqrt(n) / 2.0 phi = [0.0] * (n+1) for i in range(n+1): phi[i] = gaussian.phi(i, n/2.0, stddev) stdstats.plotLines(phi) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() curve(n, 0.0, 0.0, 1.0, 1.0) stddraw.wait()
#----------------------------------------------------------------------- # plotfilter.py #----------------------------------------------------------------------- import stdio import stddraw # Plot the points read from standard input. x0 = stdio.readFloat() y0 = stdio.readFloat() x1 = stdio.readFloat() y1 = stdio.readFloat() stddraw.createWindow() stddraw.setXscale(x0, x1) stddraw.setYscale(y0, y1) stddraw.setPenRadius(0.001) # Read and plot the points. while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() stddraw.point(x, y) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() tree(n, .5, 0, math.pi/2, .3) stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() draw(n, .5, .5, .5) stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() draw(n, 0.5, 0.5, 0.5) stddraw.wait()