diff options
Diffstat (limited to 'dictionary.py')
| -rw-r--r-- | dictionary.py | 14 |
1 files changed, 14 insertions, 0 deletions
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) |
