-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency.py
43 lines (39 loc) · 1.32 KB
/
frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from collections import defaultdict
import pdb
class Solution(object):
def push_index(self, heap, key, index, frequency):
value = frequency[key]
if heap == []:
return [key], frequency
elif frequency[heap[index]] >= value:
# logic here is messed up: this should be true!
print 'frequency[', heap[index], ']=', frequency[heap[index]], 'value=', value
heap = heap[:index] + [key] + heap[index:]
return heap, frequency
elif index % 2 == 0:
print 'other'
index -= 2
index /= 2
else:
print 'again, other'
index -= 1
index /= 2
return self.push_index(heap, key, index, frequency)
def push(self, heap, key, frequency):
return self.push_index(heap, key, len(heap) - 1, frequency)
def frequencySort(self, s):
frequency = defaultdict(int)
heap = []
for letter in s:
frequency[letter] += 1
print frequency
for key in frequency.keys():
pdb.set_trace()
heap, frequency = self.push(heap, key, frequency)
print heap
for key in heap[::-1]:
for i in xrange(frequency[key]):
print key,
print
test = Solution()
test.frequencySort('test')