13 Replies - 2096 Views - Last Post: 09 December 2012 - 10:45 AM Rate Topic: -----

#1 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Word puzzle

Posted 05 December 2012 - 04:10 AM

Hi I need to find the position of a given word in grid,the grid lines are stored in list of strings.
for e.g
w_list=[
'btrekl',
'whrhio',
'aehjpk',
'pfdsmj']


The word to be search is let's say: word='the' and it is situated in the grid starting from 1 to line 3 in the second column
Any ideas how to search vertically for words?I was thinking to transform the grid into an array and then transpose the array and continue to search horizontally but in Python I don't know if it is possible.Thank you in advance.

This post has been edited by Simown: 05 December 2012 - 04:58 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Word puzzle

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Word puzzle

Posted 05 December 2012 - 05:34 AM

Since your list is strings, it really can be treated like a 2D array:
>>> w_list=[
... 'btrekl',
... 'whrhio',
... 'aehjpk',
... 'pfdsmj']
>>> w_list[1][1]
'h'
>>> w_list[2][1]
'e'
>>> 



If you're already searching horizontally on a grid, then vertically is the same. If you, instead, just used string functions horizontally, well, now you have to actually write code. ;)
Was This Post Helpful? 0
  • +
  • -

#3 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Word puzzle

Posted 05 December 2012 - 07:29 AM

Ok,I understood that..but how do I actually search for the word..I was thinking to find the first letter of the word in certain line then check for the second one in the line below and so on..but I don't know how to interate through the word letters and the grid lines so that it gets the right positioning of the word ..help please :)
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: Word puzzle

Posted 05 December 2012 - 08:09 AM

I would consider using a list comprehension. You want to generate a list of strings from the first characters in each row, the second characters in each row, etc. This can be done with a pretty straightforward listcomp.

Hint: you actually need to do two comprehensions, nested, and use a join.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Word puzzle

Posted 05 December 2012 - 08:48 AM

View Postirisbianca93, on 05 December 2012 - 09:29 AM, said:

I was thinking to find the first letter of the word in certain line then check for the second one in the line below and so on


Sounds good.

View Postirisbianca93, on 05 December 2012 - 09:29 AM, said:

but I don't know how to interate through the word letters


Hmm... You'd really just need to know that a string has a subscript, which you say you already do. And a length, which maybe you don't:
>>> s = 'word'
>>> for i in range(len(s)):
...     print i, s[i]
... 
0 w
1 o
2 r
3 d
>>> 



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Word puzzle

Posted 07 December 2012 - 05:54 AM

I just have one more question,i'm trying to search for a particular word now diagonally,for example a diagonal with positive gradient going downwards like SE directioning.This is my code so far:
rows=len(line_list)
word='data'
L=len(word)
for element in line_list:
    columns=len(element)
    word_checked=0
    while word_checked==0:
        if word[0] in element:
            #position of the first letter in the row and column of the grid
            pos_r=line_list.index(element)
            pos_c=element.index(word[0])
            found_letters=1
            #search for the missing letters 
            i=1
            while i<len(word):
                if (pos_r+i)<=rows and (pos_c+i)<=columns:
                    if line_list[pos_r+i][pos_c+i].find(word[i]):
                        found_letters=found_letters+1
                        
                        
                i=i+1
                
            if found_letters==len(word)-1:
                print('word found starting from line: ',pos_r+1,'column: ',pos_c+1)
                print('and ending in line: ',pos_r+len(word),'column :',pos_c+len(word))
                word_checked=1

I'm trying to stop the search if the word is found but it doesn't seem to work at all and if don't do that the search continues and I get an error telling me that the index of the list is out of range.Any ideas how to modify it?

So sorry I forgot to put the code between tags..

[code]

