예제 #1
0
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()
예제 #2
0
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()
예제 #3
0
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()
예제 #4
0
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()
예제 #5
0
def main():
	stddraw.createWindow()
	import Tkinter as tk
	import tkFileDialog
	root = tk.Tk()
	root.withdraw()
	reply = tkFileDialog.asksaveasfilename(initialdir='.')
	print reply
예제 #6
0
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()
예제 #7
0
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()
예제 #8
0
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()
예제 #9
0
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()
예제 #10
0
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()
예제 #11
0
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()
예제 #12
0
def main(argv):
    stddraw.createWindow()
    newton = Universe()
    dt = float(argv[1])
    while True:
        newton.increaseTime(dt)
        newton.draw()
        stddraw.sleep(10)
        stddraw.show()
        stddraw.clear()
예제 #13
0
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()
예제 #14
0
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()
예제 #15
0
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()
예제 #16
0
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()
예제 #17
0
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()
예제 #18
0
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()
예제 #19
0
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()
예제 #20
0
def main(argv):

    lamb = float(argv[1])  # Arrival rate
    mu = float(argv[2])    # Service rate

    hist = histogram.Histogram(60 + 1)
    q = linkedlistqueue.Queue()
    stddraw.createWindow(700, 500)

    nextArrival = stdrandom.exp(lamb)  # Time of next arrival
    nextService = nextArrival + 1.0/mu # Time of next completed service

    # Simulate the M/D/1 queue
    while True:

        # Next event is an arrival.
        while nextArrival < nextService:
            # Simulate an arrival
            q.enqueue(nextArrival)
            nextArrival += stdrandom.exp(lamb)

        # Next event is a service completion.
        arrival = q.dequeue()
        wait = nextService - arrival

        # Update the histogram.
        stddraw.clear()
        hist.addDataPoint(min([60, int(wait+0.5)]))
        hist.draw()
        #stddraw.sleep(20)
        stddraw.sleep(20)
        stddraw.show()
        # Update the queue.
        if q.isEmpty():
            nextService = nextArrival + 1.0/mu
        else:
            nextService = nextService + 1.0/mu
예제 #21
0
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()
예제 #22
0
def main(argv):
    n = int(argv[1])
    stddraw.createWindow()
    tree(n, .5, 0, math.pi/2, .3)
    stddraw.wait()
예제 #23
0
def main(argv):
    n = int(argv[1])
    stddraw.createWindow()
    curve(n, 0.0, 0.0, 1.0, 1.0)
    stddraw.wait()
예제 #24
0
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-0.75, +0.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample)

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
예제 #25
0
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-.75, +.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i-24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample);

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
예제 #26
0
def main():

    stddraw.createWindow()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    # loopCount is used to control the frequency of calls of
    # stddraw.show().
    loopCount = 1023

    # The main input loop.
    while True:

        # Call stddraw.show() occasionally to capture keyboard events.
        if loopCount == 1023:
            stddraw.show()
            loopCount = 0
        loopCount += 1

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)
예제 #27
0
def main(argv):
    n = int(argv[1])
    stddraw.createWindow()
    draw(n, 0.5, 0.5, 0.5)
    stddraw.wait()
예제 #28
0
#-----------------------------------------------------------------------
# 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()
예제 #29
-1
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()