Question

In: Computer Science

PYTHON def find_treasure(mapfile): with open (mapfile, 'r') as f: data = f.readlines() for i, row in...

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

Solutions

Expert Solution

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.


Related Solutions

Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >=...
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >= 0 or i < 2: data.append(thing + i) print(len(stuff)) if more_stuff is not None: data += more_stuff print(data) Write a Python statement that calls silly and results in the output (from within silly): 4 [5, 3, 1, 5, -3] stuff = [5, 2, 0, 2] more_stuff = [-3] silly(stuff, more_stuff) Q2. Function silly2 is defined as: def silly2(nums, indices, extra=None): print(len(nums), len(indices)) new_indices =...
f : [a, b] → R is continuous and in the open interval (a,b) differentiable. f...
f : [a, b] → R is continuous and in the open interval (a,b) differentiable. f rises strictly monotonously ⇒ ∀x ∈ (a, b) : f ′(x) > 0. (TRUE or FALSE?) f rises strictly monotonously ⇐ ∀x ∈ (a, b) : f ′(x) > 0. (TRUE or FALSE?) f is constant ⇐⇒ ∀x∈(a,b): f′(x)=0 (TRUE or FALSE?) If f is reversable, f has no critical point. (TRUE or FALSE?) If a is a “minimizer” of f, then f ′(a)...
Given the python code below, show output: class MyClass: i = ["1010"] def f(self, name): self.i.append(str("470570"))...
Given the python code below, show output: class MyClass: i = ["1010"] def f(self, name): self.i.append(str("470570")) self.name = name return 'CPS' if __name__ == "__main__": x = MyClass()    print(x.f("U"))    print(x.name)    print(x.i)       y = MyClass()   print(y.f("D"))    print(y.name)   print(y.i)
def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList =...
def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList = []     for row in content [1:]:         line = row.split(",")         name = line[0]         population = float(line[1])         infectedPopulation = float(line[2])     for compareCountry in content:         comparePopulation = float (line[1])         compareInfected = float (line[2])     for name in countryList:         if population < comparePopulation:             if infectedPopulation > compareInfected:                 returnList.append(name)             else:                 return "No countries"     return returnList...
A probability density function on R is a function f :R -> R satisfying (i) f(x)≥0...
A probability density function on R is a function f :R -> R satisfying (i) f(x)≥0 or all x e R and (ii) \int_(-\infty )^(\infty ) f(x)dx = 1. For which value(s) of k e R is the function f(x)= e^(-x^(2))\root(3)(k^(5)) a probability density function? Explain.
Please use Python! def getText(file): This function should open the file named file, and return the...
Please use Python! def getText(file): This function should open the file named file, and return the contents of the file formatted as a single string. During processing, you should (i) remove any blank lines, (ii) remove any lines consisting entirely of CAPITALIZED WORDS , and (iii) replace any explicit ’\n’ (newline) characters with spaces unless directly preceeded by a ’-’ (hyphen), in which case you should simply remove both the hyphen and the newline, restoring the original word. def getText(file):...
How do I assign the resulting numbers from row and column in this function: def PlacePlayer(board,player):...
How do I assign the resulting numbers from row and column in this function: def PlacePlayer(board,player): row = DieRoller(1,6) col = DieRoller(1,6) board[row][col] = 'player' return board,player To the player['row'] and player['col'] in this function in place of the 0s: def GenPlayer(level): player = {} player['name'] = GenName() player['history'] = GenHistory() #history player['attack'] = DieRoller(3,6) + DieRoller(level,4) player['defense'] = DieRoller(3,6) + DieRoller(level,4) player['health'] = DieRoller(5,6) + DieRoller(level,4) player['row'] = 0 player['col'] = 0
Prove that {f(x) ∈ F(R, R) : f(0) = 0} is a subspace of F(R, R)....
Prove that {f(x) ∈ F(R, R) : f(0) = 0} is a subspace of F(R, R). Explain why {f(x) : f(0) = 1} is not.
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i...
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k decryptedText += chr(xor) return decryptedText def encrypt(message, key): encryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k encryptedText += chr(xor) return encryptedText # main function userText = input("Enter text: ") userKey = str(input("Enter a key: ")) encryptedMessage = encrypt(userText, userKey)...
let i be an interval in r, when is f said to be concave on i
let i be an interval in r, when is f said to be concave on i
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT