From 2ac7c873b0b514627ba750fb00953c3e81d2a99c Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Sat, 1 Dec 2012 13:18:08 -1000 Subject: [PATCH] Add the little 'scan' sample program --- 4-SoftwareEngineering/Testing/scan.py | 15 +++++++++++ 4-SoftwareEngineering/Testing/test_scan.py | 31 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 4-SoftwareEngineering/Testing/scan.py create mode 100644 4-SoftwareEngineering/Testing/test_scan.py diff --git a/4-SoftwareEngineering/Testing/scan.py b/4-SoftwareEngineering/Testing/scan.py new file mode 100644 index 0000000..eb04d5a --- /dev/null +++ b/4-SoftwareEngineering/Testing/scan.py @@ -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 index 0000000..36764c0 --- /dev/null +++ b/4-SoftwareEngineering/Testing/test_scan.py @@ -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 -- 2.26.2