def main(): w = stdio.readInt() h = stdio.readInt() tour = Tour() while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() p = Point(x, y) tour.insertSmallest(p) tour.show() stdio.writef('Tour distance = %f\n', tour.distance()) stdio.writef('Number of points = %d\n', tour.size())
def main(): w = stdio.readInt() h = stdio.readInt() stddraw.setCanvasSize(w, h) stddraw.setXscale(0, w) stddraw.setYscale(0, h) stddraw.setPenRadius(.005) while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() p = Point(x, y) p.draw() stddraw.show()
def _main(): x = float(sys.argv[1]) intervals = [] while not stdio.isEmpty(): lbound = stdio.readFloat() rbound = stdio.readFloat() intervals += [Interval(lbound, rbound)] for i in range(len(intervals)): if intervals[i].contains(x): stdio.writef('%s contains %f\n', intervals[i], x) for i in range(len(intervals)): for j in range(i + 1, len(intervals)): if intervals[i].intersects(intervals[j]): stdio.writef('%s intersects %s\n', intervals[i], intervals[j])
def playTune2(): notes = [] #while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = 440 * math.pow(2, pitch / 12.0) playNote(hz, 1)
def main(): # 1. estimating D displacements = [] n = 0 while not stdio.isEmpty(): n += 1 displacements.append(stdio.readFloat() * 0.175 * 10.0 ** -6.0) # now displacements are in meters... D = 0.0 # intialize D # then calculate D as variance of radial displacements in meters: for i in range(len(displacements)): D += displacements[i] * displacements[i] D /= (2 * n) # 2. estimating k T = 297.0 eta = 9.135 * 10.0 ** -4.0 rho = 0.5 * 10.0 ** -6.0 k = (6.0 * math.pi * eta * rho * D) / (T) # 3. estimating Avogadro's number R = 8.31457 N_A = R / k # print: stdio.writef('Boltzman = %.6e\n', k) stdio.writef('Avogadro = %.6e\n', N_A)
def main(): #button = Tkinter.Button(root, text='Play Tune', command=playTune2) #button.pack() #root.mainloop() #stdio.writeln('Playing') #playFile('woman.wav') stdio.writeln('Creating') sps = SAMPLE_RATE while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = 440 * math.pow(2, pitch / 12.0) N = int(sps * duration) notes = [] for i in range(N + 1): notes.append(math.sin(2 * math.pi * i * hz / sps)) stdio.writeln('Playing') playNotes(notes) root.mainloop()
def main(): count = 0 total = 0.0 while not stdio.isEmpty(): disp = stdio.readFloat() disp *= 0.175e-6 disp *= disp total += disp count += 1 # cal var and intialize t, ETA, RHO, r var = total var /= 2 * count t = 297 ETA = 9.135e-4 RHO = 0.5e-6 r = 8.31457 # caluclate number of avogadro k = 6 * math.pi * var * ETA * RHO / t NA = r / k # print out the value stdio.write('Boltzman = ') stdio.writef('%e\n', k) stdio.write('Avogadro = ') stdio.writef('%e\n', NA)
def main(): #button = Tkinter.Button(root, text='Play Tune', command=playTune2) #button.pack() #root.mainloop() #stdio.writeln('Playing') #playFile('woman.wav') stdio.writeln('Creating') sps = SAMPLE_RATE while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = 440 * math.pow(2, pitch / 12.0) N = int(sps * duration) notes = [] for i in range(N+1): notes.append(math.sin(2*math.pi * i * hz / sps)) stdio.writeln('Playing') playNotes(notes) root.mainloop()
def main(): w = stdio.readInt() h = stdio.readInt() stddraw.setCanvasSize(w, h) stddraw.setXscale(0, w) stddraw.setYscale(0, h) stddraw.setPenRadius(.005) tour = Tour() while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() p = Point(x, y) tour.insertSmallest(p) stdio.writef('Tour distance = %f\n', tour.distance()) stdio.writef('Number of points = %d\n', tour.size()) tour.draw() stddraw.show()
def readFloat1D(): """ Read from sys.stdin and return an array of floats. An integer at the beginning of sys.stdin defines the array's length. """ count = stdio.readInt() a = create1D(count, None) for i in range(count): a[i] = stdio.readFloat() return a
def playTune(): sps = SAMPLE_RATE while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = 440 * math.pow(2, pitch / 12.0) N = int(sps * duration) notes = [] for i in range(N + 1): notes.append(math.sin(2 * math.pi * i * hz / sps)) #stdio.writeln('Playing') playNotes(notes)
def playTune(): sps = SAMPLE_RATE while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = 440 * math.pow(2, pitch / 12.0) N = int(sps * duration) notes = [] for i in range(N+1): notes.append(math.sin(2*math.pi * i * hz / sps)) #stdio.writeln('Playing') playNotes(notes)
def readFloat2D(): """ Read from sys.stdin and return a two-dimensional array of floats. Two integers at the beginning of sys.stdin define the array's dimensions. """ rowCount = stdio.readInt() colCount = stdio.readInt() a = create2D(rowCount, colCount, 0.0) for row in range(rowCount): for col in range(colCount): a[row][col] = stdio.readFloat() return a
def _main(): x = float(sys.argv[1]) y = float(sys.argv[2]) rectangles = [] while not stdio.isEmpty(): lbound1 = stdio.readFloat() rbound1 = stdio.readFloat() lbound2 = stdio.readFloat() rbound2 = stdio.readFloat() rectangles += [Rectangle(Interval(lbound1, rbound1), Interval(lbound2, rbound2))] for i in range(len(rectangles)): stdio.writef('Area(%s) = %f\n', rectangles[i], rectangles[i].area()) stdio.writef('Perimeter(%s) = %f\n', rectangles[i], rectangles[i].perimeter()) if rectangles[i].contains(x, y): stdio.writef('%s contains (%f, %f)\n', rectangles[i], x, y) for i in range(len(rectangles)): for j in range(i + 1, len(rectangles)): if rectangles[i].intersects(rectangles[j]): stdio.writef('%s intersects %s\n', rectangles[i], rectangles[j])
def main(): n = 0 var = 0.00 while not stdio.isEmpty(): a = stdio.readFloat() * (0.175 * (10**-6)) var += a * a n += 1 var = var / (2 * n) eta = 9.135 * 10**-4 rho = 0.5 * 10**-6 T = 297.0 R = 8.31457 k = 6 * math.pi * var * eta * rho / T N_A = R / k stdio.writef('Boltzman = %e\nAvogadro = %e\n', k, N_A)
def main(): n = 0 Var = .00 while not stdio.isEmpty(): a = stdio.readFloat() * (.175 * (10**-6)) Var += a * a n += 1 Var = Var / (2 * n) VIS = 9.135 * 10**-4 RO = 0.5 * 10**-6 T = 297.0 R = 8.31457 k = 6 * (math.pi * Var * VIS * RO) / T N_A = R / k stdio.writef('Boltzman = %e\nAvogadro = %e\n', k, N_A)
def __init__(self): self._N = stdio.readInt() # Number of bodies self._radius = stdio.readFloat() # Radius of universe # the set scale for drawing on screen stddraw.setXscale(-self._radius, +self._radius) stddraw.setYscale(-self._radius, +self._radius) # read in the _N bodies self.orbs = [] # Array of bodies for i in range(self._N): rx = stdio.readFloat() ry = stdio.readFloat() vx = stdio.readFloat() vy = stdio.readFloat() mass = stdio.readFloat() position = [ rx, ry ] velocity = [ vx, vy ] r = vector.Vector(position) v = vector.Vector(velocity) self.orbs += [body.Body(r, v, mass)]
def __init__(self): self._N = stdio.readInt() # Number of bodies self._radius = stdio.readFloat() # Radius of universe # the set scale for drawing on screen stddraw.setXscale(-self._radius, +self._radius) stddraw.setYscale(-self._radius, +self._radius) # read in the _N bodies self.orbs = [] # Array of bodies for i in range(self._N): rx = stdio.readFloat() ry = stdio.readFloat() vx = stdio.readFloat() vy = stdio.readFloat() mass = stdio.readFloat() position = [rx, ry] velocity = [vx, vy] r = vector.Vector(position) v = vector.Vector(velocity) self.orbs += [body.Body(r, v, mass)]
#----------------------------------------------------------------------- # average.py #----------------------------------------------------------------------- import stdio # Read floats from the standard input stream until end-of-file. # Write to standard output the average of those floats. total = 0.0 count = 0 while not stdio.isEmpty(): value = stdio.readFloat() total += value count += 1 avg = total / count stdio.writeln('Average is ' + str(avg)) #----------------------------------------------------------------------- # python average.py # 10.0 5.0 6.0 # 3.0 # 7.0 32.0 # Average is 10.5 # python randomseq.py 1000 > data.txt # python average.py < data.txt # Average is 0.49134854771784825
import picture as p # newtons law of universal gravitation # F = G*((m1*m2)/r**2)) # Kommandozeilenparameter time = float(sys.argv[1]) delta_t = float(sys.argv[2]) g = 6.67e-11 bodies = [] current_time = 0 # Variablen vom Standardinput num_bodies = stdio.readInt() radius_univ = stdio.readFloat() # Canvas vorbereiten stddraw.setXscale(-radius_univ, radius_univ) stddraw.setYscale(-radius_univ, radius_univ) stddraw.setCanvasSize(1200, 1000) stddraw.clear(stddraw.BLACK) def force(mass_a, mass_b, position_a, position_b, g): delta = position_b - position_a # euklidische Distanz r = math.sqrt(delta[0]**2 + delta[1]**2) f = (g * mass_a * mass_b) / r**2 force = f * (delta / r) return force
# random surfer lands on each page (page ranks) after moves # matrix-vector multiplies, and write the page ranks to standard # output. moves = int(sys.argv[1]) n = stdio.readInt() stdio.readInt() # Discard the second int of standard input. # Read the transition matrix from standard input. # probs[i][j] is the probability that the surfer moves from # page i to page j. probs = stdarray.create2D(n, n, 0.0) for i in range(n): for j in range(n): probs[i][j] = stdio.readFloat() # Use the power method to compute page ranks. ranks = stdarray.create1D(n, 0.0) ranks[0] = 1.0 for i in range(moves): # Compute effect of next move on page ranks. newRanks = stdarray.create1D(n, 0.0) for j in range(n): # New rank of page j is dot product # of old ranks and column j of probs. for k in range(n): newRanks[j] += ranks[k] * probs[k][j] ranks = newRanks # Write the page ranks.
NOTES_ON_SCALE = 12.0 hz = CONCERT_A_HZ * (2.0 ** (pitch / NOTES_ON_SCALE)) a = tone(hz, t) hi = tone(2*hz, t) lo = tone(hz/2, t) h = superpose(hi, lo, .5, .5) return superpose(a, h, .5, .5) #----------------------------------------------------------------------- # Read sound samples from standard input, add harmonics, and play # the resulting the sound to standard audio. while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() a = note(pitch, duration) stdaudio.playArray(a) stdaudio.wait() #----------------------------------------------------------------------- # python playthattunedeluxe.py < ascale.txt # python playthattunedeluxe.py < elise.txt # python playthattunedeluxe.py < entertainer.txt # python playthattunedeluxe.py < firstcut.txt # python playthattunedeluxe.py < freebird.txt
# Accept three float command-line arguments x, y, z. Read from standard # input a sequence of point coordinates (xi, yi, zi), and write to # standard output the coordinates of the point closest to (x, y, z). x = float(sys.argv[1]) y = float(sys.argv[2]) z = float(sys.argv[3]) if stdio.isEmpty(): stdio.writeln('No points provided') sys.exit() bestDist2 = float('inf') while not stdio.isEmpty(): xi = stdio.readFloat() yi = stdio.readFloat() zi = stdio.readFloat() dist2 = (x - xi) * (x - xi) + (y - yi) * (y - yi) + \ (z - zi) * (z - zi) if dist2 < bestDist2: bestx = xi besty = yi bestz = zi bestDist2 = dist2 # Write the results. stdio.writef('Closest point = (%f, %f, %f)\n', bestx, besty, bestz) #----------------------------------------------------------------------- #
import math import stdio if __name__ == '__main__': n = 0 s = 0.00 while not stdio.isEmpty(): convert_to_m = stdio.readFloat() * ( 0.175 * (10**-6)) # Convert input parameters to meters s += convert_to_m**2 n += 1 # count beads var = s / (2 * n) viscosity = 9.135 * 10**-4 r_bead = 0.5 * 10**-6 # Radius of willow T = 297.0 # Absolute temperature R = 8.31446 # Global gas constant k = 6 * math.pi * var * viscosity * r_bead / T # Boltzman Avogadro = R / k print('Boltzman = {0:.4e}'.format(k)) print('Avogadro = {0:.4e}'.format(Avogadro))
from charge import Charge from color import Color from picture import Picture # Read values from standard input to create an array of charged # particles. Set each pixel color in an image to a grayscale value # proportional to the total of the potentials due to the particles at # corresponding points. Draw the resulting image to standard draw. MAX_GRAY_SCALE = 255 # Read charges from standard input into an array. n = stdio.readInt() charges = stdarray.create1D(n) for i in range(n): x0 = stdio.readFloat() y0 = stdio.readFloat() q0 = stdio.readFloat() charges[i] = Charge(x0, y0, q0) # Create a Picture depicting potential values. pic = Picture() for col in range(pic.width()): for row in range(pic.height()): # Compute pixel color. x = 1.0 * col / pic.width() y = 1.0 * row / pic.height() v = 0.0 for i in range(n): v += charges[i].potentialAt(x, y)
# transition matrix from standard input. Perform moves moves as # prescribed by the transition matrix, and write to standard output # the relative frequency of hitting each page. moves = int(sys.argv[1]) n = stdio.readInt() stdio.readInt() # Discard the second int of standard input. # Read the transition matrix from standard input. # p[i][j] is the probability that the surfer moves from # page i to page j. p = stdarray.create2D(n, n, 0.0) for i in range(n): for j in range(n): p[i][j] = stdio.readFloat() # Perform the simulation, thus computing the hits array. # hits[i] is the number of times the surfer hits page i. hits = stdarray.create1D(n, 0) page = 0 # Start at page 0. for i in range(moves): # Make one random move. r = random.random() sum = 0.0 for j in range(0, n): # Find interval containing r. sum += p[page][j] if r < sum: page = j break
#----------------------------------------------------------------------- # 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()
# values x from standard input, and write to standard output the # polynomial evaluated at x. Also write to standard output the # value computed by math.exp(x). n = int(sys.argv[1]) # Compute coeffients for Taylor series # e^x = 1 + x + x^2/2! + x^3/3! + ... a = stdarray.create1D(n, 0.0) a[0] = 1.0 for i in range(1, n): a[i] = a[i - 1] / float(i) # Evaluate the polynomial at values x read from standard input. while not stdio.isEmpty(): x = stdio.readFloat() stdio.writeln(evaluate(x, a)) stdio.writeln(math.exp(x)) stdio.writeln() #----------------------------------------------------------------------- # python horner.py 30 # 0 # 1.0 # 1.0 # # 1 # 2.718281828459045 # 2.718281828459045 #
# transition matrix from standard input. Perform moves moves as # prescribed by the transition matrix, and write to standard output # the relative frequency of hitting each page. moves = int(sys.argv[1]) n = stdio.readInt() stdio.readInt() # Discard the second int of standard input. # Read the transition matrix from standard input. # p[i][j] is the probability that the surfer moves from # page i to page j. p = stdarray.create2D(n, n, 0.0) for i in range(n): for j in range(n): p[i][j] = stdio.readFloat() # Perform the simulation, thus computing the hits array. # hits[i] is the number of times the surfer hits page i. hits = stdarray.create1D(n, 0) step = stdarray.create1D(n, 0) now = stdarray.create1D(n, 0) page = 0 # Start at page 0. for i in range(moves): # Make one random move. r = random.random() total = 0.0 for j in range(0, n): # Find interval containing r. total += p[page][j] if r < total:
# standard input a sequence of coordinates (x_i, y_i, z_i), and writes the # coordinates of the point closest to (x, y, z). import stdio import sys # Read x, y, and z from command line, as floats. x = float(sys.argv[1]) y = float(sys.argv[2]) z = float(sys.argv[3]) # Closest squared-distance so far, initialized to infinity. bestD = float('inf') # Read coordinates (xi, yi, zi) from standard input and calculate its # squared-distance to the point (x, y, z). Check if that value is smaller # than bestDist2, and if so, update bestDist2 to the new value, and # let (bestx, besty, bestz) be (xi, yi, zi). while stdio.isEmpty() is False: x1 = stdio.readFloat() y1 = stdio.readFloat() z1 = stdio.readFloat() if (((x - x1)**2 + (y - y1)**2 + (z - z1)**2) < bestD): cx = x1 cy = y1 cz = z1 bestD = ((x - x1)**2 + (y - y1)**2 + (z - z1)**2) # Write the closest point (bestx, besty, bestz). stdio.writef('closest point = (%f, %f, %f)\n', cx, cy, cz)
import stdio a = stdio.readFloat() b = stdio.readFloat() c = stdio.readFloat() d = stdio.readFloat() DTB = ((a + c) * 2 + b + d) / 6 if DTB >= 9: if a > 6.5 and b > 6.5 and c > 6.5 and d > 6.5: stdio.writeln('XS') else: stdio.writeln('G') else: if DTB >= 8: if a > 5 and b > 5 and c > 5 and d > 5: stdio.writeln('G') else: stdio.writeln('K') else: if DTB >= 6.5: if a > 3 and b > 3 and c > 3 and d > 3: stdio.writeln('K') else: stdio.writeln('TB') else: if DTB >= 5: if a > 2 and b > 2 and c > 2 and d > 2: stdio.writeln('TB') else: stdio.writeln('Y') else:
#----------------------------------------------------------------------- import stdio import sys import math # Accept integer command-line argument n. Then read n floats from # standard input, and write their mean and standard deviation to # standard output. n = int(sys.argv[1]) # Read the floats. a = [] for i in range(n): a += [stdio.readFloat()] # Compute the mean. total = 0.0 for element in a: total += element mean = total / float(n) # Compute the standard deviation. total2 = 0.0 for element in a: total2 += (element - mean)**2 stddev = math.sqrt(total2) / float(n - 1) # Write the results. stdio.writeln('Mean = ' + str(mean))
import math import stdio # this is new! import stdaudio SPS = 44100 CONCERT_A = 440.0 NOTES_ON_SCALE = 12.0 while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = CONCERT_A * (2.0**(pitch / NOTES_ON_SCALE)) n = int(SPS * duration) note = [0.0] * (n + 1) for i in range(n + 1): note[i] = math.sin(2.0 * math.pi * i * hz / SPS) stdaudio.playSamples(note) stdaudio.wait()
def _main(): n = int(sys.argv[1]) x = [stdio.readFloat() for i in range(n)] y = [stdio.readFloat() for i in range(n)] stdio.writeln(distance(x, y))
import stdio stdio.write('Mời bạn nhập điểm giữa kì: ') Dgiuaki = stdio.readFloat() stdio.write('Mới bạn nhập điểm cuối kỳ: ') Dcuoiki = stdio.readFloat() stdio.write('Mới bạn nhập số buổi vắng: ') Solanvang = stdio.readInt() Dcc = 10 - Solanvang Dqt = Dcc * 0.3 + Dgiuaki * 0.7 if Solanvang > 3 or Dqt < 4: stdio.writeln('Cấm thi') else: Dtb = Dqt * 0.3 + Dcuoiki * 0.7 stdio.writeln('Điểm trung bình: %s' % Dtb) if Dtb >= 9: stdio.writeln('Xuất sắc') elif Dtb >= 8: stdio.writeln('Giỏi') elif Dtb >= 6.5: stdio.writeln('Khá') elif Dtb >= 4: stdio.writeln('Trung bình') elif Dtb < 4: stdio.writeln('Kém')