[SOLVED] Subarray Retreat


#1

I’ve found a bug. I’ve written the dynamic algorithm in C++ (I use Codecombat to learn Python), and then it returned good return. But, the lasers remain(why?)


#2

Hmm, C++ is not supported in codecombat, I don’t get why you’re attempting to use it? Or is it something else?


#3

But I tested if the python algorihtm is bad by writting the ggod one in c++ (level is subarray retreat, I tested if the gems I selected are the best combination) Do you want some screenshots?


#4

Well, C++ isn’t a programming language that you can use in codecombat. There’s currently Python, JavaScript, and Cofeescript(Experimental). So you should use one of the 3 because C++ can’t be used in codecombat.


#5

I use python but I have a c++ compiler so I wanted to test if my code is wrong (I got the gem values and tested if my python code works good)


#6
# Find the subarray of gems with the best summary value.

# This function solves the problem in linear time.
def dynamicMaxSum (gems):
    cycles = 0
    maxStartIndex = 0
    maxEndIndex = 0
    maxEndHere = gems[0].value
    currentStartIndex = 0
    maxBest = 0
    for i in range(len(gems)):
        cycles += 1
        maxEndHere += gems[i].value
        if maxEndHere < 0:
            maxEndHere = 0
            currentStartIndex = i + 1
        if maxEndHere > maxBest:
            maxStartIndex = currentStartIndex
            maxEndIndex = i
            maxBest = maxEndHere
    hero.say("I's taken " + cycles + " cycles.")
    return [maxStartIndex, maxEndIndex]

# This function solves the problem in quadratic time.
def naiveMaxSum(gems):
    cycles = 0
    maxStartIndex = 0
    maxEndIndex = 0
    maxBest = 0
    for i in range(len(gems)):
        sum = 0
        for j in range(i, len(gems)):
            cycles += 1
            if cycles > 104:
                hero.say("I fed up of calculations.")
                return [i, j]
            sum += gems[j].value
            if sum > maxBest:
                maxStartIndex = i
                maxEndIndex = j
                maxBest = sum
    hero.say("I's taken " + cycles + " cycles.")
    return [maxStartIndex, maxEndIndex]

# Don't worry "findItems" sort out gems by X coordinate.
items = hero.findItems()
# Δ: Maybe we should change this function?
#edges = naiveMaxSum(items)
edges = dynamicMaxSum(items)

x1 = edges[0] * 4 + 4
x2 = edges[1] * 4 + 4

# Collect gems from x1 to x2 and escape:
hero.moveXY(x1, 40)
hero.moveXY(x2, 40)
hero.moveXY(40,64)

This is my code


#7

Did you change any of the the Dynamic code?


#8

Lol
It didn’t work yesterday and now… it is success
Anyway that must be a strange bug


#9

you know with pender you can just walk through


#10

I mean it’s better if you do it the normal way.


#11

also with ritic. Thats to only way to get 1 in the leaderboards


#12

Note that, doing the level as it tell you to is always better than doing something else and solving it.