[코딩테스트 입문]Day22-1
저주의 숫자 3
문제 설명
3x 마을 사람들은 3을 저주의 숫자라고 생각하기 때문에 3의 배수와 숫자 3을 사용하지 않습니다. 3x 마을 사람들의 숫자는 다음과 같습니다.
10진법 | 3x 마을에서 쓰는 숫자 | 10진법 | 3x 마을에서 쓰는 숫자 |
---|---|---|---|
1 | 1 | 6 | 8 |
2 | 2 | 7 | 10 |
3 | 4 | 8 | 11 |
4 | 5 | 9 | 14 |
5 | 7 | 10 | 16 |
정수 n
이 매개변수로 주어질 때, n
을 3x 마을에서 사용하는 숫자로 바꿔 return하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤
n
≤ 100
입출력 예
n | result |
---|---|
15 | 25 |
40 | 76 |
문제 풀이
def solution(n):
v = 0
for i in range(1, 1+n):
v += 1
if v % 3 == 0 or "3" in str(v):
if v != i:
v = v + 1
if v % 3 == 0 or "3" in str(v):
v = v + 1
else:
v = 1 + i
if 29 < v < 40 or 129 < v < 140:
v = (v//10+1)*10
return v
다른 풀이
def solution(n):
answer = 0
for _ in range(n):
answer += 1
while answer % 3 == 0 or '3' in str(answer):
answer += 1
return answer
입문 후반부의 문제들은 좀 어려운 것 같습니다..(っ °Д °;)っ
평행
문제 설명
점 네 개의 좌표를 담은 이차원 배열 dots
가 다음과 같이 매개변수로 주어집니다.
- [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.
제한사항
-
dots
의 길이 = 4 -
dots
의 원소는 [x, y] 형태이며 x, y는 정수입니다.
- 0 ≤ x, y ≤ 100
-
서로 다른 두개 이상의 점이 겹치는 경우는 없습니다.
-
두 직선이 겹치는 경우(일치하는 경우)에도 1을 return 해주세요.
- 임의의 두 점을 이은 직선이 x축 또는 y축과 평행한 경우는 주어지지 않습니다.
입출력 예
dots | result |
---|---|
[[1, 4], [9, 2], [3, 8], [11, 6]] | 1 |
[[3, 5], [4, 1], [2, 4], [5, 10]] | 0 |
문제 풀이
def solution(dots):
return int(dots[1][0] -dots[0][0] == dots[3][0] - dots[2][0] and dots[1][1] - dots[0][1] == dots[3][1] - dots[2][1])
def solution(dots):
# 두 직선이 경치는 경우는 점이 (x1, x2)(x1,x3)(x1,x4)(x2,x3),(X2,x4)(x3,x4) 이 점들 다 비교.
r = [0, 1, 2, 3]
for i in range(4):
for j in range(4):
a, s = [], []
if i != j:
r.remove(i)
r.remove(j)
a.append(dots[i])
a.append(dots[j])
s.append(dots[r[0]])
s.append(dots[r[1]])
if a[1][0] - a[0][0] == s[1][0] - s[0][0] and a[1][1] - a[0][1] == s[1][1] - s[0][1]:
return 1
r = [0, 1, 2, 3]
return 0
def solution(dots):
return 1 if abs((dots[0][0]-dots[1][0])/(dots[0][1]-dots[1][1]))==abs((dots[2][0]-dots[3][0])/(dots[2][1]-dots[3][1])) else 0
def solution(dots):
[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]=dots
answer1 = ((y1-y2)*(x3-x4) == (y3-y4)*(x1-x2))
answer2 = ((y1-y3)*(x2-x4) == (y2-y4)*(x1-x3))
answer3 = ((y1-y4)*(x2-x3) == (y2-y3)*(x1-x4))
return 1 if answer1 or answer2 or answer3 else 0
from itertools import combinations
def solution(dots):
a = []
for (x1,y1),(x2,y2) in combinations(dots,2):
a.append((y2-y1,x2-x1))
for (x1,y1),(x2,y2) in combinations(a,2):
if x1*y2==x2*y1:
return 1
return 0
기울기를 비교하는건데 테스트 케이스가 오류가 나서 풀이들을 비교를 해봐야겠습니다.
댓글남기기