diff options
| author | root <root@turin.home> | 2022-02-04 18:07:47 +0000 |
|---|---|---|
| committer | root <root@turin.home> | 2022-02-04 18:07:47 +0000 |
| commit | 6671090e31ab30711f169abfb61d880b50ba27d8 (patch) | |
| tree | 8952c13c358694c423f67e5e1e09223ed090a326 /dictionary.py | |
| parent | 0e23673e015a5261019a151f22b419f4c80b7e8d (diff) | |
Dictionary class. Reformat.
Diffstat (limited to 'dictionary.py')
| -rw-r--r-- | dictionary.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/dictionary.py b/dictionary.py new file mode 100644 index 0000000..32c4e73 --- /dev/null +++ b/dictionary.py @@ -0,0 +1,38 @@ +import errors + +# Recursive merge sort algorithm +# https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Python +def merge(x, y, i): + if x == []: + return y + if y == []: + return x + return ( + [x[0]] + merge(x[1:], y, i) + if x[0][i] < y[0][i] + else [y[0]] + merge(x, y[1:], i) + ) + + +def sort(a, n, i): + m = n // 2 + return ( + a + if n <= 1 + else merge(sort(a[:m], m, i), sort(a[m:], n - m, i), i) + ) + + +class dictionary: + def __init__(self): + self.data = [] + self.size = 0 + + def add(self, key, value): + if type(value) != int: + self.data.append([key, value]) + self.size += 1 + + # Sort dictionary by value + def sort_by_value(self): + self.data = sort_v(self.data, self.size, 1) |
