示例#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) == 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)
示例#4
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)
示例#5
0
def sort(a: List):
    """Rearranges the array in ascending order, using the natural order.

    :param a: the array to be sorted

    """
    aux = [None] * len(a)
    _sort(a, aux, 0, len(a) - 1)
    assert _is_sorted(a)


# 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__":
    import sys

    from itu.algs4.stdlib import stdio

    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)
    assert _is_sorted(a)
    for elem in a:
        print(elem)