Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you probably want something like</p> <pre><code>def get_diagonal(t, x, y, length=4): rays = [] diagonal_directions = [(1,1),(1,-1),(-1,1),(-1,-1)] for dx, dy in diagonal_directions: if not ((0 &lt;= (x+dx*(length-1)) &lt; len(t[0])) and (0 &lt;= (y+dy*(length-1)) &lt; len(t))): ray = [] else: ray = [t[y+dy*i][x+dx*i] for i in range(length)] rays.append(ray) return rays </code></pre> <p>[Better: remove <code>rays</code> entirely and simply <code>yield</code> each ray.]</p> <p>I always mix up <code>x</code> and <code>y</code> when doing this stuff, so you'll want to check that, but I think something like this should work. Example:</p> <pre><code>In [48]: get_diagonal(t, 0, 0) Out[48]: [[8, 49, 31, 23], [], [], []] In [49]: get_diagonal(t, 19, 19) Out[49]: [[], [], [], [48, 5, 4, 40]] In [50]: get_diagonal(t, 5, 5) Out[50]: [[3, 67, 20, 97], [3, 63, 42, 93], [3, 64, 68, 58], [3, 51, 23, 31]] In [51]: get_diagonal(t, 5, 5, 3) Out[51]: [[3, 67, 20], [3, 63, 42], [3, 64, 68], [3, 51, 23]] </code></pre> <p>You can change the order of <code>diagonal_directions</code> to match your expectations. Also, note that I'm returning <code>[]</code> instead of <code>[0,0,0,0]</code> if you don't have enough terms. Returning <code>[0,0,0,0]</code> is a bad idea, ISTM -- how would you distinguish between not having enough terms and having the right number of terms but they're all zero? It feels weird to me to load that logic into the diagonal function, but you could easily change that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload