0

I have the following code. The output currently I am receiving is not the expected output. The pseudocode I am trying to solve is described below.

   for each i in 1 · · · N do
         TEi = fmob(Li)
         TCi= fc(Li)
         TUi =fd(Li) 
         return 

enter image description here

Python code

def optimal_partition():                   
 TE=[10,1,3]
 TC=[2,3,1]
 TU=[2,3,1]

 N = len(TU)-1
 SUMS = [0] * N
 for j in range(N):
    for i in range(1, j + 1):
        SUMS[j] += TE[i]
    for k in range(j - 1, N + 1):
        SUMS[j] += TC[k]
    SUMS[j] += TU[j]
 return SUMS.index(min(SUMS))

For the above code, I need the expected output to be [16,15]. Thanks, help is highly appreciated.

7
  • What is the actual output? I'm guessing an error, so please include the full text of the error traceback. Commented Sep 14, 2020 at 13:35
  • @quamrana, Thanks, The expected output is [9, 10], not retrieving error but the expected output is not what want. Commented Sep 14, 2020 at 13:38
  • I'm guessing that if I copy and run the code you supply I will get an error. If you don't get an error, then you must be running different code. Please update the question with the actual code you have. Commented Sep 14, 2020 at 13:40
  • Hi @quamrana I have provided the complete code. please check. Thanks Commented Sep 14, 2020 at 13:44
  • 1
    Following your algorithm, SUMS should have 3 values in it (j = 1..3, or when we use zero-based indexing, j = 0..2), not 2 values as you currently have. Commented Sep 14, 2020 at 14:13

2 Answers 2

1

This is a better implementation of the algorithm using list slices instead of nested loops:

def part_sum(TE, TC, TU, j):
    return sum(TE[:j+1]) + sum(TC[j+1:]) + TU[j]

def optimal_partition(TE, TC, TU):
    return min(range(len(TE)), key=lambda j: part_sum(TE, TC, TU, j)) 

TE = [10,1,3]
TC = [2,3,1]
TU = [2,3,1]
print("The sums are: ", [part_sum(TE, TC, TU, j) for j in range(3)])
print("The optimal partition is at:", optimal_partition(TE, TC, TU))

Note that there are 3 sums, not 2, and that the returned value for j uses zero-based indexing. If you want to return a one-based index then just add 1 to the optimal partition result.

Sign up to request clarification or add additional context in comments.

Comments

0

You are suffering from the 0-based indexing syndrome of computing.

Computer Science sometimes uses 0-based indexing and maths, it seems, uses 1-based indexing.

This program seems to give you the expected output:

def optimal_partition():                   
    TE=[10,1,3]
    TC=[2,3,1]
    TU=[2,3,1]

    N = len(TU) - 1
    SUMS = [0] * N
    for j in range(N):
        for i in range(j + 1):
            SUMS[j] += TE[i]
        for k in range(j + 1, N + 1):
            SUMS[j] += TC[k]
        SUMS[j] += TU[j]
    return SUMS

SUMS = optimal_partition()
print(SUMS)
print(SUMS.index(min(SUMS)))

Output:

[16, 15]
1

1 Comment

thank you for your wonderful help @quamrana, this is exactly what I want has been struggling for a long time on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.