annotate python/example/array_sum.py @ 867:002c1e85ff30

prune docker volumes
author Jeff Hammel <k0scist@gmail.com>
date Wed, 18 Jul 2018 11:18:32 -0700
parents 5ba219864529
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
837
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 given a `value` and an array,
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 if the sum of two numbers in the array
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 total the value, return `True`
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 (exit 0):
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 ./array_sum.py 3 1 2 4 # YES! because 1+2=3
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 ./array_sum.py 4 1 2 5 6 # NO!
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 """
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import argparse
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import sys
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 def sum_of_two(value, *array):
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 start = 0
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 end = len(array) - 1
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 complements = []
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 while start < end:
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 start_val = array[start]
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 end_val = array[end]
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 _sum = start_val + end_val
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 if _sum == value:
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 complements.append((start_val, end_val))
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 end -= 1
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 start +=1
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 continue
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 if _sum > value:
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 end -= 1
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 else:
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 start += 1
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 return complements
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 def main(args=sys.argv[1:]):
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 parser = argparse.ArgumentParser(description=__doc__)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 parser.add_argument('value', type=int)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 parser.add_argument('array', type=int, nargs='+')
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 options = parser.parse_args(args)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 retval = sum_of_two(options.value, *options.array)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 print (retval)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 sys.exit(0 if retval else 1)
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 if __name__ == '__main__':
5ba219864529 add example program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 main()