Skip to content

Commit cdb3a6e

Browse files
committed
statistics word
1 parent 0dd713c commit cdb3a6e

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

newcodes/answers/q71.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class StatWrod(dict):
5+
def add(self, item, increment=1):
6+
self[item] = increment + self.get(item, 0)
7+
def counts(self, reverse=False):
8+
aux = [(self[k], k) for k in self]
9+
aux.sort()
10+
if reverse:
11+
aux.reverse()
12+
return [(v, k) for v,k in aux]
13+
14+
if __name__ == "__main__":
15+
sentence = "Don't stay in bed, unless you can make money in bed."
16+
words = sentence.split()
17+
c = StatWrod()
18+
for word in words:
19+
c.add(word)
20+
print("Ascending count:")
21+
print(c.counts())
22+
print("Descending count:")
23+
print(c.counts(reverse=True))

0 commit comments

Comments
 (0)