2
#Given the following 2 example tables (always both tables have the same dimensions)

tabla_ganancias = [[5, 6, 3, 5, 5], 
                   [2, 5, 3, 1, 0], 
                   [2, 4, 0, 1, 1]]

tabla_asignaciones = [[ 140,  220, None,   80,   60], 
                      [None, None,  330, None, None], 
                      [None, None,   30, None,   90]]

tabla_resultado = []
rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])

#Code that creates result_table and does the decomposition of the profits
#HERE THE PROBLEM

#Print the results obtained
print(tabla_resultado)
for lista in tabla_resultado: print(lista)

I decompose the values of tabla_ganancias that correspond to the non-empty cells (that is, they are not None) of tabla_assignments, placing the decomposition of the values of the earnings in the last of the rows and in the last of the columns .

In this case, the values of the profits would be broken down (by breaking down I mean finding 2 integers that add up to form):

The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5

The gain value 6 become in 2 in a row and 4 in a column ---> 2 + 4 = 6

The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5

The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5

The gain value 3 become in 1 in a row and 2 in a column ---> 1 + 2 = 3

The gain value 0 become in 2 in a row and -2 in a column ---> 2 + (-2) = 0

The gain value 1 become in -2 in a row and 3 in a column ---> -2 + 3 = 1

So that there is a resulting table having in its last row and its last column the decomposition of the profits. Note that tabla_resultado is the tabla_asignaciones but with the row and column of the decompositions of the profits

#The decomposed values will be stored in the last row and last column of tabla_resultado.


#System of equations that should be solved to find the values that I need to build the last of the rows and the last of the columns of tabla_resultado
# X1 + Y1 = 5
# X2 + Y1 = 6
# X4 + Y1 = 5
# X5 + Y1 = 5
# X3 + Y2 = 3
# X3 + Y3 = 0
# X5 + Y3 = 1

#tabla_resultado = [[ 140,  220, None,   80,   60,    Y1], 
#                   [None, None,  330, None, None,    Y2], 
#                   [None, None,   30, None,   90,    Y3], 
#                   [  X1,   X2,   X3,   X4,   X5,  None]]

tabla_resultado = [[ 140,  220, None,   80,   60,    2], 
                   [None, None,  330, None, None,    1], 
                   [None, None,   30, None,   90,   -2], 
                   [   3,    4,    2,    3,    3, None]]

This code not work, but I add this

#Given the following 2 example tables (always both tables have the same dimensions)

tabla_ganancias = [[5, 6, 3, 5, 5], 
                   [2, 5, 3, 1, 0], 
                   [2, 4, 0, 1, 1]]

tabla_asignaciones = [[ 140,  220, None,   80,   60], 
                      [None, None,  330, None, None], 
                      [None, None,   30, None,   90]]


tabla_resultado = []
rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])

for i in range(rows):
    tabla_resultado.append([])
    for j in range(columns):
        if tabla_asignaciones[i][j] is not None:
            value = tabla_ganancias[i][j]
            x = value // 2
            y = value - x
            tabla_resultado[i].append(x)
            if len(tabla_resultado) <= j+rows:
                tabla_resultado.append([None] * (rows+1))
            tabla_resultado[i][j+rows] = y
        else:
            tabla_resultado[i].append(None)

# Print the results obtained
print(tabla_resultado)
for lista in tabla_resultado: print(lista)

But I get that error

Traceback (most recent call last):
    tabla_resultado[i][j+rows] = y
IndexError: list assignment index out of range

Become the problem to System of Linear Equations enter image description here

