Add the little 'scan' sample program test-thw-bray
authorErik M. Bray <embray@stsci.edu>
Sat, 1 Dec 2012 23:18:08 +0000 (13:18 -1000)
committerW. Trevor King <wking@tremily.us>
Fri, 1 Nov 2013 03:29:48 +0000 (20:29 -0700)
4-SoftwareEngineering/Testing/scan.py [new file with mode: 0644]
4-SoftwareEngineering/Testing/test_scan.py [new file with mode: 0644]

diff --git a/4-SoftwareEngineering/Testing/scan.py b/4-SoftwareEngineering/Testing/scan.py
new file mode 100644 (file)
index 0000000..eb04d5a
--- /dev/null
@@ -0,0 +1,15 @@
+def scan(lst):
+    if len(lst) == 0:
+        return 0
+    total = 0
+    last = None
+    for item in lst:
+        if last is None or last < item:
+            total += item
+            last = item
+        elif last > item:
+            total = 0
+            last = None
+        else:
+            pass
+    return total
diff --git a/4-SoftwareEngineering/Testing/test_scan.py b/4-SoftwareEngineering/Testing/test_scan.py
new file mode 100644 (file)
index 0000000..36764c0
--- /dev/null
@@ -0,0 +1,31 @@
+def scan(lst):
+    if len(lst) == 0:
+        return 0
+    total = 0
+    last = None
+    for item in lst:
+        if last is None or last < item:
+            total += item
+            last = item
+        elif last > item:
+            total = 0
+            last = None
+        else:
+            pass
+    return total
+
+assert scan([])                 == 0
+assert scan([1, 2, 3, 4])       == 10
+assert scan([2, 3, 4, 5])       == 14
+assert scan([1, 1, 1, 1])       == 1
+assert scan([3, 2, 5, 4])       == 0
+assert scan([4, 3, 2, 1])       == 0
+assert scan([3, 4, 5, 1])       == 0
+assert scan([1, 2, 3, 5, 4])    == 0
+assert scan([1, 2, 3, 3])       == 6
+assert scan([1, 2, 4, 4])       == 7
+assert scan([1, 2, 4, 4, 5])    == 12
+assert scan([-3, -2, -1])       == -6
+assert scan([-1, -2, -3])       == -3
+assert scan([-1, -2, -3, -2, -1]) == -6
+assert scan([3, 2, 1, 2, 3])      == 6