DEV Community

Discussion on: Daily Challenge #237 - Delete Extra Occurrences

Collapse
 
mellen profile image
Matt Ellen • Edited

Trying to figure out a version that dosn't use a list or map to remember what's been, I've created this mess in python, that is limited to a maximum count of 999:

def nomorethancount(arr, count):
    if count < 1:
        return []

    if count > len(arr):
        return arr

    if count >= 1000:
        raise ValueError('count must be less than 1000')

    bigposnum = 0
    bignegnum = 0

    result = []

    for item in arr:
        if item < 0:
            absitem = -1 * item
            bignegnum = checkitem(absitem, item, bignegnum, count, result)
        else:
            bigposnum = checkitem(item, item, bigposnum, count, result)

    return result

def checkitem(absitem, item, bignum, count, result):
    if 1000**absitem > bignum:
        bignum += 1000**absitem
        result.append(item)
    else:
        currentcount = (bignum // 1000**absitem) % 1000
        if currentcount < count:
            result.append(item)
            bignum += 1000**absitem

    return bignum