-2

I need help with shifting and deleting elements in a 2-dimensional array.

  1. If the value in a list is negative and their is a list above it with positive values in the same location. It should shift everything down, causing the negative values to disappear.

  2. If there isn't any list above it or the corresponding values in the list above are just 0. It will replace the negative values with 0.

Scenario 1, 3, and 4 are working! But Scenario 2 doesn't work. (I hope I covered all possible scenarios in my examples)

Note: The positive values should never disappear, they can only move down when needed. Only the negative values (below -100) disappear.

These examples should explain it better:

Scenario 1: # This Works

DATA

[[   0,    0,    0, 0, 0],
 [   0,    0,    0, 0, 0],
 [   1,    3,    1, 0, 0],
 [-102, -102, -102, 0, 0],
 [   3,    1,    3, 0, 0]]

EXPECT

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [1, 3, 1, 0, 0],
 [3, 1, 3, 0, 0]]

Scenario 2: # Doesn't Work

DATA

[[   0,    0,    0, 0, 0],
 [   0,    0,    0, 0, 0],
 [   1,    2,    1, 0, 0],
 [   2,    1,    2, 0, 0],
 [-103, -103, -103, 0, 0]]

EXPECT

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [1, 2, 1, 0, 0],
 [2, 1, 2, 0, 0]]

The Current Output (Incorrect):

[[0, 0, 0, 0, 0], 
 [0, 0, 0, 0, 0], 
 [1, 2, 1, 0, 0], 
 [2, 1, 2, 0, 0], 
 [0, 0, 0, 0, 0]] 

Scenario 3: # This Works

DATA

[[0, 0,    0,    0,    0],
 [0, 0,    0,    0,    0],
 [0, 2, -101, -101, -101],
 [0, 1,    2,    3,    2],
 [0, 3,    3,    2,    3]]

EXPECT

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 2, 0, 0, 0],
 [0, 1, 2, 3, 2],
 [0, 3, 3, 2, 3]]

Scenario 4: # This Works

DATA

[[ 0, 0, 0, 0, 0],
 [ 0, 0, 0, 0, 0],
 [-101, 2, 2, 3, 4],
 [-101, 1, 2, 3, 2],
 [-101, 3, 3, 2, 3]]

EXPECT

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 2, 2, 3, 4],
 [0, 1, 2, 3, 2],
 [0, 3, 3, 2, 3]]

Here is my code - It's definitely overcomplicated for what I am trying to achieve but I have been stuck on this and can't figure out any alternative. (I can't import any outside function...so no numpy)

def move(data):

    # PART 1
    '''
    Creates a list that contains a tuple (row, coll) of the location where a negative value (> -100) appears on the list.  
    '''

    rows = len(data)

    c_count = 4

    row_list = []

    while c_count >= 0:
        for x in range(rows):
            if data[x][c_count] < -100:
                row_list.append((x, c_count))

        c_count -=1



    # PART 2
    '''
    Iterates through the list of values that contain negative value (> -100) and performs the actions listed below. 
    '''

    for x in row_list:
        row = x[0]
        col = x[1]

        try: # If there isn't anything above the negative value (except for 0), make all negative value (>-100) == 0. EXAMPLE (DATA 3) 

            if data[row-1][col] == 0:
                 data[row][col] = 0

        except(IndexError):
            pass

        try: # If a row of negative values is between a row on top and bottom that contains possitive values, then merge the values above with the negative values below it. EXAMPLE (DATA 1)

            if data[row-1][col] > 0 and data[row+1][col] > 0:

                c_count = 4
                while c_count >= 0:

                    count = len(data) - 1
                    prev = count - 1

                    while count > 0 and prev >= 0:
                        if data[count][c_count] < -100:

                            while prev >= 0 and data[prev][c_count] == 0:
                                prev -= 1

                            data[count][c_count] = data[prev][c_count]
                            data[prev][c_count]= 0

                        count -= 1
                        prev -= 1

                    c_count -= 1
                return data

        except(IndexError):
            pass

        try: # If a row of negative values has nothing underneath it (at the bottom) of the list. Then push everything on the top down replacing the negative value. This isn't working! 

**SCENARIO 2 Should Have Worked Here**

            if data[row-1][col] > 0:
                data[row][col] = 0

                rows = len(data)

                c_count = 4
                while c_count >= 0:

                    for x in range(rows):
                        if data[x][c_count] > 0:
                            rows = x
                            break

                    last = rows-1

                    if data[last][c_count] == 0:
                        while last > 0:
                            data[last][c_count] = data[last-1][c_count]
                            last -= 1   
                        data[0][c_count] = 0

                    c_count -= 1



        except(IndexError):
            pass



    return data

print('Data 1') # This Works
data1 = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [1, 3, 1, 0, 0],
         [-102, -102, -102, 0, 0],
         [3, 1, 3, 0, 0]]

print(data1, 'org')
x = move(data1)

expect1 = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [1, 3, 1, 0, 0],
           [3, 1, 3, 0, 0]]

print(x, 'sol')
print(expect1, 'expect')
print(data1 == expect1)

print()

print('Data 2') # Doesn't Work

data2 = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [1, 2, 1, 0, 0],
         [2, 1, 2, 0, 0],
         [-103, -103, -103, 0, 0]]

print(data2, 'org')
y = move(data2)

expect2 = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [1, 2, 1, 0, 0],
           [2, 1, 2, 0, 0]]

print(y,'sol')
print(expect2, 'expect')
print(data2 == expect2)

print()

print('Data 3') # This Works

data3 = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 2, -101, -101, -101],
         [0, 1, 2, 3, 2],
         [0, 3, 3, 2, 3]]
