# Buggy code
def get_availability(self):
    """Return overall the availability of all pieces, using connected peers
    Availability is defined as the number of complete copies of a piece, thus seeders
    increment the availability by 1. Leechers provide a subset of piece thus we count the
    overall availability of all pieces provided by the connected peers and use the minimum
    of this + the average of all additional pieces.
    """
    nr_seeders_complete = 0
    merged_bitfields = None

    peers = self.get_peerlist()
    for peer in peers:
        completed = peer.get("completed", 0)
        have = peer.get("have", [])

        if completed == 1 or have and all(have):
            nr_seeders_complete += 1
        elif have:
            if merged_bitfields is None:
                merged_bitfields = [0] * len(have)

            for i in range(len(have)):
                if have[i]:
                    merged_bitfields[i] += 1

    if merged_bitfields:
        # count the number of complete copies due to overlapping leecher bitfields
        nr_leechers_complete = min(merged_bitfields)

        # detect remainder of bitfields which are > 0
        nr_more_than_min = len(
            [x for x in merged_bitfields if x > nr_leechers_complete]
        )
        fraction_additonal = float(nr_more_than_min) / len(merged_bitfields)

        return nr_seeders_complete + nr_leechers_complete + fraction_additonal
    return nr_seeders_complete


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 130, in on_read_data
RuntimeError: [Failure instance: Traceback: <type 'exceptions.IndexError'>: list index out of range
lib\site-packages\twisted\internet\base.py:1243:run
lib\site-packages\twisted\internet\base.py:1252:mainLoop
lib\site-packages\twisted\internet\base.py:878:runUntilCurrent
lib\site-packages\twisted\internet\task.py:239:__call__
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:150:maybeDeferred
Tribler\Core\CreditMining\CreditMiningManager.py:279:select_torrents
Tribler\Core\DownloadState.py:196:get_availability
]

# Fixed code
def get_availability(self):
    """Return overall the availability of all pieces, using connected peers
    Availability is defined as the number of complete copies of a piece, thus seeders
    increment the availability by 1. Leechers provide a subset of piece thus we count the
    overall availability of all pieces provided by the connected peers and use the minimum
    of this + the average of all additional pieces.
    """
    if not self.lt_status:
        return 0  # We do not have any info for this download so we cannot accurately get its availability

    nr_seeders_complete = 0
    merged_bitfields = [0] * len(self.lt_status.pieces)

    peers = self.get_peerlist()
    for peer in peers:
        completed = peer.get("completed", 0)
        have = peer.get("have", [])

        if completed == 1 or have and all(have):
            nr_seeders_complete += 1
        elif have and len(have) == len(merged_bitfields):
            for i in range(len(have)):
                if have[i]:
                    merged_bitfields[i] += 1

    if merged_bitfields:
        # count the number of complete copies due to overlapping leecher bitfields
        nr_leechers_complete = min(merged_bitfields)

        # detect remainder of bitfields which are > 0
        nr_more_than_min = len(
            [x for x in merged_bitfields if x > nr_leechers_complete]
        )
        fraction_additonal = float(nr_more_than_min) / len(merged_bitfields)

        return nr_seeders_complete + nr_leechers_complete + fraction_additonal
    return nr_seeders_complete