From df7e05fea4a14d6749eb476ae4e66b4f2a771a19 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 5 Feb 2022 18:40:27 +0000 Subject: Working on a few last features. --- dictionary.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'dictionary.py') diff --git a/dictionary.py b/dictionary.py index 32c4e73..9fe1bb4 100644 --- a/dictionary.py +++ b/dictionary.py @@ -33,6 +33,20 @@ class dictionary: self.data.append([key, value]) self.size += 1 + # increment a value + def inc(self, key, value=1): + # defensive programming + in_data = False + i = 0 + # high efficiency loop stops after found + while i < self.size and in_data == False: + if self.data[i][0] == key: + self.data[i][1] += value + in_data = True + i += 1 + if not in_data: + self.add(key, value) + # Sort dictionary by value def sort_by_value(self): self.data = sort_v(self.data, self.size, 1) -- cgit v1.2.3