summaryrefslogtreecommitdiff
path: root/dictionary.py
diff options
context:
space:
mode:
Diffstat (limited to 'dictionary.py')
-rw-r--r--dictionary.py38
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)