Facing issue with the logic
Well, So I have coded a little bit different and added free spaces in right, left, up and below… here is my main logic…
freeSpace = 0
for l in range(1, len(ground)):
for l1 in range(1, (len(ground2[l])-1)):
if ground2[l][l1] == ".":
if (ground2[l][l1-1] and ground2[l][l1+1] and ground2[l-1][l1] and ground2[l+1][l1]) == '.':
freeSpace += 1
else:
l1 += 1
Where the the lists is…
ground = [['.', '*', '*', '*', '*', '.', '.', '.'],['.', '*', '*', '*', '*', '*', '.', '.'],['.', '.', '.', '*', '.', '*', '*', '.'],['.', '.', '*', '.', '.', '.', '*', '.'],['.', '.', '*', '.', '*', '*', '.', '.'],['.', '.', '.', '*', '*', '.', '.', '.']]
ground2 = [['.', '.', '.', '.', '.', '.', '.', '.'],['.', '*', '*', '*', '*', '.', '.', '.'],['.', '*', '*', '*', '*', '*', '.', '.'],['.', '.', '.', '*', '.', '*', '*', '.'],['.', '.', '*', '.', '.', '.', '*', '.'],['.', '.', '*', '.', '*', '*', '.', '.'],['.', '.', '.', '*', '*', '.', '.', '.'],['.', '.', '.', '.', '.', '.', '.', '.']]
Full Code
x, y = 6, 6
ground = []
for i in range(x):
usr = input("")
ground.append(usr)
ground2 = []
# all list in for every abstract
for k in ground:
ground2.append(list(k))
ground2.insert(0, ['.', '.', '.', '.', '.', '.'])
ground2.insert((len(ground2)), ['.', '.', '.', '.', '.', '.'])
for o in range(len(ground2)):
ground2[o].insert(0, ".")
ground2[o].insert(len(ground2)+1, ".")
freeSpace = 0
for l in range(1, len(ground)):
for l1 in range(1, (len(ground2[l])-1)):
if ground2[l][l1] == ".":
if (ground2[l][l1-1] and ground2[l][l1+1] and ground2[l-1][l1] and ground2[l+1][l1]) == '.':
freeSpace += 1
else:
l1 += 1
print(freeSpace)
**Output comes 7 while the output could be 3 **
@hjr265