print(data3, 'org')
z = move(data3)

expect3 = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 2, 0, 0, 0],
           [0, 1, 2, 3, 2],
           [0, 3, 3, 2, 3]]

print(z,'sol')
print(expect3, 'expect')
print(data3 == expect3)

print()

print('Data 4') # This Works

data4 = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [-101, 2, 2, 3, 4],
         [-101, 1, 2, 3, 2],
         [-101, 3, 3, 2, 3]]

print(data4, 'org')
a = move(data4)

expect4 = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 2, 2, 3, 4],
           [0, 1, 2, 3, 2],
           [0, 3, 3, 2, 3]]

print(a,'sol')
print(expect4, 'expect')
print(data4 == expect4)
5
  • Thanks for your help and @furas if you can help that would be great! Commented Dec 3, 2017 at 23:55
  • Can you demonstrate any effort at solving this yourself? For example, comparing the actual output with what is expected to see where the differences are, and what part(s) of the code they point to? Commented Dec 4, 2017 at 0:00
  • Hi @Scott Hunter I made some updates. Scenario 2 and 4 are not working so I wrote on the excerpt of code where it should have worked. (Scenario 1/2 work). I also posted the incorrect output for scenario 2 and 4. Commented Dec 4, 2017 at 0:18
  • Please explain Scenario 4: there are no positive values above the -1's, and there are no values less than -100. Commented Dec 4, 2017 at 0:59
  • @ScottHunter - You are right!! Scenario 4 is working!! So it seems that scenario 2 is the only one that doesn't work right now. I hope I covered all possible scenarios in my examples Commented Dec 4, 2017 at 1:18

1 Answer 1

1

I use code from previous question Python - Shift/Delete Elements in a 2-Dimensional Array
and it gives correct results:

I work with columns separately, not with full rows.

  • search in column from bottom to top
  • find negative value
  • find positive value (bigger then zero) above
  • if not found then put zero in place of negative
  • if found then move down all value above (move above to row, above-1 to row-1, above-2 to row-2, etc.)

.

def move(data):

    # work in column, not with full rows
    for col in range(len(data)):

        # move from bottom to top
        for row in range(len(data[0])-1, -1, -1):

            # check if negative value
            if data[row][col] < 0:
                print('debug: negative:', data[row][col])

                # find positive value above
                above = row-1
                while above > -1 and data[above][col] <= 0:
                    above -= 1

                # check if found positive value 
                if above == -1:
                    # put zero if not found value above
                    print('debug: put zero')
                    data[row][col] = 0
                else:
                    # move down all values above 
                    print('debug: move down', above+1, 'element(s)')
                    while above > -1:
                        data[row][col] = data[above][col]
                        data[above][col] = 0
                        row -= 1
                        above -= 1

    return data

# --- function to run one scenario, display data and check result ---

def run(data, expect):

        print('data:')
        print('\n'.join(str(row) for row in data))
        print()
        result = move(data)
        print()
        print('result:')
        print(result)
        print('expect:')
        print(expect)
        print('expect == result:', expect == result)
        print('---')

# --- scenarios ---

def scenario_B1():

    DATA = [
        [   0,    0,    0, 0, 0],
        [   0,    0,    0, 0, 0],
        [   1,    3,    1, 0, 0],
        [-102, -102, -102, 0, 0],
        [   3,    1,    3, 0, 0]
    ]

    EXPECT =    [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 3, 1, 0, 0],
        [3, 1, 3, 0, 0]
    ]

    run(DATA, EXPECT)

def scenario_B2():

    DATA = [
        [   0,    0,    0, 0, 0],
        [   0,    0,    0, 0, 0],
        [   1,    2,    1, 0, 0],
        [   2,    1,    2, 0, 0],
        [-103, -103, -103, 0, 0]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 2, 1, 0, 0],
        [2, 1, 2, 0, 0]
    ]

    run(DATA, EXPECT)

def scenario_B3():

    DATA = [
        [0, 0,    0,    0,    0],
        [0, 0,    0,    0,    0],
        [0, 2, -101, -101, -101],
        [0, 1,    2,    3,    2],
        [0, 3,    3,    2,    3]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 2, 0, 0, 0],
        [0, 1, 2, 3, 2],
        [0, 3, 3, 2, 3]
    ]

    run(DATA, EXPECT)

def scenario_B4():

    DATA = [
        [ 0, 0, 0, 0, 0],
        [ 0, 0, 0, 0, 0],
        [-101, 2, 2, 3, 4],
        [-101, 1, 2, 3, 2],
        [-101, 3, 3, 2, 3]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 2, 2, 3, 4],
        [0, 1, 2, 3, 2],
        [0, 3, 3, 2, 3]
    ]

    run(DATA, EXPECT)

# --- start scenarios ---

scenario_B1()
scenario_B2()
scenario_B3()
scenario_B4()

Results:

data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 3, 1, 0, 0]
[-102, -102, -102, 0, 0]
[3, 1, 3, 0, 0]

debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 2, 1, 0, 0]
[2, 1, 2, 0, 0]
[-103, -103, -103, 0, 0]

debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 2, -101, -101, -101]
[0, 1, 2, 3, 2]
[0, 3, 3, 2, 3]

debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[-101, 2, 2, 3, 4]
[-101, 1, 2, 3, 2]
[-101, 3, 3, 2, 3]

debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @furas, but the length of the rows and columns won’t always be the same. Is there a way to adjust this so it works in those cases? Thanks
did you try it with different lengths ? it doesn't have hardcoded lengths. See len(data) and len(data[0]).

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.