changeset 837:5ba219864529

add example program
author Jeff Hammel <k0scist@gmail.com>
date Mon, 27 Mar 2017 14:05:29 -0700
parents 0fa456446fea
children 3cda483d71b2
files python/example/array_sum.py
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/example/array_sum.py	Mon Mar 27 14:05:29 2017 -0700
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+"""
+given a `value` and an array,
+if the sum of two numbers in the array
+total the value, return `True`
+(exit 0):
+
+ ./array_sum.py 3 1 2 4  # YES!  because 1+2=3
+
+ ./array_sum.py 4 1 2 5 6  # NO!
+"""
+
+import argparse
+import sys
+
+
+def sum_of_two(value, *array):
+
+    start = 0
+    end = len(array) - 1
+    complements = []
+    while start < end:
+        start_val = array[start]
+        end_val = array[end]
+        _sum = start_val + end_val
+        if _sum == value:
+            complements.append((start_val, end_val))
+            end -= 1
+            start +=1
+            continue
+        if _sum > value:
+            end -= 1
+        else:
+            start += 1
+    return complements
+
+
+def main(args=sys.argv[1:]):
+
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('value', type=int)
+    parser.add_argument('array', type=int, nargs='+')
+    options = parser.parse_args(args)
+
+    retval = sum_of_two(options.value, *options.array)
+
+    print (retval)
+
+    sys.exit(0 if retval else 1)
+
+
+if __name__ == '__main__':
+    main()