Skip to content

Commit 192cb51

Browse files
committed
Completed Positional List from Book.
1 parent ae3a655 commit 192cb51

File tree

5 files changed

+179
-55
lines changed

5 files changed

+179
-55
lines changed

.idea/workspace.xml

Lines changed: 58 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import favourites_sorted_list as FL
2+
3+
class FavouritesListMTF(FL.FavouritesList):
4+
"""List of elements ordered with move-to-front heuristic"""
5+
6+
# override _move_up to provide move-to-front semantics
7+
def _move_up(self, p):
8+
"""Move accessed item at Position p to front of list"""
9+
if p != self._data.first():
10+
self._data.add_first(self._data.delete(p))
11+
12+
# override top because list is no longer sorted
13+
def top(self, k):
14+
"""Generate sequence of top k elements in term of access count."""
15+
if not 1 <= k <= len(self):
16+
raise ValueError('Illegal value for k')
17+
18+
# we begin by making the copy of the original list
19+
temp = FL.PositionalList()
20+
for item in self._data:
21+
temp.add_last(item)
22+
23+
# we repeatedly find, report, and remove the element with largest count
24+
for j in range(k):
25+
# find and report next highest from temp
26+
highPos = temp.first()
27+
walk = temp.after(highPos)
28+
while walk is not None:
29+
if walk.element()._count > highPos.element._count:
30+
highPos = walk
31+
walk = temp.after(walk)
32+
# we have found the element with highest count
33+
yield highPos.element()._value
34+
temp.delete(highPos)
35+

0 commit comments

Comments
 (0)