Python Forum
Finding the treasure location - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Finding the treasure location (/thread-21523.html)



Finding the treasure location - cjlee420 - Oct-03-2019

A map is given to me in a form of this

WWWTWWWTTT
WWTTTWWWWW
WWWTWWWWWT


The treasure is located at the center of the cross of T's for which the case is (1,3)
The coordinate format is (row,column)
How do I find that exact coordinate of the treasure


RE: Finding the treasure location - cjlee420 - Oct-03-2019

How do I code it in a way it will find the center of T and returns its coordinates


RE: Finding the treasure location - ichabod801 - Oct-03-2019

What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.


RE: Finding the treasure location - cjlee420 - Oct-03-2019

 def find_treasure(mapfile):
    with open(mapfile,'r')as column:
        for i, row in enumerate(column):
            for j, val in enumerate(row):
                if val == 'T':
                    print(i,j) 
I can only manage to find all the coordinates for T, i dk how to set a condition such that it returns only the treasure coordinate when it is surrounded by T's


RE: Finding the treasure location - ichabod801 - Oct-03-2019

Don't think about writing the code for the condition yet. Think about what the condition is. If you have a T at i, j, what other T's do you need (in terms of i and j) to be the treasure?


RE: Finding the treasure location - cjlee420 - Oct-03-2019

What I am thinking is that for that specific T coordinate, i compare it to (i-1,j),(i+1,j),(i,j-1) and (i,j+1) if all those coordinates shows 'T' it means that (i,j) is the treasure location


RE: Finding the treasure location - ichabod801 - Oct-03-2019

So if one condition is column[i - 1][j] == 'T', how are you going to check all four conditions?


RE: Finding the treasure location - cjlee420 - Oct-03-2019

My guess is create a list to store all the T coordinate
Then run through each of the coordinate, if (i-1,j),(i+1,j),(i,j-1) and (i,j+1) are also inside the list then return (i,j)