def main(fin, prt=sys.stdout): """Unit tests the TST data type.""" # build symbol table from standard input arr = cli_get_array(fin) print(arr) st = TST() for i, key in enumerate(arr): st.put(key, i) # print results if st.size() < 100: prt.write('keys(""):') for key in st.keys(): prt.write("{} {}".format(key, st.get(key))) prt.write("\n") prt.write('longestPrefixOf("shellsort"):') prt.write(st.longestPrefixOf("shellsort")) prt.write("\n") prt.write('keysWithPrefix("shor"):') for s in st.keysWithPrefix("shor"): prt.write(s) prt.write("\n") prt.write('keysThatMatch(".he.l."):') for s in st.keysThatMatch(".he.l."): prt.write(s)
def main(seqinfo, prt=sys.stdout): """LSD radix sorts fixed-length strings and prints them in ascending order.""" a = cli_get_array(seqinfo) a = list(itertools.chain(*a)) # Flatten list N = len(a) sort(a) # sort the strings prt.write("{}\n".format(' '.join(a)))
def main(fin_G, prt=sys.stdout): a = cli_get_array(fin_G) G = EdgeWeightedGraph(a) mst = LazyPrimMST(G) for e in mst.edges(): prt.write(e) prt.write("{:.5f}\n".format(mst.weight()))
def main(prt=sys.stdout): a = cli_get_array() G = EdgeWeightedGraph(a) mst = KruskalMST(G) for e in mst.edges(): prt.write(e) prt.write("{:.5f}\n".format(mst.weight()))
def main(fin, prt=sys.stdout): """Unit tests the TST data type.""" # build symbol table from standard input arr = cli_get_array(fin) print arr st = TST() for i, key in enumerate(arr): st.put(key, i) # print results if st.size() < 100: prt.write('keys(""):') for key in st.keys(): prt.write("{} {}".format(key, st.get(key))) prt.write("\n") prt.write('longestPrefixOf("shellsort"):') prt.write(st.longestPrefixOf("shellsort")) prt.write("\n") prt.write('keysWithPrefix("shor"):') for s in st.keysWithPrefix("shor"): prt.write(s) prt.write("\n") prt.write('keysThatMatch(".he.l."):') for s in st.keysThatMatch(".he.l."): prt.write(s)
def main(): # read in the data a = cli_get_array("a b c d e f") # shuffle the array shuffle(a) # print results. sys.stdout.write('{}\n'.format(' '.join(map(str, a))))
def run(container, seqstr, expected=None, prt=sys.stdout, details=sys.stdout): """Run a sequence of Stack commands.""" lst = cli_get_array(seqstr) result = run_list(container, lst, details) if expected is not None: pf = result == expected pass_fail = " Pass" if pf else " **FAIL" # prt.write("{}: EXP({}) ACTUAL({}) from seq: {}\n".format( prt.write("{PF} GIVEN({SEQ}) -> ACTUAL({ACT})".format(PF=pass_fail, EXP=expected, ACT=result, SEQ=seqstr)) if not pf: prt.write(" EXPECTED({})".format(expected)) prt.write("\n")
def run(container, seqstr, expected=None, prt=sys.stdout, details=sys.stdout): """Run a sequence of Stack commands.""" lst = cli_get_array(seqstr) result = run_list(container, lst, details) if expected is not None: pf = result == expected pass_fail = " Pass" if pf else " **FAIL" #prt.write("{}: EXP({}) ACTUAL({}) from seq: {}\n".format( prt.write("{PF} GIVEN({SEQ}) -> ACTUAL({ACT})".format( PF=pass_fail, EXP=expected, ACT=result, SEQ=seqstr)) if not pf: prt.write(" EXPECTED({})".format(expected)) prt.write("\n")
def main(s=0, prt=sys.stdout): a = cli_get_array() G = EdgeWeightedGraph(a) prt.write(str(G)) # find shortest path from s to each other vertex in DAG sp = AcyclicSP(G, s) for v in range(G.V()): if sp.hasPathTo(v): prt.write("{} to {} ({:.2f}) ".format(s, v, sp.distTo(v))) for e in sp.pathTo(v): prt.write("{} ".format(e)) prt.write("\n") else: prt.write("{} to {} no path\n".format(s, v))
def main(prt=sys.stdout): a = cli_get_array() G = Digraph(a) s = 0 dfs = DepthFirstDirectedPaths(G, s) prt.write("{}\n".format(G)) for v in range(G.V()): if dfs.hasPathTo(v): prt.write("{SRC} to {V}: ".format(SRC=s, V=v)) for x in dfs.pathTo(v): if x == s: prt.write(str(x)) else: prt.write("-{X}".format(X=x)) prt.write("\n") else: prt.write("{SRC} to {V}: not connected\n".format(SRC=s, V=v))
def test_0(prt=sys.stdout): item_list = cli_get_array("tinyST.txt") print item_list st = BST() for i, key in enumerate(item_list): st.put(key, i) png = "BST_{I}_{K}.png".format(I=i, K=key) st.wr_png(png) prt.write("LEVEL ORDER:\n") for s in st.levelOrder(): prt.write("{S} {ST}\n".format(S=s, ST=st.get(s))) prt.write("\n") prt.write("KEYS:\n") for s in st.keys(): prt.write("{S} {ST}\n".format(S=s, ST=st.get(s))) st.wr_png("tinyST.png")
def main(seqinfo, prt=sys.stdout): """MSD radix sorts sequence of extended ASCII strings and prints in ascending order.""" a = cli_get_array(seqinfo) N = len(a) sort(a) prt.write("{}\n".format(a))
def default_example(): """Run simple example.""" run(cli_get_array("a b c d - - e f - - g h i -")) run(cli_get_array("a - b - c d - - e f - - g h i -"))
"""Test ResizingArrayQueue.py.""" from AlgsSedgewickWayne.ResizingArrayQueue import ResizingArrayQueue from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array import sys def run(item_list): """Unit tests the ResizingArrayQueue data type.""" sys.stdout.write("\nRUNNING: {}\n".format(' '.join(item_list))) q = ResizingArrayQueue() for item in item_list: if item != "-": q.enqueue(item) else: sys.stdout.write(" DEQUEUE: {}\n".format(q.dequeue())) sys.stdout.write("({} left on queue): {}\n".format(q.size(), q)) def default_example(): """Run simple example.""" run(cli_get_array("a b c d - - e f - - g h i -")) run(cli_get_array("a - b - c d - - e f - - g h i -")) if __name__ == '__main__': # If user did not provide a sequence. if len(sys.argv) == 1: default_example() # If user provided a sequence in the runtime arguments. else: run(cli_get_array())
def wr_png(bh_arrstr, **kwargs): wr_png_array(cli_get_array(bh_arrstr), kwargs)
#!/usr/bin/env python from AlgsSedgewickWayne.Quick3way import Sort from AlgsSedgewickWayne.testcode.ArrayHistory import ArrayHistory from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array import sys def run(a, prt=sys.stdout): desc = 'QUICK3WAY' ah = ArrayHistory() # Do not shuffle so that we may better visualize _sort actions Sort(a, array_history=ah, shuffle=False) ah.prt() #ah.show(desc) prt.write("{DESC} RESULT: {A}\n".format(DESC=desc, A=' '.join([str(e) for e in a]))) if __name__ == '__main__': run(cli_get_array("P A B X W P P V P D P C Y Z"))
def cli(): N = len(sys.argv) if N == 1: run_all() elif N == 2: run(cli_get_array())
#!/usr/bin/env python """Tests the Bag class.""" import sys from AlgsSedgewickWayne.Bag import Bag from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array def run(item_list): """Add items to a Bad. Iterate through Bag.""" bag = Bag() for item in item_list: bag.add(item) sys.stdout.write("size of bag = {}\n".format(bag.size())) for s in bag: sys.stdout.write(" BAG CONTAINS: {}\n".format(s)) if __name__ == '__main__': run(cli_get_array("0 1 2 3 4 5 6 7 8 9"))
def run_Queue(container, seqstr, prt=sys.stdout): """Inserts items in a string into a container.""" return run_Queue_list(container, cli_get_array(seqstr), prt)
#!/usr/bin/env python from AlgsSedgewickWayne.Quick3way import Sort from AlgsSedgewickWayne.testcode.ArrayHistory import ArrayHistory from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array import sys def run(a, prt=sys.stdout): desc = 'QUICK3WAY' ah = ArrayHistory() # Do not shuffle so that we may better visualize _sort actions Sort(a, array_history=ah, shuffle=False) ah.prt() #ah.show(desc) prt.write("{DESC} RESULT: {A}\n".format( DESC=desc, A=' '.join([str(e) for e in a]))) if __name__ == '__main__': run(cli_get_array("P A B X W P P V P D P C Y Z"))
#!/usr/bin/env python from AlgsSedgewickWayne.Quick import _partition from AlgsSedgewickWayne.testcode.ArrayHistory import ArrayHistory from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array import sys def run(a, prt=sys.stdout): desc = 'QUICKSORT PARTITION' ah = ArrayHistory() j = _partition(a, lo=0, hi=len(a)-1, array_history=ah) ah.prt() ah.show(desc) print desc, "RESULT:", ' '.join(str(e) for e in a) if __name__ == '__main__': run(cli_get_array("13 16 40 60 19 70 71 47 12 67"))
#!/usr/bin/env python from AlgsSedgewickWayne.Quick import Sort from AlgsSedgewickWayne.testcode.ArrayHistory import ArrayHistory from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array def run(a): desc = 'QUICKSORT' ah = ArrayHistory() Sort(a, array_history=ah) ah.prt() ah.show(desc) print(desc, "RESULT:", a) if __name__ == '__main__': run(cli_get_array("13 16 40 60 19 70 71 47 12 67"))
def main(prt=sys.stdout): a = cli_get_array() G = EdgeWeightedGraph(a) prt.write(str(G))
from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array import sys def run(item_list): """Unit tests the ResizingArrayQueue data type.""" sys.stdout.write("\nRUNNING: {}\n".format(' '.join(item_list))) q = ResizingArrayQueue() for item in item_list: if item != "-": q.enqueue(item) else: sys.stdout.write(" DEQUEUE: {}\n".format(q.dequeue())) sys.stdout.write("({} left on queue): {}\n".format(q.size(), q)) def default_example(): """Run simple example.""" run(cli_get_array("a b c d - - e f - - g h i -")) run(cli_get_array("a - b - c d - - e f - - g h i -")) if __name__ == '__main__': # If user did not provide a sequence. if len(sys.argv) == 1: default_example() # If user provided a sequence in the runtime arguments. else: run(cli_get_array())