-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path022_Find the Mine!.py
91 lines (59 loc) · 2.58 KB
/
022_Find the Mine!.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Codewars Coding Challenge
Find the Mine!
You've just discovered a square (NxN) field and you notice a warning sign. The sign states that there's a single bomb in the 2D grid-like field in front of you.
Write a function mineLocation/MineLocation that accepts a 2D array, and returns the location of the mine. The mine is represented as the integer 1 in the 2D array. Areas in the 2D array that are not the mine will be represented as 0s.
The location returned should be an array (Tuple<int, int> in C#, RAX:RDX in NASM) where the first element is the row index, and the second element is the column index of the bomb location (both should be 0 based). All 2D arrays passed into your function will be square (NxN), and there will only be one mine in the array.
Examples:
mineLocation( [ [1, 0, 0], [0, 0, 0], [0, 0, 0] ] ) => returns [0, 0]
mineLocation( [ [0, 0, 0], [0, 1, 0], [0, 0, 0] ] ) => returns [1, 1]
mineLocation( [ [0, 0, 0], [0, 0, 0], [0, 1, 0] ] ) => returns [2, 1]
https://www.codewars.com/kata/528d9adf0e03778b9e00067e/train/python
"""
# My Solution
def mine_location(field):
for row_idx, row in enumerate(field):
for col_idx, cell in enumerate(row):
if cell == 1:
return [row_idx, col_idx]
# print(mine_location( [ [1, 0, 0], [0, 0, 0], [0, 0, 0] ]))
# print(mine_location( [ [0, 0, 0], [0, 1, 0], [0, 0, 0] ]))
# print(mine_location( [ [0, 0, 0], [0, 0, 0], [0, 1, 0] ]))
# Sample test
"""
import codewars_test as test
try:
from solution import mineLocation as mine_location
except:
from solution import mine_location
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(mine_location([ [1, 0], [0, 0] ]), [0, 0])
test.assert_equals(mine_location([ [1, 0, 0], [0, 0, 0], [0, 0, 0] ]), [0, 0])
test.assert_equals(mine_location([ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0] ]), [2, 2])
"""
"""
PERFECT SOLUTION FROM CODEWARS
=1=
mineLocation = lambda f: next([i, j] for i, r in enumerate(f) for j, v in enumerate(r) if v == 1)
=2=
def mineLocation(field):
return list(next((i, j) for i, row in enumerate(field) for j, x in enumerate(row) if x))
=3=
def mineLocation(F):
for i in range(len(F)):
for j in range(len(F[i])):
if F[i][j]==1: return [i,j]
return []
=4=
def mineLocation(f):
return [(i:=sum(f,[]).index(1))//len(f[0]),i%len(f[0])]
=5=
def mineLocation(field):
for (y, row) in enumerate(field):
for (x, cell) in enumerate(row):
if cell:
return [y, x]
"""