Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python max heap of strings

The suggested method to have a max heap is to multiply the key with -1. What is the suggested method to have a max heap of strings?

Is there an alternative library that has that feature?

like image 696
CS_EE Avatar asked Nov 05 '19 15:11

CS_EE


People also ask

What is max heap in Python?

Max Heap in Python Difficulty Level : Medium Last Updated : 10 Sep, 2020 A Max-Heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node.

What is Max () in Python string?

Python String max () Method 1 Description. Python string method max () returns the max alphabetical character from the string str. 2 Syntax 3 Parameters 4 Return Value. This method returns the max alphabetical character from the string str. 5 Example. The following example shows the usage of max () method.

Is it possible to use heapq in Python?

3 The python standard library provides heapq – Green Cloak Guy Nov 5 '19 at 15:15 1 Yes but that is min heap. I want the inverse. – CS_EE Nov 5 '19 at 15:16 1 I think this question is very reasonable as current heapq library does not support 'max heap' by some parameter. upvote! – JUNPA Dec 27 '19 at 14:03 Add a comment |

What is heap data structure in Python?

Heap data structure is a very useful data structure when it comes to working with graphs or trees. Heap data structure helps us improve the efficiency of various programs and problem statements. We can use min-heap and max-heap as they are efficient when processing data sets.


2 Answers

You could construct a maxheap from your strings by using heapq to build a minheap of string-like objects whose character content is the same as your original strings but whose comparison operators (and therefore their sort order) produce results that are the opposite of the normal string object. If we call that object a contra_string then it would be defined like this:

class contra_string(str):

    def __init__(self, s):
       self.original = s

    def __lt__(self, s):
        return self.original.__gt__(s)

    def __le__(self, s):
        return self.original.__ge__(s)

    def __eq__(self, s):
        return self.original.__eq__(s)

    def __ne__(self, s):
        return self.original.__ne__(s)

    def __gt__(self, s):
        return self.original.__lt__(s)

    def __ge__(self, s):
        return self.original.__le__(s)

    def normal(self):
        return self.original

It would be used like this:

import heapq

mystrings = [ 'b', 'c', 'a', 'bravo', 'alpha', 'charlie' ]

maxheap = [contra_string(s) for s in mystrings]
heapq.heapify(maxheap)

print [heapq.heappop(maxheap) for n in range(len(maxheap))]
# prints "['charlie', 'c', 'bravo', 'b', 'alpha', 'a']"

Bear in mind that the objects in the heap are instances of contra_string, so they'll behave strangely if you use them in comparisons after you pop them from the heap. If you need to do anything involving naturally-ordered string comparisons with those objects after extracting them then you can recover the original ordinary string by using the normal method on the contra_string, and of course comparisons using those original strings will behave normally. That would look like:

original_strings_in_reverse_order = [heapq.heappop(maxheap).normal() for n in range(len(maxheap))]
print type(original_strings_in_reverse_order[0])
# prints "<type 'str'>"
like image 121
ottomeister Avatar answered Oct 16 '22 21:10

ottomeister


Create your own custom string class:

class MyString:
    def __init__(self, word):
        self.word = word

    def __lt__(self, other):
        return self.word > other.word

    def __eq__(self, other):
        return self.word == other.word

And use it like:

heapq.heappush(heap, MyString(word))    
like image 20
me_astr Avatar answered Oct 16 '22 21:10

me_astr