#!/usr/bin/env python3 # Author: Dr. Robert Heckendorn, Computer Science Department, University of Idaho, 2013 from copy import * ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # # LINK A DIGRAPH AS A GRAPH by adding backedges for every # directed edge. def relink(g) : for i in range(0, len(g)) : for j in g[i] : if i!=j : g[j].add(i) # prevent the add from changing g[i] return g # note that g was changed and really doesn't have to be returned ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # # SIMPLE ARRAY # # simple utility 1D array function def array(size, value) : a = [] for i in range(0, size) : a.append(deepcopy(value)) return a ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # # STACK AND QUEUE # # This provides very simple stack and queue operations # LEFT SIDE def pushLeft(l, item) : l.insert(0, item) def popLeft(l) : if len(l)==0 : print("ERROR(popLeft): no elements in list") return None tmp = l[0] del l[0] return tmp def topLeft(l) : if len(l)==0 : print("ERROR(topLeft): no elements in list") return None return l[0] # RIGHT SIDE def pushRight(l, item) : l.append(item) def popRight(l) : if len(l)==0 : print("ERROR(popRight): no elements in list") return None return l.pop() def topRight(l) : if len(l)==0 : print("ERROR(topRight): no elements in list") return None return l[-1] # z = ["ant", "bat", "cow"] # print(z) # print("R:", topRight(z)) # print("L:", topLeft(z)) # pushLeft(z, "aardvark") # pushRight(z, "dog") # print(z) # print(popLeft(z)) # print(z) # print(popRight(z)) # print(z) # print(popLeft(z)) # print(z) # print(popRight(z)) # print(z) # print(popLeft(z)) # print(z) # print(popRight(z)) # print(z) # print("R:", topRight(z)) # print("L:", topLeft(z))