In: Computer Science
PYTHON
def find_treasure(mapfile):
with open (mapfile, 'r') as f:
data = f.readlines()
for i, row in enumerate(data):
for j, column in enumerate(row):
if 'S' in column:
print((i,j))
return (i,j)
#This is what mapfile contains
#AAASAAA
#AASSSAA
#AAASAAA
#Need to find the coordinate of the middle S
def find_treasure(mapfile): with open (mapfile, 'r') as f: data = f.readlines() for i, row in enumerate(data): for j, column in enumerate(row): # Check the current cell should not be on any edges # also, It should be surrounded by all side from # S if i > 0 and j > 0 and i < len(data) - 1 and j < len(data[i]) - 1\ and column == 'S' and row[j-1] == 'S' and row[j+1] == 'S'\ and data[i-1][j] == 'S' and data[i+1][j] == 'S': print(i, j) return (i, j) #This is what mapfile contains #AAASAAA #AASSSAA #AAASAAA #Need to find the coordinate of the middle S find_treasure('map.data') # Call like this.
************************************************** You have given very less info in the question on what are the conditions.. I am assuming that, we need to find the 'S', which has all its neighbors(up/Down/left/right) as 'S'. I coded based on this fact.. If any issues,please ask. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.