示例#1
0
def main():
    """
    Reads strings from stdin, sorts them, and prints the result to stdout.
    """
    a = stdio.readAllStrings()
    sort(a)
    _show(a)
示例#2
0
def main():
    """
    Reads in a sequence of strings from stdin
    heapsorts them, and prints the result in ascending order.
    """
    a = stdio.readAllStrings()
    sort(a)
    _show(a)
示例#3
0
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) is 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) is -1:
                print(key)
示例#4
0
        lo = 0
        while lo < n-length:
            mid = lo+length-1
            hi = min(mid+length, n-1)
            _merge(a, aux, lo, mid, hi)
            lo += 2*length
        length *= 2
    
    assert _is_sorted(a)
    

import sys
from algs4.stdlib import stdio

# Reads in a sequence of strings from standard input or a file 
# supplied as argument to the program; mergesorts them; 
# and prints them to standard output in ascending order.
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")
    
    a = stdio.readAllStrings()
    sort(a)
    for elem in a:
        print(elem)
    

示例#5
0
def is_sorted(array):
    return _is_sorted(array, 0, len(array) - 1)


def _is_sorted(array, lo, hi):
    for i in range(lo + 1, hi + 1):
        if array[i] < array[i - 1]:
            return False
    return True


# print array to standard output
def show(array):
    stdio.write(" ".join(array))


if __name__ == "__main__":
    array = stdio.readAllStrings()
    sort(array)
    assert is_sorted(array)
    show(array)

    # shuffle
    stdrandom.shuffle(array)

    # display results again using select
    print()
    for i in range(0, len(array)):
        ith = str(select(array, i))
        stdio.writeln(ith)