def main(args): """Reads in a social network from a file, and then repeatedly reads in individuals from standard input and prints out their degrees of separation. Takes three command-line arguments: the name of a file, a delimiter, and the name of the distinguished individual. Each line in the file contains the name of a vertex, followed by a list of the names of the vertices adjacent to that vertex, separated by the delimiter. :param args: the command-line arguments """ filename = args[1] delimiter = args[2] source = args[3] sg = SymbolGraph(filename, delimiter) G = sg.graph() if not sg.contains(source): stdio.writeln("{} not in database".format(source)) return s = sg.index_of(source) bfs = BreadthFirstPaths(G, s) while not stdio.isEmpty(): sink = stdio.readLine() if sg.contains(sink): t = sg.index_of(sink) if bfs.has_path_to(t): for v in bfs.path_to(t): stdio.writef("\t%s\n", sg.name_of(v)) else: stdio.writeln("\tNot connected") else: stdio.writeln("\tNot in database.")
def evaluate(): ops = Stack() vals = Stack() while not stdio.isEmpty(): # Read token, push if operator s = stdio.readString() if s == "(": pass elif s == "+": ops.push(s) elif s == "-": ops.push(s) elif s == "*": ops.push(s) elif s == "/": ops.push(s) elif s == "sqrt": ops.push(s) elif s == ")": # Pop, evaluate and push result if token is ")" op = ops.pop() v = vals.pop() if op == "+": v = vals.pop() + v elif op == "-": v = vals.pop() - v elif op == "*": v = vals.pop() * v elif op == "/": v = vals.pop() / v elif op == "sqrt": v = math.sqrt(v) vals.push(v) else: vals.push(float(s)) stdio.writeln(vals.pop())
def main(): """Reads strings from first input file and sorts them Reads strings from second input file and prints every string not in first input file.""" if len(sys.argv) == 3: sys.stdin = open(sys.argv[1]) arr = stdio.readAllStrings() arr.sort() sys.stdin = open(sys.argv[2]) while not stdio.isEmpty(): key = stdio.readString() if index_of(arr, key) == -1: print(key)
def main(): """ Reads strings from stdin and adds them to a priority queue. When reading a '-' it removes a maximum item on the priority queue and prints it to stdout. Prints the amount of items left on the priority queue """ pq = MaxPQ() while not stdio.isEmpty(): item = stdio.readString() if item != '-': pq.insert(item) elif not pq.is_empty(): print(pq.del_max()) print("({} left on pq)".format(pq.size()))
def main(): """Reads strings from stdin and adds them to a minimum priority queue. When reading a '-' it removes the minimum element and prints it to stdout. """ pq = MinPQ() while not stdio.isEmpty(): item = stdio.readString() if item != "-": pq.insert(item) elif not pq.is_empty(): print(pq.del_min()) print("({} left on pq)".format(pq.size()))
i = 1 while i < self.size(): if self._keys[i] < self._keys[i - 1]: return False i += 1 return True def _rank_check(self): # check that rank(select(i)) = i for i in range(self.size()): if i != self.rank(self.select(i)): return False for i in range(self.size()): if self._keys[i] != self.select(self.rank(self._keys[i])): return False return True if __name__ == "__main__": from itu.algs4.stdlib import stdio st = BinarySearchST() i = 0 while not stdio.isEmpty(): key = stdio.readString() st.put(key, i) i += 1 for s in st.keys(): stdio.writef("%s %i\n", s, st.get(s))