I need help with shifting and deleting elements in a 2-dimensional array.
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.
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)