7

So I'm pretty upset I can't figure something this seemingly trivial out as I'm fairly well versed in Java, but anyways my professor for introduction to Python assigned us a lab where we have to create a pattern with letters based on row and column position. No loops or iterations, just conditional statements.

For instance, this function:

def letter(row, col):
   if row>col:
      return 'T'
   else:
      return 'W'

would yield:

WWWWWWWWWWWWWWWWWWWW
TWWWWWWWWWWWWWWWWWWW
TTWWWWWWWWWWWWWWWWWW
TTTWWWWWWWWWWWWWWWWW
TTTTWWWWWWWWWWWWWWWW
TTTTTWWWWWWWWWWWWWWW
TTTTTTWWWWWWWWWWWWWW
TTTTTTTWWWWWWWWWWWWW
TTTTTTTTWWWWWWWWWWWW
TTTTTTTTTWWWWWWWWWWW
TTTTTTTTTTWWWWWWWWWW
TTTTTTTTTTTWWWWWWWWW
TTTTTTTTTTTTWWWWWWWW
TTTTTTTTTTTTTWWWWWWW
TTTTTTTTTTTTTTWWWWWW
TTTTTTTTTTTTTTTWWWWW
TTTTTTTTTTTTTTTTWWWW
TTTTTTTTTTTTTTTTTWWW
TTTTTTTTTTTTTTTTTTWW
TTTTTTTTTTTTTTTTTTTW

if run through his driver file with row and col both equaling 20.

The one I'm stuck with is creating a function for the pattern:

XOOOOOX
OXOOOXO
OOXOXOO
OOOXOOO
OOXOXOO
OXOOOXO
XOOOOOX

Please do NOT spoonfeed me the answer, rather point me in the right direction.

So far I know that the X's for the left->right diagonal can be identified when row==col. It's the right->left diagonal I'm having issues with.

Thanks a lot.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Sam Nayerman
  • 135
  • 1
  • 1
  • 8
  • Can you clarify how the given function produces the given output - i.e., what is the "driver file" and how does one "run through" it? – TigerhawkT3 Apr 18 '15 at 01:19
  • 1
    In order to do the top-right -> bottom-left diagonal, you'll need to know the width (or height, since it's square). In your example, it's 7. That may be enough to get you started. – jedwards Apr 18 '15 at 01:21
  • Oh, and does your professor consider recursion to be in the same category as loops? :P – TigerhawkT3 Apr 18 '15 at 01:22
  • 1
    @TigerhawkT3: It's pretty much got to be equivalent to `print('\n'.join(''.join(letter(row, col) for col in range(colcount)) for row in range(rowcount)))` (except maybe it shuffles the iteration to catch programs that accidentally or intentionally cheat with state…). – abarnert Apr 18 '15 at 01:24
  • @TigerhawkT3 - Unfortunately I don't have access to the driver source code. @jedwards - Thanks! I've actually solved it by stating that `if row==col or row+col==6 then return 'X'`. I'm not particularly happy with this though because I don't want to have to rely on using a constant. But the general formula is row+col=n+1. – Sam Nayerman Apr 18 '15 at 01:25
  • Actually, speaking of "cheat with state"… I'm not sure how else you _do_ get the width/height? Either your program has to pass it to the driver, and you stash it in a global for `letter` to find, or `letter` keeps track of what it's seen so far and makes an assumption about the iteration order. So presumably one of those two doesn't count as "cheating". – abarnert Apr 18 '15 at 01:27
  • 1
    @abarnert My guess is that you choose it, so you tell the driver `rowcount`/`colcount` and you write your code with that assumption. At least that's what I got from "if run through his driver file with row and col both equaling 20." – jedwards Apr 18 '15 at 01:30
  • 1
    One last thing: If you're really a lot more comfortable in Java, write a `public static char letter(int row, int col)` implementation, then try to port it to Python. As a general rule, that's a bad way to write code, but when you're stuck, it can be a way to get unstuck. (I've frequently written Ruby or JS code by writing it in nice Python, porting it as bad Ruby/JS, then once I get it, throwing it out and rewriting it as idiomatic Ruby/JS…) – abarnert Apr 18 '15 at 01:31
  • @jedwards: Oh, duh, you're right, the answer is right there in the way the question is worded; I was overcomplicating things… – abarnert Apr 18 '15 at 01:31
  • @abarnert that would be some next-level introduction to Python course though :) – jedwards Apr 18 '15 at 01:32
  • 1
    @jedwards: "You may not use global variables. Hint: see `inspect.getsourcefile` and `ast.parse`". :) – abarnert Apr 18 '15 at 01:36

1 Answers1

6

Look at the relationship between the row and the column of each X's position. Then, split this problem in two: one aspect of it is the line that goes from top-left to bottom-right, and the other aspect is the line that goes from bottom-left to top right.

Let's look at the top-left to bottom-right Xs:

row: column:
1    1
2    2
3    3
4    4
5    5
6    6
7    7

I think you can determine a relationship between row and column based on that.

Now how about the other line, from bottom-left to top-right:

row: column:
1    7
2    6
3    5
4    4
5    3
6    2
7    1

Your gentle hint here is "+".

So, if an element's row and column have the first specified relationship or the second, you put an X there.

I hope that was a proper amount of help.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 1
    Great explanation. But I suspect `row` and `col` are 0-based, not 1-based. (Although the OP might learn something by working out what his code would do wrong if he guessed wrong each way, and then running it to verify…) – abarnert Apr 18 '15 at 01:34
  • Hey, thanks a lot! This was a great answer, and I appreciate the gentle hint, haha! – Sam Nayerman Apr 18 '15 at 01:38
  • I think that in this case the explanation is index-agnostic - it could've been `n`, `n+1`, etc. Also, I tested a function using this algorithm, running it through the driver you gave in that comment, and it behaved as expected. :) – TigerhawkT3 Apr 18 '15 at 01:39
  • @TigerhawkT3: Well, the code you have to write for 0-based vs. 1-based is going to be different here. But I guess you're right; to anyone who already understands the difference between 0-based and 1-based, your explanation will tell them all they need to know to write it either way. – abarnert Apr 18 '15 at 01:55
  • That was the idea. :) – TigerhawkT3 Apr 18 '15 at 02:06