Sales by Match

  • + 0 comments

    python without build in function (Manual Way with Logic)

    def isExistCompare(compare,record):
        for data in compare:
            if record == data:
                return True
        return False
    
    def getCompare(arr):
        compare = []
        for idx in range(len(arr)):
            record = arr[idx]
            if idx == len(arr)-1:
                checkAr = isExistCompare(compare, record)
                if  checkAr:
                    compare.append(record)
                break
            recordNext = arr[idx+1]
            if record == recordNext:
                compare.append(record)
                continue
            checkAr = isExistCompare(compare, record)
            if  checkAr:
                compare.append(record)
            
        return compare
        
    def coupleMapping(comparedData):
        res = []
        counter = 1
        for idx in range(len(comparedData)):
            records = comparedData[idx]
            if idx == len(comparedData)-1:
                if records == comparedData[len(comparedData)-2]:
                    counter +=1
                    res.append({"count": counter-1, "records":records })
                break
            else:
                recordsNext = comparedData[idx+1]
                if records == recordsNext:
                    counter+=1
                else:
                    res.append({"count": counter, "records":records })
                    counter = 1
        return res
    
    def coupleCompare(mapping):
        couple = 0
        for idx in range(len(mapping)):
            record = mapping[idx]
            if record["count"] %2 ==0 :
                couple += int(record["count"]/2)
            else:
                couple += int((record["count"]-1)/2)
        return couple
                   
    def sockMerchant(n, ar):
        arr = sorted(ar)
        comparedData = getCompare(arr)
        mapping = coupleMapping(comparedData)
        couple = coupleCompare(mapping)
        return couple