-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy2_joystick.py
More file actions
54 lines (41 loc) · 1.38 KB
/
greedy2_joystick.py
File metadata and controls
54 lines (41 loc) · 1.38 KB
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
from operator import attrgetter
from string import ascii_letters
alpabet = ascii_letters[len(ascii_letters)//2:]
class RoutingInfo():
def __init__(self, index, distance):
self.index = index
self.distance = distance
def __repr__(self):
return "RouteInfo(index: %s, distance: %s)" %(self.index, self.distance)
def solution(name):
name = list(name)
handle, cur_idx = 0, 0
while True:
### handle up down
if name[cur_idx] != 'A':
handle += min_find(name[cur_idx])
name[cur_idx] = 'A'
### end condition
if name == ['A',]*len(name):
break
### route greedy
router = []
for i, letter in enumerate(name):
if letter != 'A':
rd = abs(i-cur_idx)
ld = len(name)-rd
router.append(RoutingInfo(i, min(rd, ld)))
greedy = sorted(router, key=attrgetter('distance'))[0]
### handle right left
handle += greedy.distance
cur_idx = greedy.index
return handle
def min_find(letter):
lidx = alpabet.find(letter)
ridx = len(alpabet)-lidx
return min(lidx, ridx)
print(solution("ABAABBB")) #9
print(solution("AZAAAZ")) #5
print(solution("JAZ")) #11
print(solution("JEROEN")) #56
print(solution("JAN")) #23