#!/usr/bin/env python from __future__ import with_statement from numpy import zeros, int8 import sys, getopt """Erdos Discrepancy Problem sequence verification. Verifies that discrepancy is bounded by Michael Yang, 2010 Modified by Boris Konev, 2014 """ discrepancy = 0 class DiscrepancyError(Exception): pass def usage(): print "usage: Verify -i filename -d discrepancy" print " the file should contain a single line of positive and negative " print " values interpreted as +1 and -1 " def verifySequence(x, l, C=2, verbose=False): """verify discrepancy <= C """ for i in range(l): s = 0 for j in range(i,l,i+1): #if verbose: print i+1, s, s = s+x[j] # discrepancy """print 'j is ', j print 'x[j] is ', x[j] print 's is ', s""" if s > C: msg = 'd = %d has upper discrepancy %s at %d' msg %= (i+1, s, j+1) raise DiscrepancyError(msg) if s < -C: msg = 'd = %d has lower discrepancy %s at %d' msg %= (i+1, s, j+1) raise DiscrepancyError(msg) if verbose: print i+1, s if verbose: print 'verified' return True def main(argv): fileInput = '' try: opts, args = getopt.getopt(argv, "hi:d:", ["help", "input="]) except getopt.GetoptError as err: print str(err) usage() sys.exit(2) for o, a in opts: if o in ("-i", "--input"): fileInput = a if o=="-d": discrepancy = int(a) if o in ("-h", "--help"): usage() sys.exit(0) if fileInput == '': usage() sys.exit(0) with open(fileInput, 'r') as f: a = f.read() #SEQUENCE_LENGTH = (len(a)-1)/2 #x = zeros(SEQUENCE_LENGTH, dtype = int8) x = [] for side in a.strip().split(): if int(side) < 0: x.append(-1) else: x.append(1) print x SEQUENCE_LENGTH = len(x) print "Checking discrepancy of", fileInput print "for discrepancy =", discrepancy print "sequence length =", SEQUENCE_LENGTH isGood = verifySequence(x, SEQUENCE_LENGTH, discrepancy, True) if __name__=='__main__': # load +/- sequence main(sys.argv[1:])