rows=len(line_list)
#diagonal downwards search,negative gradient for the diagonal line
word='data'
L=len(word)
for element in line_list:
    columns=len(element)
    word_checked=0
    while word_checked==0:
        if word[0] in element:
            #position of the first letter in the row and column of the grid
            pos_r=line_list.index(element)
            pos_c=element.index(word[0])
            found_letters=1
            #search for the missing letters 
            i=1
            while i<len(word):
                if (pos_r+i)<=rows and (pos_c+i)<=columns:
                    if line_list[pos_r+i][pos_c+i].find(word[i]):
                        found_letters=found_letters+1
                        
                        
                i=i+1
                
            if found_letters==len(word)-1:
                print('word found starting from line: ',pos_r+1,'column: ',pos_c+1)
                print('and ending in line: ',pos_r+len(word),'column :',pos_c+len(word))
                word_checked=1





Oh and the line_list is a list that contains strings as elements,simulating in this way a 2D array.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: Word puzzle

Posted 07 December 2012 - 07:06 AM

Can you show us the code you ended up using to do the verticals? There are a number of ways to do this sort of thing, it's probably easiest for you if we use something that's related to what you're already doing.

Posting during meetings = bad idea.

This post has been edited by jon.kiparsky: 07 December 2012 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Word puzzle

Posted 07 December 2012 - 07:19 AM

This:
if (pos_r+i)<=rows and (pos_c+i)<=columns:



Should almost certainly be this:
if (pos_r+i)<rows and (pos_c+i)<columns:



Stop +1 all over the place; everything starts at zero.

I'm also dubious about your while word_checked==0:. I don't think you need it or want it. If you do need it, it probably should be boolean.

This post has been edited by baavgai: 07 December 2012 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#9 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Word puzzle

Posted 07 December 2012 - 02:46 PM

I've modified them into Boolean variables but now the program doesn't display anything at all ..
Was This Post Helpful? 0
  • +
  • -

#10 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Word puzzle

Posted 07 December 2012 - 03:02 PM

if line_list[pos_r+i][pos_c+i].find(word[i])

This line was giving me the error with index out of range,is there any way to put a condition that will restrain the checking so it won't go further after it finds the word to be searched?
Was This Post Helpful? 0
  • +
  • -

#11 woooee  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 88
  • Joined: 21-November 12

Re: Word puzzle

Posted 07 December 2012 - 03:20 PM

For diagonals especially, it is probably easier to create a list and compare. Groups of rows or columns "words" would be much easier since it is only one dimension. This code creates diagonals for top to bottom and left to right. Note that a word may be a subset of what is returned. I have changed the original list so one of the first row diagonals contains "thej" which includes the word "the". You appear to be in over your head on this one so you might consider doing something easier instead.
def one_diagonal(row, col, w_list):
    len_w_list=len(w_list)
    diagonals = []
    ltr_found = True
    start_word = w_list[row]
    word=[]
    while ltr_found:
        if row < len_w_list and col < len(w_list[row]):
            word.append(w_list[row][col])
        else:
            ltr_found=False
        row += 1
        col += 1

    return word

w_list=['btrekl',
        'whrhio',
        'aehjpk',
        'pfdsmj']

for row in range(len(w_list)):
    for col in range(len(w_list[row])):
        print row, col,
        print "".join(one_diagonal(row, col, w_list)) 

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Word puzzle

Posted 07 December 2012 - 04:16 PM

This is a rather strange line:
if line_list[pos_r+i][pos_c+i].find(word[i]) 



You really want:
if line_list[pos_r+i][pos_c+i] == word[i]: 



You aren't using find. You're breaking up your string intro chars. You are writing your own find.
Was This Post Helpful? 0
  • +
  • -

#13 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Word puzzle

Posted 07 December 2012 - 05:22 PM

Oh I got thank you that's such a smart way of solving things..getting the possible diagonals first :) thank you I'll try and combine the idea that you gave with some of the code that I had before..thank you again guys!
Was This Post Helpful? 0
  • +
  • -

#14 woooee  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 88
  • Joined: 21-November 12

Re: Word puzzle

Posted 09 December 2012 - 10:45 AM

Note that the diagonals program only produces one half of the diagonals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1