def write1D(a): """ Write array a to sys.stdout. First write its length. bool objects are written as 0 and 1, not False and True. """ length = len(a) stdio.writeln(length) for i in range(length): # stdio.writef('%9.5f ', a[i]) element = a[i] if isinstance(element, bool): if element == True: stdio.write(1) else: stdio.write(0) else: stdio.write(element) stdio.write(' ') stdio.writeln()
def write2D(a): """ Write two-dimensional array a to sys.stdout. First write its dimensions. bool objects are written as 0 and 1, not False and True. """ rowCount = len(a) colCount = len(a[0]) stdio.writeln(str(rowCount) + ' ' + str(colCount)) for row in range(rowCount): for col in range(colCount): #stdio.writef('%9.5f ', a[row][col]) element = a[row][col] if isinstance(element, bool): if element == True: stdio.write(1) else: stdio.write(0) else: stdio.write(element) stdio.write(' ') stdio.writeln()
return path def _validateVertex(self, v): # throw an ValueError unless 0 <= v < V V = len(self._marked) if v < 0 or v >= V: raise ValueError("vertex {} is not between 0 and {}".format( v, V - 1)) if __name__ == "__main__": from algs4.stdlib import stdio from algs4.graphs.graph import Graph from algs4.stdlib.instream import InStream import sys In = InStream(sys.argv[1]) G = Graph.from_stream(In) s = int(sys.argv[2]) dfs = DepthFirstPaths(G, s) for v in range(G.V()): if dfs.has_path_to(v): stdio.writef("%d to %d: ", s, v) for x in dfs.path_to(v): if x == s: stdio.write(x) else: stdio.writef("-%i", x) stdio.writeln() else: stdio.writef("%d to %d: not connected\n", s, v)
def show(array): stdio.write(" ".join(array))
if self.n > 0 and self.n == len(self.a)/4: self.resize(len(self.a)/2) return item def __iter__(self): i = self.n -1 while i >= 0: yield self.a[i] i -= 1 if __name__ == '__main__': import sys from algs4.stdlib import stdio if len(sys.argv) > 1: try: sys.stdin = open(sys.argv[1]) except IOError: print("File not found, using standard input instead") stack = Stack() while not stdio.isEmpty(): item = stdio.readString() if not item == "-": stack.push(item) elif not stack.is_empty(): stdio.write(stack.pop() + " ") stdio.writef("(%i left on stack)\n", stack.size())
def _regressionTest(): """ Perform regression testing. """ clear() setPenRadius(.5) setPenColor(ORANGE) point(0.5, 0.5) show(0.0) setPenRadius(.25) setPenColor(BLUE) point(0.5, 0.5) show(0.0) setPenRadius(.02) setPenColor(RED) point(0.25, 0.25) show(0.0) setPenRadius(.01) setPenColor(GREEN) point(0.25, 0.25) show(0.0) setPenRadius(0) setPenColor(BLACK) point(0.25, 0.25) show(0.0) setPenRadius(.1) setPenColor(RED) point(0.75, 0.75) show(0.0) setPenRadius(0) setPenColor(CYAN) for i in range(0, 100): point(i / 512.0, .5) point(.5, i / 512.0) show(0.0) setPenRadius(0) setPenColor(MAGENTA) line(.1, .1, .3, .3) line(.1, .2, .3, .2) line(.2, .1, .2, .3) show(0.0) setPenRadius(.05) setPenColor(MAGENTA) line(.7, .5, .8, .9) show(0.0) setPenRadius(.01) setPenColor(YELLOW) circle(.75, .25, .2) show(0.0) setPenRadius(.01) setPenColor(YELLOW) filledCircle(.75, .25, .1) show(0.0) setPenRadius(.01) setPenColor(PINK) rectangle(.25, .75, .1, .2) show(0.0) setPenRadius(.01) setPenColor(PINK) filledRectangle(.25, .75, .05, .1) show(0.0) setPenRadius(.01) setPenColor(DARK_RED) square(.5, .5, .1) show(0.0) setPenRadius(.01) setPenColor(DARK_RED) filledSquare(.5, .5, .05) show(0.0) setPenRadius(.01) setPenColor(DARK_BLUE) polygon([.4, .5, .6], [.7, .8, .7]) show(0.0) setPenRadius(.01) setPenColor(DARK_GREEN) setFontSize(24) text(.2, .4, 'hello, world') show(0.0) #import picture as p #pic = p.Picture('saveIcon.png') #picture(pic, .5, .85) #show(0.0) # Test handling of mouse and keyboard events. setPenColor(BLACK) from algs4.stdlib import stdio stdio.writeln('Left click with the mouse or type a key') while True: if mousePressed(): filledCircle(mouseX(), mouseY(), .02) if hasNextKeyTyped(): stdio.write(nextKeyTyped()) show(0.0) # Never get here. show()