자기계발/그외 준비하는것

[Python]COS_PRO_기출 1회차 [6-10]풀이

개발자 덕구🐾 2022. 10. 23. 12:30
728x90

 

6번 체스의 나이트 

dx = [-2,-1,1,2,2,1,-1,-2]
dy = [1,2,2,1,-1,-2,-2,-1]

def solution(pos):
	answer = 0
	x = ord(pos[0])-65
	y = int(pos[1])-1
	for i in range(8) :
		nx,ny = x + dx[i],y + dy[i]
		if 0<=nx<8 and 0<=ny<8 :
			answer+=1 
	return answer

 

 

 

 

7번 병합 and 정렬 

def solution(arrA, arrB):
	arrA_idx = 0
	arrB_idx = 0
	arrA_len = len(arrA)
	arrB_len = len(arrB)
	answer = []
	while (arrA_idx < arrA_len and arrB_idx < arrB_len):
		if arrA[arrA_idx] < arrB[arrB_idx]:
			answer.append(arrA[arrA_idx])
			arrA_idx += 1
		else:
			answer.append(arrB[arrB_idx])
			arrB_idx += 1
	while arrA_idx < arrA_len:
		answer.append(arrA[arrA_idx])
		arrA_idx += 1
	while arrB_idx < arrB_len:
		answer.append(arrB[arrB_idx])
		arrB_idx += 1
	return answer

 

 

 

8번 누가 당선 되나요?

def solution(N, votes):
	vote_counter = [0 for i in range(N+1)]
	for i in votes:
		vote_counter[i] += 1

	max_val = max(vote_counter)

	answer = []
	for idx in range(1, N + 1):
		if vote_counter[idx] == max_val:
        	# 이 부분 수정 됨 
			answer.append(idx)
	return answer

 

 

 

9번 계단게임 

def solution(recordA, recordB):
	cnt = 0
	for i in range(len(recordA)):
		if recordA[i] == recordB[i]:
			continue
		elif recordA[i] == func(recordB[i]):
			cnt = cnt + 3
		else:
			if cnt == 0 : continue 
			cnt = cnt - 1
	return cnt

나는 이렇게 if cnt == 0 : continue를 추가했는데 

 

		else:
			cnt = max(0,cnt - 1)

max(0,cnt-1)으로 하면 더 간단할 것같다. 

이렇게 하면 cnt는 0보다 작아질 수 없다. 

 

 

 

 

 

 

10번 주식으로 최대 수익을 내세요

def solution(prices):
	INF = 1000000001;
	tmp = INF
	answer = -INF
	for price in prices:
		if tmp != INF:
			answer = max(answer, price - tmp)
		tmp = min(tmp, price)
	return answer

 

반응형