How to create a 2D array in python forming a diamond?
Question:
I’m trying to produce an n by n full diamond using python, but I’m having trouble making it fully work.
If n is even, the result is two hashes wide and one hash wide if n is odd.
diamond(3) returns
[['0', '1', '0'],
['1', '1', '1'],
[' ', '1', ' ']]
So far, the best I could come up with was:
def my_diamond(n):
if n % 2 == 0:
return [
[“1” 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 “0” for i in range(n)
]
for j in range(n)
]
return [
[“1” if (i == n//2) or (j == n//2)
or (i > 0 and j>0 and (i%j ==0 or j%i ==0))
else “0” for i in range(n)
] for j in range(n)
]
but it does not work well yet. I’m only making a cross and the rest is not yet in place.
I also have seen this answer: 2d array diamond shape of 1's of size x . I got the idea, but it does use numpy.
Answers:
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)
:
[[' ', ' ', '#', '#', ' ', ' '],
[' ', '#', '#', '#', '#', ' '],
['#', '#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#', '#'],
[' ', '#', '#', '#', '#', ' '],
[' ', ' ', '#', '#', ' ', ' ']]
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, i.e. |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))))
[' ', ' ', '#', '#', ' ', ' ']
[' ', '#', '#', '#', '#', ' ']
['#', '#', '#', '#', '#', '#']
['#', '#', '#', '#', '#', '#']
[' ', '#', '#', '#', '#', ' ']
[' ', ' ', '#', '#', ' ', ' ']
I’m trying to produce an n by n full diamond using python, but I’m having trouble making it fully work.
If n is even, the result is two hashes wide and one hash wide if n is odd.
diamond(3) returns
[['0', '1', '0'],
['1', '1', '1'],
[' ', '1', ' ']]
So far, the best I could come up with was:
def my_diamond(n):
if n % 2 == 0:
return [
[“1” 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 “0” for i in range(n)
]
for j in range(n)
]
return [
[“1” if (i == n//2) or (j == n//2)
or (i > 0 and j>0 and (i%j ==0 or j%i ==0))
else “0” for i in range(n)
] for j in range(n)
]
but it does not work well yet. I’m only making a cross and the rest is not yet in place.
I also have seen this answer: 2d array diamond shape of 1's of size x . I got the idea, 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)
:
[[' ', ' ', '#', '#', ' ', ' '],
[' ', '#', '#', '#', '#', ' '],
['#', '#', '#', '#', '#', '#'],
['#', '#', '#', '#', '#', '#'],
[' ', '#', '#', '#', '#', ' '],
[' ', ' ', '#', '#', ' ', ' ']]
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, i.e. |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))))
[' ', ' ', '#', '#', ' ', ' ']
[' ', '#', '#', '#', '#', ' ']
['#', '#', '#', '#', '#', '#']
['#', '#', '#', '#', '#', '#']
[' ', '#', '#', '#', '#', ' ']
[' ', ' ', '#', '#', ' ', ' ']