Jayanth Koushik
Jayanth Koushik

Reputation: 9904

Printing numbers in a diamond shape

I came across this problem: http://codegolf.com/numeric-diamonds which requires you to print numbers in this format:

  1
 4 2
7 5 3
 8 6
  9
           1
         7   2
      13   8   3
    19  14   9   4
  25  20  15  10   5
31  26  21  16  11   6
  32  27  22  17  12
    33  28  23  18
      34  29  24
        35  30
          36
                             1
                         11     2
                      21    12     3
                   31    22    13     4
                41    32    23    14     5
             51    42    33    24    15     6
          61    52    43    34    25    16     7
       71    62    53    44    35    26    17     8
    81    72    63    54    45    36    27    18     9
 91    82    73    64    55    46    37    28    19    10
    92    83    74    65    56    47    38    29    20
       93    84    75    66    57    48    39    30
          94    85    76    67    58    49    40
             95    86    77    68    59    50
                96    87    78    69    60
                   97    88    79    70
                      98    89    80
                         99    90
                           100

I was able to find the numbers that go into a particular row just fine, but my logic for arranging them is quite obscure.

for i in range(2*n - 1):
    m = <list of numbers for this row>
    row_width = (n+i)*(n/3) if i < n else row_width - (n/3)

    print ''.join(str(i).rjust(n/2 + 1) for i in m).strip().rjust(row_width))

That's ok from a code golf perspective, but is there a clean, pythonic way to arrange numbers from a 2D array in this format?

Upvotes: 2

Views: 1485

Answers (3)

thinker3
thinker3

Reputation: 13301

def print_diamond(n):
    if n == 1:
        print 1
        return
    maxlen = len(str(n*n))
    gap =  maxlen * ' '
    first = 0
    for i in xrange(2*n-1):
        if i < n:
            first = i*n + 1
            print (abs(i-n)-1)*gap + gap.join(map(lambda x: '{0:{1}}'.format(x, maxlen), xrange(first, i, 1-n)))
        else:
            first += 1
            print (abs(i-n)+1)*gap + gap.join(map(lambda x: '{0:{1}}'.format(x, maxlen), xrange(first, n*(i+2-n)-1, 1-n)))

Upvotes: 0

Chris Hagmann
Chris Hagmann

Reputation: 1096

So the function M returns the values of the number in each row (e.g. for n=10, then M(n) = [[1],[11,2],...]]. The cell function make every number take up as much space as the largest number does(learned for selected answer).

def M(n):
    t = 2 * n - 1
    N = [range(i * n + 1, (i + 1) * n + 1) for i in range(n)]

    M = []
    for k in xrange(t):
        M.append([])
        for i in reversed(xrange(n)):
            for j in reversed(xrange(n)):
                if i + j == k:
                    M[k].append(N[i][j])
    return M



def magic_square(n):
    t = 2 * n - 1
    maxlen = len(str(n * n))
    gap = ' ' * maxlen

    cell = lambda num:  '{0:{1}}'.format(num, maxlen)

    for m in M(n):
        padding = gap  * ((t - (2 * len(m) - 1)) / 2)
        print padding + gap.join(map(cell, m)) + padding

Upvotes: 0

georg
georg

Reputation: 214989

Don't know about "pythonic", but this looks quite clean to me:

size = 10
maxlen = len(str(size * size))
m = size * 2 - 1
matrix = [[' ' * maxlen] * m for _ in range(m)]

for n in range(size * size):
    r = n // size
    c = n % size
    matrix[c + r][size - r - 1 + c] = '{0:{1}}'.format(n + 1, maxlen)

print '\n'.join(''.join(row) for row in matrix)

Upvotes: 3

Related Questions