코딩테스트 연습 - 캐릭터의 좌표 | 프로그래머스 스쿨 (programmers.co.kr)
내 풀이 (에러)
def solution(keyinput, board):
x_p=keyinput.count("right")
if "left" in keyinput:
for i in range(0, keyinput.count("left")):
x_p-=1
y_p=keyinput.count("up")
if "down" in keyinput:
for i in range(0, keyinput.count("down")):
y_p-=1
result=0
if (abs(2*x_p)<=board[0]) and (abs(2*y_p)<=board[1]):
result=[x_p, y_p]
elif abs(2*x_p)>board[0] and abs(2*y_p)<=board[1]:
if x_p<0:
result=[(-board[0]//2),y-p]
else:
result=[(board[0]//2),y_p]
elif abs(2*x_p)<=board[0] and abs(2*y_p)>board[1]:
if y_p<0:
result=[x_p, (-board[1]//2)]
else:
result=[x_p, (board[1]//2)]
else:
result=[board[0]//2, board[1]//2]
return result
풀이
def solution(keyinput, board):
answer = [0, 0]
x = board[0]
y = board[1]
for k in keyinput:
if k == "left" and abs(answer[0] - 1) <= x // 2:
answer[0] -= 1
elif k == "right" and abs(answer[0] + 1) <= x // 2:
answer[0] += 1
elif k == "down" and abs(answer[1] - 1) <= y // 2:
answer[1] -= 1
elif k == "up" and abs(answer[1] + 1) <= y // 2:
answer[1] += 1
return answer
코딩테스트 연습 - 로그인 성공? | 프로그래머스 스쿨 (programmers.co.kr)
문제 설명
중앙값은 어떤 주어진 값들을 크기의 순서대로 정렬했을 때 가장 중앙에 위치하는 값을 의미합니다. 예를 들어 1, 2, 7, 10, 11의 중앙값은 7입니다. 정수 배열 array가 매개변수로 주어질 때, 중앙값을 return 하도록 solution 함수를
완성해보세요.
내 풀이
def solution(id_pw, db):
if id_pw in db:
return "login"
else:
for i in db:
if i[0]==id_pw[0] and i[1]!=id_pw:
return "wrong pw"
else:
return "fail"
풀이
def solution(id_pw, db):
result = 'fail'
if id_pw in db:
result = 'login'
elif id_pw[0] in (i[0] for i in db):
result = 'wrong pw'
return result
초기값을 fail로 해서 해결
코딩테스트 연습 - 등수 매기기 | 프로그래머스 스쿨 (programmers.co.kr)
문제 설명
영어 점수와 수학 점수의 평균 점수를 기준으로 학생들의 등수를 매기려고 합니다. 영어 점수와 수학 점수를 담은 2차원 정수 배열 score가 주어질 때, 영어 점수와 수학 점수의 평균을 기준으로 매긴 등수를 담은 배열을 return하도록 solution 함수를 완성해주세요.
입출력 예
score | result |
[[80, 70], [90, 50], [40, 70], [50, 80]] | [1, 2, 4, 3] |
[[80, 70], [70, 80], [30, 50], [90, 100], [100, 90], [100, 100], [10, 30]] | [4, 4, 6, 2, 2, 1, 7] |
내 풀이
def solution(score):
scores=[]
result=[]
for i in score:
scores.append(i[0]+i[1])
array=sorted(scores, reverse=True)
for j in range(len(array)):
result.append(scores.index(array[j])+1)
return result
공동 순위가 있으면 다음 순위가 없어져야 함 (막힘)
풀이
def solution(score):
array=[]
result=[]
for i in score:
array.append(sum(i)/len(i))
sort_array=sorted(array, reverse=True)
for i in array:
result.append(sort_array.index(i)+1)
return result
★ 두 번째 for문에서 array와 scores의 위치 바뀌어야 중복 순위 제대로 출력됨
def solution(score):
scores=[]
result=[]
for i in score:
scores.append(i[0]+i[1])
array=sorted(scores, reverse=True)
for i in scores:
result.append(array.index(i)+1)
return result
-가장 간단한 풀이
def solution(score):
a = sorted([sum(i) for i in score], reverse = True)
return [a.index(sum(i))+1 for i in score]
'코딩테스트 > programmers (python)' 카테고리의 다른 글
python/ Lv.1 수박수박수박수? (0) | 2023.01.21 |
---|---|
python/ Lv.0 저주의 숫자 3 (0) | 2023.01.21 |
python/ Lv.0 소인수분해, 구슬을 나누는 경우의 수, 컨트롤 제트 (0) | 2023.01.17 |
python/ Lv.0 숨어있는 숫자의 덧셈 (2), 이진수 더하기, 7의 개수, 공 던지기, 영어는 싫어 (0) | 2023.01.17 |
python/ Lv.0 가까운 수, K의 개수★, 진료순서 정하기 ★★ (0) | 2023.01.15 |