#🔒 Hidden word problem

13 messages · Page 1 of 1 (latest)

merry basin
#

Task:

Karol and Tomek are playing a game where they hide a word in a 10x10 grid. The word is hidden by placing the first letter in a random cell, then the next letter in an adjacent cell (up, down, left, or right). Each letter must be placed in a unique cell, and the rest of the grid is filled with random letters.

Tomek's job is to find the hidden word in the grid. The grid is represented as a string of 100 letters (10 rows, 10 columns). Your task is to write a program that reads the grid and the hidden word, and finds the position of the first letter of the word in the grid. If the word appears multiple times, return the smallest position. If the word is not found, return -1.

Input:

  1. A string of 100 uppercase letters representing the 10x10 grid (first 10 letters = row 1, next 10 = row 2, etc.).
  2. A string representing the hidden word (1 to 100 characters long).

Output:

The position of the first letter of the word in the grid (1 to 100), or -1 if the word is not found.


umbral pagodaBOT
#

@merry basin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

midnight horizon
#

Have you tried solving the problem? Can you share the code?

merry basin
#

‛‛
def sasiedzi(nr):
proby = []
if nr % 10 != 0:
proby.append(nr - 1)
if nr % 10 != 9:
proby.append(nr + 1)
if nr >= 10:
proby.append(nr - 10)
if nr < 90:
proby.append(nr + 10)
return proby

def sprawdzarka(nr, n, cel, used):
curr = nr
war = False
#kawalek = 1
for i in range(1, len(cel)):
war = False
for prawdopodobienstwo in sasiedzi(curr):
if n[prawdopodobienstwo] == cel[i] and used[curr]:
curr = prawdopodobienstwo
used[curr] = False
war = True
break
if not war:
return False
return True
n = input()
cel = input()
tekst = [n[i:i+10] for i in range(0, 100, 10)]
i = 0
kawalek = cel[0]
result = -1
possibleLocations = [i for i in range(len(n)) if n[i] == kawalek]
for location in possibleLocations:
if sprawdzarka(location, n, cel, ([True] * len(n))):
result = location + 1
break
print(result)
‛‛

#

sorry for weird varibles names it's just that original exercise is in polish

midnight horizon
#

Can you explain what's happening in your code and what help you need?

merry basin
#

so at first i made a function that counts the neighbours of the number then i made anothjer funcition tghat checks if the following possdible location is ok the funcition goes through the possible nieghoburs and returns if its correct or not then i take an input and make a varilbe with all the possible locationbs and i go through every location adn i check tevery loication with the funcityion

winter crypt
#

With your current approach, you'll try to bounce back to previous letters - you don't check against what is already stored, so e.g. alamakota can be found by your idea in

lam
xko
xat

Reusing the first a 3 times. But the task says each letter is encoded separately.

Your variable names also seem off and don't help reading the code. "prawdopodobieństwo" is not intuitive (the meaning is probability, like numeric value of chance, rather than "possible option"), and just too long and makes the code hard to read.

#

I think backtracking might work here - basically a DFS (since such grid is a specific type of graph), so from a given a start point you don't repeat the paths (and with nicely stored current partial solution, you have an easy check if the character is already there in the solution or not).

#

Btw, where is the task from? Just making sure it's not some current competition (it's March, so it shouldn't be) and curious where you're learning from :3 (reminds me of my school times and given Polish variable names, I just need to know)

umbral pagodaBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.