Skip to content

Instantly share code, notes, and snippets.

@santa4nt
Created April 17, 2013 22:18
Show Gist options
  • Select an option

  • Save santa4nt/5408228 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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)
@santa4nt
Copy link
Author

$ ./vrfsrtints.py < sortedints 
1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment