variant def bomb_attack bomb_field numpy zeros with open bombs_list cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# variant #2
def bomb_attack (n, m):
bomb_field = numpy.zeros((n,n))
with open ("bombs_list.csv", "w") as f:
writer = csv.writer(f)
for i in range(m):
x = random.randint(0, n-1)
y = random.randint(0, n-1)
writer.writerow([f'{x}',f'{y}'])
for j in range(n):
bomb_field[x][j] += 1
if x != j:
bomb_field[j][y] += 1
numpy.savetxt('result_bomb_field.csv', bomb_field, delimiter=',', fmt='%d')
# bomb_attack(1000,500000)
# variant #3
def bombs_attack(n, m):
field = []
for i in range(n):
rowList = []
for j in range(n):
rowList.append('0')
field.append(rowList)
with open ("bombs_list_1.csv", "w") as f:
writer = csv.writer(f)
for s in range(m):
x = random.randint(0, n - 1)
y = random.randint(0, n - 1)
writer.writerow([f'{x}',f'{y}'])
for k in range(n):
field[x][k] = str(int(field[x][k]) + 1)
if x != k:
field[k][y] = str(int(field[k][y]) + 1)
print(f'{field}')
# bombs_attack(1000, 500000)