From 1a283dc96e29459da9522d5fc995563280a0141f Mon Sep 17 00:00:00 2001 From: shellfly Date: Tue, 19 Dec 2017 15:02:13 +0800 Subject: [PATCH] add MaxPQ MinPQ --- algs4/max_pq.py | 43 +++++++++++++++++++++++++++++++++++++++++++ algs4/min_pq.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ algs4/top_m.py | 19 +++++++++++++++++++ algs4/transaction.py | 26 ++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 algs4/max_pq.py create mode 100644 algs4/min_pq.py create mode 100644 algs4/top_m.py create mode 100644 algs4/transaction.py diff --git a/algs4/max_pq.py b/algs4/max_pq.py new file mode 100644 index 0000000..54fed75 --- /dev/null +++ b/algs4/max_pq.py @@ -0,0 +1,43 @@ +class MaxPQ: + + def __init__(self): + self.pq = [] + + def insert(self, v): + self.pq.append(v) + self.swim(len(self.pq) - 1) + + def max(): + return self.pq[1] + + def del_max(self, ): + m = self.pq[0] + self.pq[0], self.pq[-1] = self.pq[-1], self.pq[0] + self.pq = self.pq[:-1] + self.sink(0) + return m + + def is_empty(self, ): + return not self.pq + + def size(self, ): + return len(self.pq) + + def swim(self, k): + while k > 0 and self.pq[(k - 1) // 2] < self.pq[k]: + self.pq[k], self.pq[ + (k - 1) // 2] = self.pq[(k - 1) // 2], self.pq[k] + k = k // 2 + + def sink(self, k): + N = len(self.pq) + + while 2 * k + 1 <= N - 1: + j = 2 * k + 1 + if j < N - 1 and self.pq[j] < self.pq[j + 1]: + j += 1 + + if self.pq[k] > self.pq[j]: + break + self.pq[k], self.pq[j] = self.pq[j], self.pq[k] + k = j diff --git a/algs4/min_pq.py b/algs4/min_pq.py new file mode 100644 index 0000000..b9e4411 --- /dev/null +++ b/algs4/min_pq.py @@ -0,0 +1,44 @@ +class MinPQ: + + def __init__(self): + self.pq = [] + + def insert(self, v): + self.pq.append(v) + self.swim(len(self.pq) - 1) + + def min(self): + return self.pq[1] + + def del_min(self, ): + m = self.pq[0] + self.pq[0], self.pq[-1] = self.pq[-1], self.pq[0] + self.pq = self.pq[:-1] + self.sink(0) + return m + + def is_empty(self, ): + return not self.pq + + def size(self, ): + return len(self.pq) + + def swim(self, k): + while k > 0 and self.pq[(k - 1) // 2] > self.pq[k]: + self.pq[k], self.pq[ + (k - 1) // 2] = self.pq[(k - 1) // 2], self.pq[k] + k = (k - 1) // 2 + + def sink(self, k): + N = len(self.pq) + + while 2 * k + 1 <= N - 1: + j = 2 * k + 1 + if j < N - 1 and self.pq[j] > self.pq[j + 1]: + j += 1 + + if self.pq[k] < self.pq[j]: + break + + self.pq[k], self.pq[j] = self.pq[j], self.pq[k] + k = j diff --git a/algs4/top_m.py b/algs4/top_m.py new file mode 100644 index 0000000..b1c3abe --- /dev/null +++ b/algs4/top_m.py @@ -0,0 +1,19 @@ +import sys + +from algs4.min_pq import MinPQ +from algs4.stack import Stack +from algs4.transaction import Transaction + +M = int(sys.argv[1]) +pq = MinPQ() +for line in sys.stdin: + Transaction(line) + pq.insert(Transaction(line)) + if pq.size() > M: + pq.del_min() + +stack = Stack() +while not pq.is_empty(): + stack.push(pq.del_min()) +for t in stack: + print(t) diff --git a/algs4/transaction.py b/algs4/transaction.py new file mode 100644 index 0000000..4ebd6d0 --- /dev/null +++ b/algs4/transaction.py @@ -0,0 +1,26 @@ +class Transaction: + + def __init__(self, l): + who, when, amount = l.split() + self.who = who + self.when = when + self.amount = float(amount) + + def __str__(self): + return "%-10s %10s %8.2f" % (self.who, self.when, float(self.amount)) + + def __lt__(self, other): + return self.amount < other.amount + + def __gt__(self, other): + return self.amount > other.amount + + def __eq__(self, other): + return self.amount == other.amount + + def hashcode(self): + hashcode = 1 + hashcode = 31 * hashcode + hash(self.who) + hashcode = 31 * hashcode + hash(self.when) + hashcode = 31 * hashcode + hash(self.amount) + return hashcode