-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |