Exemple #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
Exemple #2
0
def readBool2D():
    """
    Read from sys.stdin and return a two-dimensional array of booleans.
    Two integers at the beginning of sys.stdin define the array's
    dimensions.
    """
    rowCount = stdio.readInt()
    colCount = stdio.readInt()
    a = create2D(rowCount, colCount, False)
    for row in range(rowCount):
        for col in range(colCount):
            a[row][col] = stdio.readBool()
    return a
    def count(self):
        return self._count

import sys
import time
from algs4.stdlib import stdio

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")
    start_time = time.time()
    n = stdio.readInt()
    quf = QuickFindUF(n)
    while not stdio.isEmpty():
        p = stdio.readInt()
        q = stdio.readInt()
        if quf.connected(p, q):
            continue
        quf.union(p, q)	
    
    for i in range(n):
        print(quf._id[i])
    
    if quf.connected(1, 0):
        sys.stdout.write('True\n')
        #print("--- %s seconds ---" % (time.time() - start_time))
    else:
Exemple #4
0
from algs4.stdlib import stdio
from priority_queue import MaxPQ, Node
import math
import sys

pq = MaxPQ()

states = stdio.readInt()                                            # assign the amount of states
total_seats = stdio.readInt()                                       # assign the amount of seats
allocated_seats = states
remaining_seats = total_seats -allocated_seats
 

while not stdio.isEmpty():                                          # make sure our file still has values 
    
    state = str(stdio.readLine()).strip('\n')                       # assign statename and population count 
    population = int(stdio.readLine())
    priority = population / math.sqrt(1*(1+1))
    
    initial_seat = 1                                                # initialize 1 seat per state
    current_node = Node(state, priority, population, initial_seat)
    pq.insert(current_node)
    

while allocated_seats < total_seats:                                # run untill we have no more seats  
    item = pq.del_max()                                             # extract highest prioritized item in the queue
    s = item.seats
    p = item.population
    item.seats += 1
    allocated_seats += 1
    constant = math.sqrt(item.seats*(item.seats+1))                 # calculate constant that is used for division
Exemple #5
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