aboutsummaryrefslogtreecommitdiff
path: root/nicla
diff options
context:
space:
mode:
Diffstat (limited to 'nicla')
-rw-r--r--nicla/garbage_filter.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/nicla/garbage_filter.py b/nicla/garbage_filter.py
new file mode 100644
index 0000000..3afd2c3
--- /dev/null
+++ b/nicla/garbage_filter.py
@@ -0,0 +1,40 @@
+traffic_light_garbage = list()
+
+"""
+filter garbage
+
+arguments:
+arr -- garbage filter memory list
+val -- input value
+sensitivity -- minimum amount of `val` in `arr` to return `val`
+limit -- max length of `arr`
+
+return value:
+if `arr` contains `sensitivity` or more of any item, that item will be
+returned, else None is returned
+"""
+def garbage_filter(arr, val, sensitivity, limit):
+ if val == None: return None
+ arr[:] = [None]*(limit - len(arr)) + arr
+ arr.pop(0)
+ arr.append(val)
+ if len([x for x in arr if x == val]) >= sensitivity:
+ return val
+ return None
+
+if __name__ == "__main__":
+ inputs = [
+ "red",
+ None,
+ "green",
+ "green",
+ None,
+ "red",
+ "green",
+ "red",
+ "red",
+ None,
+ None
+ ]
+ for x in inputs:
+ print(garbage_filter(traffic_light_garbage, x, 3, 4))