1차시도
def solution(name):
answer = 0
init="A"*len(name)
for i in range(len(name)):# ascii: A=65 Z=90
if ord(name[i]) == 65:
pass
else:
tmp=ord(name[i]) - 65
#print(tmp)
if tmp>13:
answer+=26-tmp
else:
answer+=tmp
return answer
2차시도
def solution(name):
answer = 0
len_name=len(name)
init="A"*len_name
chk=[]
for i in range(len_name):# ascii: A=65 Z=90
if ord(name[i]) == 65:
chk.append(1)
pass
else:
tmp=ord(name[i]) - 65
chk.append(2)
#print(tmp)
if tmp>13:
answer+=26-tmp
else:
answer+=tmp
#print(chk)
ind=0
ind_tmp=0
while True:
#print(chk)
try:
ind_tmp = chk.index(2)
except:
break
diff_ind=ind_tmp-ind
if diff_ind>(len_name//2):#차이가 길이의 반보다 큰 경우
answer+=len_name-diff_ind
else: # 차이가 길이의 반보다 작거나 같은 경우
answer+=diff_ind
chk[ind]=0
ind=ind_tmp
return answer
"BAAAABB" 는 5가 나와야하는데 6이 나온다.
index를 구하는거는 제일 처음 B를 구하는거여서 2+1이되나보다
def solution(name):
answer = 0
len_name=len(name)
init="A"*len_name
chk=[]
## Number of movements
for i in range(len_name):
if ord(name[i]) == 65:
pass
else:
tmp=ord(name[i]) - 65
chk.append(i)
if tmp>13:
answer+=26-tmp
else:
answer+=tmp
ind=0
## Greedy algorithm
while chk!=[]:
tmp1=abs(chk[0]-ind)
tmp2=len_name+ind-chk[-1]
#print("tmp1:{}\ttmp2:{}".format(tmp1,tmp2))
if tmp1>tmp2:
ind=chk.pop()
answer+=tmp2
else:
ind=chk.pop(0)
answer+=tmp1
#print("ind:{}\tchk:{}".format(ind,chk))
return answer
def solution(name):
answer = 0
len_name=len(name)
init="A"*len_name
chk=[]
## Number of movements
for i in range(len_name):
if ord(name[i]) == 65:
pass
else:
tmp=ord(name[i]) - 65
chk.append(i)
if tmp>13:
answer+=26-tmp
else:
answer+=tmp
ind=0
## Greedy algorithm
while chk!=[]:
tmp1=min(abs(chk[0]-ind),len_name-abs(chk[0]-ind))
tmp2=min(abs(chk[-1]-ind),len_name-abs(chk[-1]-ind))
min_chk=min(tmp1,tmp2)
#print("tmp1:{}\ttmp2:{}".format(tmp1,tmp2))
if tmp1>tmp2:
ind=chk.pop()
answer+=tmp2
else :
ind=chk.pop(0)
answer+=tmp1
#print("ind:{}\tchk:{}".format(ind,chk))
return answer
'STUDY > Algorithm' 카테고리의 다른 글
[프로그래머스] LEVEL2 가장큰수, python3, 정렬 (0) | 2019.11.06 |
---|---|
[프로그래머스] LEVEL2 더맵게, python3, 힙,Heap (0) | 2019.11.06 |
[프로그래머스] LEVEL2 소수 찾기, python3,완전탐색 (0) | 2019.11.04 |
[프로그래머스] LEVEL2 탑, python3, 스택/큐 (0) | 2019.10.29 |
[프로그래머스] LEVEL2 주식가격,python3,스택/큐 (0) | 2019.10.29 |