#!/usr/bin/env python3 """Provides a quick treap demonstration.""" import sys import treap import random n = 7 def rnd_num(): """Return a random number from 0 to 99.""" return int(random.random() * 100) def rnd_seq(): """Generate n random numbers.""" for i in range(n): yield rnd_num() def inc_seq(): """Generate n consecutive, ascending numbers starting from 0.""" return range(n) def dec_seq(): """Generate n consecutive, descending numbers starting from n-1.""" return range(n-1, -1, -1) def demo(sequence, order): """Do the demo.""" print('%d values, %s order' % (n, order)) tr = treap.treap() for i in sequence(): print('adding {}'.format(i)) tr[i] = 0 print(tr) sys.stdout.write('\n') def usage(retval): """Output a usage message.""" sys.stderr.write("Usage: %s --increasing --decreasing --random\n" % sys.argv[0]) sys.exit(retval) for arg in sys.argv[1:]: if arg == '--increasing': demo(inc_seq, 'increasing') elif arg == '--decreasing': demo(dec_seq, 'decreasing') elif arg == '--random': demo(rnd_seq, 'random') elif arg in ['--help', '-h']: usage(0) else: sys.stderr.write('%s: illegal argument: %s\n' % (sys.argv[0], arg)) usage(1) print