Elektvocal95
  • 117
  • 7
  • 2
    What's your question? If you're having trouble getting your code working, post what you tried. We're not going to write it for you. – Barmar Jun 22 '23 at 05:49
  • 1
    I see a preliminary mathematical question: for a 5x3 matrix like your example, you have to find 3+5=8 values. Here you have 7 non null values in `tabla_asignaciones` which give you 7 linear equations. Except for problematic cases you should have an infinity of solutions. But if you had 9 non null values, you would end with 9 (linear) equations for 8 unknown values which except in special cases give no solution... Have you a reason to choose one solution in the example use case, and are you sure that a solution will always exist? – Serge Ballesta Jun 22 '23 at 06:40
  • @Barmar sorry, now I attach my code to the end of the question, even though it doesn't give the correct array – Elektvocal95 Jun 22 '23 at 09:57
  • @SergeBallesta most likely there is always a solution while working with integers, the only thing you should keep in mind is that since it is a table, the elements of the boxes must be decomposed into elements that also allow the decomposition of the rest of the elements in its row and its column – Elektvocal95 Jun 22 '23 at 10:00
  • @Elektvocal95: Let us look at `tabla_ganancias = [[2, 2], [2, 3]]` and `tabla_asignaciones = [[ 140, 220], [40, 60]]`. As we have same values in first column, the numbers to add in the new columns should be the same. But as values in second column are different, the numbers in the new column should have the same difference => no possible solution... – Serge Ballesta Jun 22 '23 at 12:38
  • @SergeBallesta I was quite interested in what you said before stating the problem as a system of linear equations, I have outlined it on paper, although the system is large enough to make it practical to solve it manually, perhaps the solution to this question is how to set up this problem as a System of equations and the values of X1, X2, X3, X4, X5, Y1, Y2 and Y3 are what we must place in the last row (the Xi) and in the last column (the Yi) of the resulting matrix , which is our goal to put together. – Elektvocal95 Jun 22 '23 at 12:56
  • @SergeBallesta I have proposed a code to solve this system of equations and at the same time complete the matrix, change the code that I put before in the question for this new code that generates an `IndexError` – Elektvocal95 Jun 22 '23 at 13:38

1 Answers1

1

The answer is not to fix the IndexError in your code, but to achieve your desired output. And also when you try to create a list of None, avoid using [None] * n, refer to this answer : List of lists changes reflected across sublists unexpectedly.

Here is the code, I have added some comments :

rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])
# Append a None for each row 
[tabla_asignaciones[t].append(None) for t in range(rows)]

# Append a list of None as the last row
add_row_list = [None for t in range(columns+1)]
tabla_asignaciones.append(add_row_list)

for i in range(rows):
    for j in range(columns):
        # Check if is None in tabla_asignaciones
        if tabla_asignaciones[i][j] is not None:
            value = tabla_ganancias[i][j]
            
            # If don't have value in X and Y
            if add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
                x = value // 2
                y = value - x
                tabla_asignaciones[rows][j] = y
                tabla_asignaciones[i][columns] = x
            # If don't have value in X but have value in  Y
            elif add_row_list[j] is None and  not tabla_asignaciones[i][columns] is None:
                tabla_asignaciones[rows][j] = value - tabla_asignaciones[i][columns]
            # If have value in X but don't have value in  Y
            elif not add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
                tabla_asignaciones[i][columns] = value - tabla_asignaciones[rows][j]

The result is tabla_asignaciones:

[[140, 220, None, 80, 60, 2],

[None, None, 330, None, None, 1],

[None, None, 30, None, 90, -2],

[3, 4, 2, 3, 3, None]]

HMH1013
  • 1,216
  • 2
  • 13
  • This code returns an empty array [] – Elektvocal95 Jun 22 '23 at 15:08
  • 1
    @ Elektvocal95, As I wrote at last, the result is variable `tabla_asignaciones`, if you only want the result name as `tabla_resultado`, then add `tabla_resultado = tabla_asignaciones` at last of all the codes. – HMH1013 Jun 22 '23 at 15:48
  • @HMV1013 Sorry, I forget that, really thanks for the help – Elektvocal95 Jun 22 '23 at 15:55
  • 1
    @ Elektvocal95 My error for adding a useless code `tabla_resultado = []` at first line, which make you confused, I have deleted that. – HMH1013 Jun 22 '23 at 16:04
  • What's the reason for this list comprehension: `[tabla_asignaciones[t].append(None) for t in range(rows)]`? You never assign the resulting list to anything. Use a `for` loop if it's just for side effects. – Barmar Jun 23 '23 at 00:39
  • There's nothing wrong with `list.append([None]*n)`. The problem happens if you try to multiply that, e.g. `[[None]*n] * m` – Barmar Jun 23 '23 at 00:42