diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-06-08 12:31:45 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-06-08 12:31:48 +0200 |
commit | a6ea4db7c5b4fe942eb7bed4deee19efa6789b44 (patch) | |
tree | 0e7118ecce503990199e8d5f64474c38f5782978 | |
parent | bb19171eaa717acec95a7473267db8ce38f208b9 (diff) |
implement garbage filterdev
-rw-r--r-- | nicla/garbage_filter.py | 40 |
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)) |