Alan Wakke Alan Wakke - 3 months ago 29
Python Question

Function to check if row is occupied?

I'm creating a function to check if the row in a list is empty to the right of a variable and then returning a Boolean. But having trouble figuring out how to get the positioning, how would I go about checking the row properly? Anything right of 'A' has to be empty in order to be returned as True. If there is another string, it will be returned as false.


Area0 = [['.', '.', 'A', 'A', '.', '.']]

→ True

Area1 = [['.', '.', 'A', 'A', '.', 'e']]

→ False

Code I have so far:

def right_path_clear(area):
for i in len(area):
if "A" in area[i] and area == '.':
return True


In case you do not want to use any extra method, you may create a function like:

def right_path_clear(area):
    for row in area:
        for content in row[::-1]: # iterate in reverse order
            if content == '.':    
                continue     # continue iteration till it receives '.'
            elif content == 'A':
                return True  # apart from '.', if 'A' is encounter first, return True
                return False  # apart from '.', if first encountered value is other than 'A', return False

Sample run:

# Example 1
>>> Area0   =   [['.',  '.',    'A',    'A',    '.',    '.']]
>>> right_path_clear(Area0)

# Example 2
>>> Area1   =   [['.',  '.',    'A',    'A',    '.',    'e']]
>>> right_path_clear(Area1)