데이터 - 기본 코드 및 알고리즘 연습/Codewar
[Codewars] Connect Four
Huin999
2021. 6. 28. 17:57
Codewars: Achieve mastery through challenge
Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.
www.codewars.com
▶ 문제 :
▶ 내 답안 : 시간초과로 실패
import pandas as pd
def sameguys(con4) :
r = ['Red','Red','Red','Red']
y = ['Yellow', 'Yellow', 'Yellow','Yellow']
if con4 == r :
return "Red"
elif con4 == y :
return "Yellow"
def who_is_winner(pieces_position_list):
cols = 'A B C D E F G'.split()
x = 4
while True :
if x >= len(pieces_position_list) :
return 'Draw'
break
else :
x+=1
ppl = pieces_position_list[:x]
c4_df = pd.DataFrame(columns=range(7), index=range(6))
for i, v in enumerate(cols) :
globals()[v] = [j.split('_')[1] for j in ppl if j.split('_')[0] == v]
c4_df[i] = globals()[v] + [None]*(6-len(globals()[v]))
for i in range(7) :
for j in range(6) :
try :
a = sameguys([c4_df[i][j], c4_df[i+1][j], c4_df[i+2][j], c4_df[i+3][j]])
b = sameguys([c4_df[i][j], c4_df[i][j+1], c4_df[i][j+2], c4_df[i][j+3]])
c = sameguys([c4_df[i][j], c4_df[i+1][j+1], c4_df[i+2][j+2], c4_df[i+3][j+3]])
d = sameguys([c4_df[i+3][j], c4_df[i+2][j+1], c4_df[i+1][j+2], c4_df[i][j+3]])
if a in ['Red',"Yellow"] :
return(a)
break
if b in ['Red',"Yellow"] :
return(b)
break
if c in ['Red',"Yellow"] :
return(c)
break
if d in ['Red',"Yellow"] :
return(d)
break
except :
pass
▶ 모범답안 :
import numpy as np
from scipy.signal import convolve2d
def who_is_winner(ppl):
arr = np.zeros((7,6), int)
for a in ppl :
pos, color = a.split('_')
pos = ord(pos) - ord('A')
val = (-1,1)[color == 'Red']
arr[pos, np.argmin(arr[pos] != 0)] = val
t_arr = val * arr
if any(np.max(cv) == 4 for cv in(convolve2d(t_arr, [[1,1,1,1]], 'same'),
convolve2d(t_arr, [[1],[1],[1],[1]], 'same'),
convolve2d(t_arr, [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], 'same'),
convolve2d(t_arr, [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]], 'same'))) :
return color
return 'Draw'
▶ 배워야할 부분 : any( ), ord( ), np.argmin( ), np.zeros( ), np.max( ), convolve2d(),ㅣ
val = (-1,1)[color == 'Red']
# pos : position
1. ord(), chr()
# 아스키코드변환
ord('A')
>>> 65
chr(65)
>>> 'A'
2. pos = ord(pos) - ord('A')
for문을 돌면서 position 설정
A_Yellow = 0
B_Yellow = 1
C_Yellow = 2
3. val = (-1,1)[color == 'Red']
(-1,1)[False]
>>> -1
(-1,1)[True]
>>> 1
4. arr[pos, np.argmin(arr[pos] != 0)] = val
> 각 리스트의 0이 아닌 최소 인덱스에 val를 할당해라
ppl = [ 'A_Yellow', 'B_Red', 'B_Yellow']
arr
>>> array([[-1, 0, 0, 0, 0, 0],
[ 1, -1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])
ppl = [ 'A_Yellow', 'B_Red', 'B_Yellow', 'C_Red']
arr
>>> array([[-1, 0, 0, 0, 0, 0],
[ 1, -1, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])
5. t_arr = val * arr 해주는 이유 :
arr 안의 -1 값인 "Yellow"를 1로 뒤집어 주기 위해
ppl = [ 'A_Yellow', 'B_Red', 'B_Yellow']
>>> arr
array([[-1, 0, 0, 0, 0, 0],
[ 1, -1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])
>>> t_arr
array([[ 1, 0, 0, 0, 0, 0],
[-1, 1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])
6. np.max()
7. convolve2d()
8. any()
9. if any(np.max(cv) == 4 for cv in(
convolve2d(t_arr, [[1,1,1,1]], 'same'),
convolve2d(t_arr, [[1],[1],[1],[1]], 'same'),
convolve2d(t_arr, [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], 'same'),
convolve2d(t_arr, [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]], 'same'))) :
return color