Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine letter based on row and col - python

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.

like image 937
Sam Nayerman Avatar asked Apr 18 '15 01:04

Sam Nayerman


1 Answers

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.

like image 131
TigerhawkT3 Avatar answered Oct 10 '22 09:10

TigerhawkT3