2021. 3. 5. 00:21ㆍWork/StudyDaily
코준 21-03-04
제일 작은 수 제거하기
https://programmers.co.kr/learn/courses/30/lessons/12935
def solution(arr):
answer = []
# 길이가 1이라는 것은 어차피 빠져야 하므로 -1 리턴
if len(arr) <= 1:
return [-1]
# 제한 조건에 의해서 중복이 허용되지 않는다.
temp_arr = list(arr)
temp_arr.remove(min(temp_arr))
answer = temp_arr
return answer
코준 21-03-04
짝수와 홀수
https://programmers.co.kr/learn/courses/30/lessons/12937
def solution(num):
answer = ''
#limit
if type(num) == float:
return -1
answer = "Even" if num % 2 == 0 else "Odd"
return answer
def solution(num):
return ["Even", "Odd"][num & 1]
코준 21-03-04
[카카오 인턴] 키패드 누르기
https://programmers.co.kr/learn/courses/30/lessons/67256
def solution(numbers, hand):
answer = ''
if len(numbers) < 0 or len(numbers) > 1000:
return -1
left_pos = [0,3]
right_pos = [2,3]
left = [1, 4, 7]
right = [3, 6, 9]
mid = [2, 5, 8, 0]
dic = {0:[1,3], 1:[0,0], 2:[1,0], 3:[2,0], 4:[0,1], 5:[1,1], 6:[2,1], 7:[0,2], 8:[1,2], 9:[2,2]}
# left or right 위치
for i in numbers:
if i in left:
answer += "L"
left_pos = dic[i]
elif i in right:
answer += "R"
right_pos = dic[i]
elif i in mid:
left_len = abs(left_pos[0] - dic[i][0]) + abs(left_pos[1] - dic[i][1])
right_len = abs(right_pos[0] - dic[i][0]) + abs(right_pos[1] - dic[i][1])
if left_len < right_len:
answer += "L"
left_pos = dic[i]
elif left_len > right_len:
answer += "R"
right_pos = dic[i]
elif left_len == right_len:
if hand == "right":
answer += "R"
right_pos = dic[i]
else:
answer += "L"
left_pos = dic[i]
return answer
코준 21-03-04
최대공약수와 최소공배수 (이건.. 그냥 외우자..)
https://programmers.co.kr/learn/courses/30/lessons/12940
def gcd(x,y):
while y:
x, y = y, x%y
return x
def lcm(gcd,x,y):
if gcd == 1:
return x * y
else:
return gcd * (x // gcd) * (y // gcd)
def solution(n, m):
answer = [0]*2
answer[0] = gcd(n,m)
answer[1] = lcm(answer[0], n, m)
return answer
# gcd는 math에 있음
def solution(n, m):
gcd = lambda a,b : b if not a%b else gcd(b, a%b)
lcm = lambda a,b : a*b//gcd(a,b)
return [gcd(n, m), lcm(n, m)]
코준 21-03-04
콜라츠 추측
https://programmers.co.kr/learn/courses/30/lessons/12943
def solution(num):
answer = 0
if num < 1 or num > 8000000:
return -1
temp = num
cnt = 0
while temp != 1:
if temp % 2 == 0:
temp = temp // 2
else:
temp = (temp * 3) + 1
cnt += 1
if cnt >= 500:
return -1
answer = cnt
return answer
코준 21-03-04
평균 구하기
https://programmers.co.kr/learn/courses/30/lessons/12944
def solution(arr):
answer = 0
#limit
if len(arr) < 1 or len(arr) > 100:
return -1
average = sum(arr) / len(arr)
answer = average
return answer
코준 21-03-04
하샤드 수
https://programmers.co.kr/learn/courses/30/lessons/12947
def solution(x):
answer = True
h = list(map(int,list(str(x))))
total_h = sum(h)
answer = True if x % total_h == 0 else False
return answer
def Harshad(n):
# n은 하샤드 수 인가요?
return n % sum([int(c) for c in str(n)]) == 0
코준 21-03-04
핸드폰 번호 가리기
https://programmers.co.kr/learn/courses/30/lessons/12948
def solution(phone_number):
answer = ''
# limit
if len(phone_number) < 4 or len(phone_number) > 20:
return -1
ph_number_list = list(phone_number)
ph_number_list[:-4] = "*"* (len(ph_number_list) - 4)
answer = ''.join(ph_number_list)
return answer
코준 21-03-04
행렬의 덧셈 (1차원 2차원 변형)
https://programmers.co.kr/learn/courses/30/lessons/12950
import functools as ft
def solution(arr1, arr2):
#limit
size = len(arr1[0])
answer = [[0]*size,[0]*size]
if size > 500:
return -1
# 행과 열의 크기가 같음
unpress_arr1 = sum(arr1,[])
unpress_arr2 = sum(arr2,[])
sum_unpress_arr = list(map(lambda x,y:x+y, unpress_arr1,unpress_arr2))
S = size,2
sum_unpress_arr = list(ft.reduce(lambda x, y: map(list, zip(*y*(x,))), (iter(sum_unpress_arr), *S[0:-1])))
answer = sum_unpress_arr
return answer
https://stackoverflow.com/questions/56121903/how-to-reshape-python-list-without-numpy
import functools as ft
# example
L = list(range(2*3*4))
S = 2,3,4
# if tuples are acceptable
tuple(ft.reduce(lambda x, y: zip(*y*(x,)), (iter(L), *S[:0:-1])))
# (((0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)), ((12, 13, 14, 15), (16, 17, 18, 19), (20, 21, 22, 23)))
# if it must be lists
list(ft.reduce(lambda x, y: map(list, zip(*y*(x,))), (iter(L), *S[:0:-1])))
# [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]
def sumMatrix(A,B):
answer = [[c + d for c, d in zip(a, b)] for a, b in zip(A,B)]
return answer
import numpy as np
def sumMatrix(A,B):
A=np.array(A)
B=np.array(B)
answer=A+B
return answer.tolist()
def sumMatrix(A,B):
return [list(map(sum, zip(*x))) for x in zip(A, B)]
코준 21-03-04
x만큼 간격이 있는 n개의 숫자
https://programmers.co.kr/learn/courses/30/lessons/12954
def solution(x, n):
answer = []
if x < -10000000 or x > 10000000:
return -1
if n > 1000:
return -1
answer = [ x + (x*i) for i in range(n)]
return answer
코준 21-03-04
소수 만들기
- combinations을 이용해 3개씩 더해지는 값 만들기
- 각 구간 더해서 소수 판별
- true는 1 false는 0이기 때문에 더하면 해당 수 나옴
https://programmers.co.kr/learn/courses/30/lessons/12977
from itertools import combinations
def isprime(num):
if num != 1:
for i in range(2, num):
if num % i == 0:
return False
else:
return False
return True
def solution(nums):
answer = -1
# limit
if len(nums) < 3 or len(nums) > 50:
return -1
comb = list(combinations(nums,3))
comb_sum = sum([isprime(x+y+z) for x,y,z in comb])
answer = comb_sum
return answer
코준 21-03-04
직사각형 별찍기
https://programmers.co.kr/learn/courses/30/lessons/12969
def solution(a, b):
answer = -1
row = "*"*a
column = (row + "\n") * b
print(column)
return answer
print(solution(5, 3))
'Work > StudyDaily' 카테고리의 다른 글
Computer Science (0) | 2021.03.04 |
---|---|
Daily Study for Coding Test (02/24 ~ 03/03) (0) | 2021.03.04 |