Created
April 17, 2013 22:18
-
-
Save santa4nt/5408228 to your computer and use it in GitHub Desktop.
Verify that a binary input containing 4-byte (signed) integers are sorted ascending.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| import array | |
| BUFSIZE = 4096 # read 1000 * 4-byte (32-bit) numbers at a time | |
| input = sys.stdin | |
| largest = None | |
| buf = input.read(BUFSIZE) | |
| while buf: | |
| a = array.array('i', buf) | |
| for n in a: | |
| if a < largest: | |
| # found a new number that is larger than currently known largest number! | |
| print 0 | |
| sys.exit(-1) | |
| else: | |
| largest = a | |
| buf = input.read(BUFSIZE) | |
| # the input is sorted! | |
| print 1 | |
| sys.exit(0) |
Author
santa4nt
commented
Apr 17, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment