BRUTE FORCEER

 def maxVal(toConsider, avail):


    if not toConsider or avail == 0:

        result = (0, ())


    elif toConsider[0] > avail:

        result = maxVal(toConsider[1:], avail)


    else:

        nextItem = toConsider[0]


        # Ambil item

        withVal, withTake = maxVal(

            toConsider[1:],

            avail - nextItem

        )

        withVal += nextItem


        # Tidak ambil item

        withoutVal, withoutTake = maxVal(

            toConsider[1:],

            avail

        )


        # Pilih yang nilainya lebih besar

        if withVal > withoutVal:

            result = (withVal, withTake + (nextItem,))

        else:

            result = (withoutVal, withoutTake)


    return result



list_item = [2, 3, 5, 10, 20, 30, 50]


print(maxVal(list_item, 78))

Komentar

Postingan populer dari blog ini

Analisis Data Menggunakan Orange

Pengenalan Bahasa C

Pengalaman Test ELPT ITB