0

I am trying to print the path for the LowCost script I found here the code Find LowCost I change some small stuff only.

The script works but it gives me only the final cost; I want it also to give me the path I have taken.

Here is an example:

[ [1, 2, 3],
  [4, 8, 2],
  [1, 5, 3] ]

so here how look like the path its get the 1,2,2,3:

1-2 3
.....\
4 8 2
......|
1 5 3

i want to print correc the path like Correct

path : [1,2,2,3]
low cost : 8

Now I get a very big path result and it's no correct Wrong !

path : [1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 1, 5, 5, 5]
low cost : 8

Here is the code:

import sys

def mymin(a,b,c):
    return min(min(a,b),c)

def minCostMain(cost, m, n):
    result = []

    def minCost(cost, m, n):
        if (n < 0 or m < 0):
           return sys.maxsize
        elif (m == 0 and n == 0):
           return  cost[m][n]
        else:
           t1= minCost(cost, m-1, n-1) 
           t2= minCost(cost, m-1, n)   
           t3= minCost(cost, m, n-1)   

           v=mymin(t1,t2,t3)

           #this dosen't work get more items     
           result.append(v)

           return cost[m][n] + v

    return minCost(cost, m, n),result

cost= [ [1, 2, 3],
        [4, 8, 2],
        [1, 5, 3] ]

lowcost,path= minCostMain(cost, 2, 2) 
print "path : " +  str(path)
print "low cost : " +  str(lowcost) 
Laz
  • 11
  • 2
  • Hi Laz, I don't think you'll get a lot of attention on this question because it's not useful to anyone besides you. My recommendation would be to use a debugger to step through the code to see exactly what is going on. This might get you started: https://stackoverflow.com/questions/25678978/how-to-debug-python-script-that-is-crashing-python – Brent Allard Jun 11 '18 at 16:33
  • Should be migrated to [code review](https://codereview.stackexchange.com/) – Mauro Baraldi Jun 11 '18 at 16:34
  • @ Brent Allard yes i don't say it's something useful , i just wonder how i can cache when you have loop function how cache in list the results this just example i have here, but also the link you post very nice to see all the steps thank you for this – Laz Jun 11 '18 at 20:09

1 Answers1

0

ok now understand how its working i need to keep list in function and the self function return list no value and after i add the new item :) like this now i can get the path and the map where it's in list the number 1,2,2,3

import sys
def MyPrint(mlist):
    mlist[0]=str(mlist[0]) + " = (0,0)"
    sum=0
    for item in mlist:sum+=int(item.split(" = (")[0])
    print "*"*20    
    print "Sum = "+str(sum)
    print "*"*20
    print "map" 
    print "*"*20
    for item in mlist:print item

def Min(x,y, z):
    if (x[-1] < y[-1]):
        return x if (x[-1] < z[-1]) else z
    else:
        return y if (y[-1] < z[-1]) else z

def minCost(cost, m, n):
    if (n < 0 or m < 0):
        return [sys.maxsize]
    elif (m == 0 and n == 0):
        return [cost[m][n]]
    else:
        arr=Min(minCost(cost, m-1, n-1),
                minCost(cost, m-1, n),
                minCost(cost, m, n-1))

        arr.append(str(cost[m][n])+" = ("+str(m)+","+str(n)+")")

        return arr

cost= [ [1, 2, 3],
        [4, 8, 2],
        [1, 5, 3] ]
x=minCost(cost, 2, 2)      
MyPrint(x)

the result it's

********************
Sum = 8
********************
map
********************
1 = (0,0)
2 = (0,1)
2 = (1,2)
3 = (2,2)
Laz
  • 11
  • 2