From a6ea4db7c5b4fe942eb7bed4deee19efa6789b44 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 8 Jun 2023 12:31:45 +0200 Subject: implement garbage filter --- nicla/garbage_filter.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nicla/garbage_filter.py (limited to 'nicla') 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)) -- cgit v1.2.3