Python word search

Asked

Viewed 895 times

6

Guys, I’m trying to do a word search. The first lines should contain letters with white space, then speak the number of words and say which word. The output should contain the diagram with the word found, replacing the rest of the letters with "." and say how many times the words appeared.

In total I have 3 errors in my code( if not, more). My code is this:

l = input()
d= []
l_palavras=[]
palavras=[]

while not l.isdigit():
  l= l.split()
  coluna= len(l)
  d.append(l)
  l = input()

linha= len(d)
n_palavras= int(l)

d2 = [['.' for j in range(coluna)] for i in range(linha)]


for i in range(0,n_palavras):
  palavras= list(input())
  l_palavras +=[palavras]



for m in range(0,n_palavras):


#NORTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i-k][j] = l_palavras[m][k]

          k= i
          achou= True


#SUL

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i+k][j] = l_palavras[m][k]

          k= i 
          achou= True 


#OESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i][j-k] = l_palavras[m][k]

          k= i
          achou= True


#LESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i][j+k] = l_palavras[m][k]

          k= i
          achou= True  


#NOROESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i-k][j+k] = l_palavras[m][k]  

          k= i
          achou= True  


#NORDESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i-k][j-k] = l_palavras[m][k]

          k= i
          achou= True


#SUDOESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i+k][j-k] = l_palavras[m][k]

          k= i
          achou= True


#SUDESTE

    for i in range(0,linha):
      for j in range(0,coluna):

        if l_palavras[m][0] == d[i][j]:    
          achou = True
          k = i

          for l in l_palavras[m]:
            if k < 0 or d[k][j] != l:
              achou = False
              break
            k = k - 1

          if achou:
            for k in range(len(l_palavras[m])):
              d2[i+k][j+k] = l_palavras[m][k]




for i in range(len(d2)):
  for j in range(len(d2[i])):
    print(d2[i][j], end=' ')
  print()

my problem is this, I have several word search with various formats, for example, when I put this word search:

g z h y z l y n s g q j u
i n w a k b n x w w t n y
i w o u i p o f d b o y o
m k o h k l h r k h j w u
j l i o t r t c t b c o p
a h e d n y y y e g t p t
f n o h t y p y t h o n f
r l q o s y y y h n a f x
c e z x t k t m t x e u z
t g m h c v h x c h j n a
s n o w t q o b v n o z j
u n u x x a n p u i g n b
s i x o d j v s f e o n z
1
python

the output is that(the one that really should be):

. . . . . . . . . . . . .
. n . . . . n . . . . n .
. . o . . . o . . . o . .
. . . h . . h . . h . . .
. . . . t . t . t . . . .
. . . . . y y y . . . . .
. n o h t y p y t h o n .
. . . . . y y y . . . . .
. . . . t . t . t . . . .
. . . h . . h . . h . . .
. . o . . . o . . . o . .
. n . . . . n . . . . n .
. . . . . . . . . . . . .
python: 8

But when I change the word to be sought, for example: Thon

g z h y z l y n s g q j u
i n w a k b n x w w t n y
i w o u i p o f d b o y o
m k o h k l h r k h j w u
j l i o t r t c t b c o p
a h e d n y y y e g t p t
f n o h t y p y t h o n f
r l q o s y y y h n a f x
c e z x t k t m t x e u z
t g m h c v h x c h j n a
s n o w t q o b v n o z j
u n u x x a n p u i g n b
s i x o d j v s f e o n z
1
thon

how output appears:

. . . . . . . . . . . . .
. . . n . . n . . n . . .
. . . . o . o . o . . . .
. . . . . h h h . . . . .
. . . n o h t h o n . . .
. . . . . h h h . . . . .
. . . . o . o . o . . . .
. . . n . . n . . n . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

if I try other word hunting, for example:

t c b i n t r o d u c a o
s a m b h r u o u f m e g
a l g o r i t m o z f x j
h k p r o g j c h m l h f
j s f w g p r o g r a m a
m a q j q q p v e w v g p
3
algoritmo
programa
introducao

The exit appears only the dots:

. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

another problem I found, is when I try this word search, an error appears:

l c n x i s u p i i
t u u e z a q s n i
o q s t i m a i o r
o a l e c n s w h s
f t a l e i r f t c
e e g c t f b m y j
s a m a r g o r p i
c b a r e m t j r a
2
programa
python

that’s the mistake:

Traceback (most recent call last):
  File "main.py", line 62, in <module>
    d2[i+k][j] = l_palavras[m][k]
IndexError: list index out of range
  • 1

    This means either i+k, j, m or k are passing the size of the list (which does not necessarily have to do with the point question). It would be interesting to simplify posting or provide a [mcve] to get more viable help. Try printing the values on the screen inside this loop to see which one is "popping"

  • I was as detailed as possible.Since I’m new, I don’t know how to reduce my code, sorry.About the code, even if I fix this error, which is off the range, it print this word hunt as dots like the other, it won’t go?

  • Ta well detailed yes (better than the average inclusive), but is mixing different situations and was complex by excess code. The ideal would be to test each block separately and correct one by one (make it work that picks up the words horizontally, then do only what picks up the inverted horizontal, etc). I think it would facilitate your debug. Testing with a smaller word search would help to have less output on the screen as well. On the ranges, the print would help you locate the problem (but this I speak independent of the post here on Sopt).

  • https://answall.com/q/411417/70

  • I’ll try to fix the range problem first so thanks for the help.

  • And try to run each axis separately, then you put it together in one code. Probably after you understand the range and each axis well, you will be able to simplify the code and decrease the repetitions by reusing the internal loop

  • It’s just that I was very curious, by working only on the first word hunt perfectly, and the rest appear only the points.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.