views:

101

answers:

4

I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?

def checkRow(table, r, pos, word):
    for i in range(0, len(word)):
        if table[r][pos+i] != word[i]:
            return False
    return True

a sample table would be like this:

[
  ['a','p','p','l','e','b'],
  ['u','y','c','v','a','s'],
  ['n','u','t','o','n','s'],
  ['t','n','c','v','d','b'],
  ['o','r','i','x','o','f'],
  ['e','a','t','i','n','g']
]
+4  A: 

Isn't it simple like this:

def checkCol(table, r, pos, word):
   for i in range(0, len(word)):
       if table[r+i][pos] != word[i]:
           return False
   return True
Jiri
If you must use explicit loop then use at least `all` or `any` for readability: `return all(table[r+i][pos] == w for i, w in enumerate(word))`
J.F. Sebastian
@Jiri's implementation of `checkCol()` is perfectly readable as is. It uses a very standard idiom for checking things in loops. Also, importantly, it is very similar to the answer @Jack already has, and he can learn something by comparing the two. If I were teaching an introduction to programming class, this is how I would write `checkCol()` (in *any* programming language). Combining `all()`, `enumerate()`, and a generator expression is not how I would answer this question to a beginning student. That said, if @Jack can learn the advanced answer too, so much the better.
steveha
@steveha: I would agree if the question were tagged `language-agnostic` but it is tagged `python` therefore it is more appropriate to use Python's idioms.
J.F. Sebastian
@J.F. Sebastian, we are just going to have to disagree on this one.
steveha
I agree with @steveha - I could write some more "pythonic" code indeed, but question (and code example!) was so trivial that I believe he was was looking for some trivial solution that one can learn from.
Jiri
@Jiri: How the OP is supposed to learn Python if you are showing him C code using Python syntax (there is more to language than a mere syntax). The beginner by definition can't judge by himself what is preferable way to accomplish a particular task. In other words you can write Java in many languages but you should know better.
J.F. Sebastian
+4  A: 
import itertools

def checkRow(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r:][pos]))

The OP indicates "they haven't learned about import yet" so they'd rather reinvent the wheel than reuse functionality in the standard library. In general, that would be a pretty absurd stance, but in this case it ain't even too bad:

def checkRow(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r:][pos]))

I hope at least builtins such as all and zip are acceptable -- or would the OP rather code binary machine language down to the bare metal to avoid learning some Python?-)

Alex Martelli
http://docs.python.org/library/itertools.html#itertools.izip
Crescent Fresh
is there a way to do it without importing python tools? like the format i have right now? I'm new to python, so I've never learned to use import
Jack
@Jack, yes, you can laboriously handcode inferior versions of most of the wonderful functionality you could instead `import` from Google's rich standard library -- almost invariably, they'll be slower, you'll have bugs and waste lots of time tracking them down, you'll do ten+ times the amount of work you'd have to do, but Python doesn't **force** you do to the only sane thing, which is to learn about `import` and start learning about the incredible wealth of solid, fast, useful stuff the standard library offers: you can be crazy and recode it all. Let me edit the answer accordingly.
Alex Martelli
@Alex: GOOGLE's standard library?! Google didn't write the python libs.
Lee B
@Alex Martelli, I am a big fan. Usually when I see an answer from you, I don't bother trying to write an answer of my own, because I know you have nailed it, so I just give you another +1. But not this time. An introductory programming class is about learning to do things, not about how to master a particular language's built in wonderful features. If he was asking you to help him debug a BubbleSort algorithm, would you chide him for not using the amazing "Timsort" built in to Python? @Jiri's answer seems, to me, better for a "homework" sort of question.
steveha
@Jack: if you do study Alex Martelli's answer, to the point where you understand the whole thing, you will learn some really cool features of Python. The way a generator expression works hand-in-hand with the builtin `all()` is neat, and both `zip()` and `itertools.izip()` are slick. You can trust Alex Martelli to solve a Python problem in the most elegant way. If you don't understand this now, be sure to come back and read it once you have completed your class where you are learning Python. I also encourage you to get a good book and really study Python.
steveha
@Lee, heh, you're right, funny typo on my part;-). @steveha, I'm all for understanding lower levels of abstraction, but if an intro to programming class is held in Python, the choice is to start from pretty high levels instead (and move down later), otherwise it would be better to start from lower-level languages and move up later.
Alex Martelli
+1  A: 
def intable(table, word):
    if any(word in ''.join(row) for row in table):          # check rows
       return True
    return any(word in ''.join(col) for col in zip(*table)) # check columns
J.F. Sebastian
That's really beautiful. For Python 2.x, I'd suggest using `itertools.izip()` instead of `zip()`. This is probably the fastest way to write `intable()` in Python: let the built-in features do all the heavy lifting!
steveha
+2  A: 
def checkRow(table, r, pos, word):
    return word=="".join(table[r][pos:pos+len(word)])

def checkColumn(table, r, pos, word):
    return word=="".join(row[pos] for row in table[r:r+len(word)])
gnibbler