Ejemplo n.º 1
0
def readInt1D():
    """
    Read from sys.stdin and return an array of integers. 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.readInt()
    return a
Ejemplo n.º 2
0
def readInt2D():
    """
    Read from sys.stdin and return a two-dimensional array of integers.
    Two integers at the beginning of sys.stdin define the array's
    dimensions.
    """
    rowCount = stdio.readInt()
    colCount = stdio.readInt()
    a = create2D(rowCount, colCount, 0)
    for row in range(rowCount):
        for col in range(colCount):
            a[row][col] = stdio.readInt()
    return a
Ejemplo n.º 3
0
    of the exchange table and then finding a negative cycle in the digraph.
    
    This implementation uses the Bellman-Ford algorithm to find a negative cycle in 
    the complete digraph. The running time is proportional to V3 in the worst case, 
    where V is the number of currencies.

    For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
    """
    if len(sys.argv) > 1:
        try: 
            sys.stdin = open(sys.argv[1])
        except IOError:
            print("File not found, using standard input instead")
    
    # V currencies
    V = stdio.readInt()
    name = [None]*V
    
    # Create complete network
    graph = EdgeWeightedDigraph(V)
    for v in range(V):
        name[v] = stdio.readString()
        for w in range(V):
            rate = stdio.readFloat()
            edge = DirectedEdge(v, w, -math.log(rate))
            graph.add_edge(edge)
    
    # find negative cycle
    spt = BellmanFordSP(graph, 0)
    if spt.has_negative_cycle():
        stake = 1000.0
Ejemplo n.º 4
0
        """
        self._validate(p)
        self._validate(q)
        return self._id[p] == self._id[q]

    def count(self):
        return self._count


# Reads in a an integer n and a sequence of pairs of integers
# (between 0 and n-1) from standard input or a file
# supplied as argument to the program, where each integer
# in the pair represents some site; if the sites are in different
# components, merge the two components and print the pair to standard output.
if __name__ == "__main__":
    if len(sys.argv) > 1:
        try:
            sys.stdin = open(sys.argv[1])
        except IOError:
            print("File not found, using standard input instead")
    n = stdio.readInt()
    uf = UF(n)
    while not stdio.isEmpty():
        p = stdio.readInt()
        q = stdio.readInt()
        if uf.connected(p, q):
            continue
        uf.union(p, q)
        print("{} {}".format(p, q))
    print("number of components: {}".format(uf.count()))
Ejemplo n.º 5
0
from itu.algs4.sorting.max_pq import MaxPQ
#from itu.algs4.sorting.heap import sort
from itu.algs4.sorting.index_min_pq import IndexMinPQ
from itu.algs4.stdlib.stdio import readString, readInt
import sys

p = readInt()
# parties
s = readInt()
# seats
maxPQ = MaxPQ()
partiesdict = {}
plist = []
tempPartylist = []
qu = 0
votes = 0


def quotient(qu, nextP):
    if nextP > qu:
        return True
    else:
        #rearrange()
        return False


def rearrange():
    tempPartylist.clear()
    for i in range(p):
        maxPQ.insert(plist[i])
        #tempPartylist.append(i)
Ejemplo n.º 6
0
from itu.algs4.sorting.insertion_sort import sort
from itu.algs4.stdlib.stdio import readString, readInt
import sys

m = readInt()
list = []

for i in range(m):
    n = readString()
    g = readString()
    list.append([g, n])
    # print("name: ", n,"grade: ", g)

list.sort()

for i in list:
    print(i[-1])