Last active
September 20, 2019 22:01
-
-
Save 221V/c9f752e3955412d6690d273915e14789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = int(input()) | |
matrix = [['.']*n]*n | |
for i in range(0,n): | |
for j in range(0,n): | |
if i == j : | |
matrix[i][j] = '*' | |
print(matrix[i][j],sep='',end=' ') | |
print('') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n = 5; | |
var matrix = []; | |
for(var i=0;i<n;i++){ matrix[i] = []; for(var j=0;j<n;j++){ matrix[i][j]='.'; } } | |
for(var i=0;i<n;i++){ | |
for(var j=0;j<n;j++){ | |
if(i == j){ | |
matrix[i][j]='*'; | |
} | |
} | |
} | |
for(var i=0;i<n;i++){ | |
var rz = ''; | |
for(var j=0;j<n;j++){ | |
rz = rz + matrix[i][j] + ' '; | |
} | |
console.log(rz); | |
} | |
/* | |
* . . . . | |
. * . . . | |
. . * . . | |
. . . * . | |
. . . . * | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 5 | |
matrix = [['']*n]*n | |
for i in range(0,n): | |
for j in range(0,n): | |
if i == j : | |
matrix[i][j] = '*' | |
else : | |
matrix[i][j] = '.' | |
print(matrix[i][j],sep='',end=' ') | |
print('') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in range(0,n): | |
for j in range(0,n): | |
print(i, j) | |
print(matrix[i][j]) | |
if i == j : | |
matrix[i][j] = '*' | |
print(matrix[i][j]) | |
print('') | |
=> | |
0 0 | |
. | |
* | |
0 1 | |
. | |
. | |
0 2 | |
. | |
. | |
0 3 | |
. | |
. | |
0 4 | |
. | |
. | |
1 0 | |
* | |
* | |
1 1 | |
. | |
* | |
1 2 | |
. | |
. | |
1 3 | |
. | |
. | |
1 4 | |
. | |
. | |
2 0 | |
* | |
* | |
2 1 | |
* | |
* | |
2 2 | |
. | |
* | |
2 3 | |
. | |
. | |
2 4 | |
. | |
. | |
3 0 | |
* | |
* | |
3 1 | |
* | |
* | |
3 2 | |
* | |
* | |
3 3 | |
. | |
* | |
3 4 | |
. | |
. | |
4 0 | |
* | |
* | |
4 1 | |
* | |
* | |
4 2 | |
* | |
* | |
4 3 | |
* | |
* | |
4 4 | |
. | |
* | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[Forwarded from Mikhail Kovalev]
Когда пишешь так (matrix = [['.']*n]*n), то у тебя получается по факту N строк в матрице, которые ссылаются на одну и ту же строку, поэтому меняя что-либо в одной строке у тебя поменяется во всех строках
Исправь на matrix = [['.' for _ in range(n)] for _ in range(n)]
[Forwarded from Alexander]
Немного не так. Строки иммутабельны. Проблема во втором умножении листа. Лист листов , умноженный на n создаёт n ссылок на один и то же лист
[1]*3
тут все ок
[[1]] * 3
тут создаётся лист из трех ссылок на [1]