简体   繁体   中英

How to create a 2D array in python forming a diamond?

How can I create a 2d array in python without using any libraries (no numpy or dataframes)? That is, only using vanilla python.

It must result in an by n full diamond, where even-shaped diamonds are two hashes wide and odd-sized diamonds are one hash wide at their points, for example:

diamond(5) returns
    [[' ', ' ', '#', ' ', ' '],
     [' ', '#', '#', '#', ' '],
     ['#', '#', '#', '#', '#'],
     [' ', '#', '#', '#', ' '],
     [' ', ' ', '#', ' ', ' ']]
diamond(6) returns
    [[' ', ' ', '#', '#', ' ', ' '],
     [' ', '#', '#', '#', '#', ' '],
     ['#', '#', '#', '#', '#', '#'],
     ['#', '#', '#', '#', '#', '#'],
     [' ', '#', '#', '#', '#', ' '],
     [' ', ' ', '#', '#', ' ', ' ']]

So far, the best I could come up with was:

HASH = "#"
SPACE = " "
def diamond(n):
    if n % 2 == 0:
        return [
            [HASH if (i+1 == n//2) or (i == n//2) or (j == n//2) or (j+1 == n//2)
             or (i > 0 and j>0 and i!=j and (i%j ==0 or j%i ==0))
             else SPACE for i in range(n)
            ] 
            for j in range(n)
        ]
    return [
        [HASH if (i == n//2) or (j == n//2)
         or (i > 0 and j>0 and  (i%j ==0 or j%i ==0))
         else SPACE for i in range(n) 
        ] for j in range(n)
    ]

but it does not work well yet.

I also have seen this answer: 2d array diamond shape of 1's of size x . But it does use numpy.

You could do it like this:

def diamond(n):
    if n% 2:
        result = [[' ']*i+['#']*(n-(2*i))+[' ']*i for i in range(n//2, -1,-1)]
        return result + result[:-1][::-1]

    result = [[' ']*i+['#']*(n-(2*i))+[' ']*i for i in range(n//2-1, -1, -1)]
    return result + result[::-1]

diamond(5) :

[[' ', ' ', '#', ' ', ' '],
 [' ', '#', '#', '#', ' '],
 ['#', '#', '#', '#', '#'],
 [' ', '#', '#', '#', ' '],
 [' ', ' ', '#', ' ', ' ']]

diamond(6) :

[[' ', ' ', '#', '#', ' ', ' '],
 [' ', '#', '#', '#', '#', ' '],
 ['#', '#', '#', '#', '#', '#'],
 ['#', '#', '#', '#', '#', '#'],
 [' ', '#', '#', '#', '#', ' '],
 [' ', ' ', '#', '#', ' ', ' ']]

try this, you start by adding the middle full line and reduce 1 column every time you step down a row until you reach the bottom (n//2), then you flip the list and add it to the original list, creating the full diamond

def diamond(n):
    # define the full line in the middle
    hash_line = ["#"]*n
    # this will hold the diamond shape
    d_list = []
    # start by adding the hash_line
    d_list.append(hash_line)
    # add one half of the diamond, reducing one column each time you step down
    for i in range(n//2):
        line = hash_line.copy()
        line[:i+1] = [' ']*(i+1)
        line[-1*(i+1):] = [' ']*(i+1)
        d_list.append(line)
    # now you have the bottom half of the diamond
    # if n is even flip d_list and add it in the beginning of the original d_list
    if n%2 == 0:
        final = d_list[::-1]+d_list
        return final[1:-1]
    # if n is odd flip d_list without the hash_line which is held in the first index
    return d_list[1:][::-1]+d_list

d = diamond(5)
print(d)
d = diamond(6)
print(d)

After realising that a diamond simply is a ball wrt. L1 metric you can put '#' when distance from the centre is half of the size of the array, ie |x-center_x| + |y-center_y| < half_screen which gives a one liner solution

def diamond(n):
    return [['#' if (abs(i-(n+1)/2) + abs(j- (n+1)/2)) <  (n+1)/2 else ' '
             for i in range(1, n+1)] for j in range(1, n+1)]

and then

print('\n'.join(map(str,diamond(5))))


[' ', ' ', '#', ' ', ' ']
[' ', '#', '#', '#', ' ']
['#', '#', '#', '#', '#']
[' ', '#', '#', '#', ' ']
[' ', ' ', '#', ' ', ' ']



print('\n'.join(map(str,diamond(6))))


[' ', ' ', '#', '#', ' ', ' ']
[' ', '#', '#', '#', '#', ' ']
['#', '#', '#', '#', '#', '#']
['#', '#', '#', '#', '#', '#']
[' ', '#', '#', '#', '#', ' ']
[' ', ' ', '#', '#', ' ', ' ']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM