[스파르타코딩클럽]데이터분석 과정/PYTHON

[Python 코드카타] 58 ~ 60번 (프로그래머스)

doo_ 2024. 1. 24. 14:02

58번 소수 만들기

def solution(nums):
    answer = 0
    n = len(nums)
    # 1) 3개의 숫자를 꺼내서 합하기
    for i in range(n-2):
        for j in range(i+1,n-1):
            for k in range(j+1, n):
                a = nums[i] + nums[j] + nums[k]
                # 2) 소수인지 아닌지 판별하기
                b = True
                for l in range(2,int(a/2+1)):
                    if a%l == 0:
                        b = False
                        break
                if b == True :
                    answer += 1
    return answer

> combinations 라이브러리 활용과 for - else문 이해

def solution(nums):
    from itertools import combinations as cb
    answer = 0
    for a in cb(nums, 3):
        cand = sum(a)
        for j in range(2, cand):
            if cand%j==0:
                break
        else:
            answer += 1
    return answer

 

> prime 소수 판별하는 함수를 만든 뒤, 조합한 수를 그 함수에 넣어 판별하는 과정도 있다

def is_prime(num):
    for i in range(2, int(num/2) + 1):
        if num % i == 0:
            return False
    return True

def solution(nums):
    from itertools import combinations
    comb = list(combinations(nums, 3))
    cnt = 0
    for j in comb:
        if is_prime(sum(j)):
            cnt += 1
    return cnt

> 또 다른 방법

def solution(nums):
    import itertools
    n_list = list(sum(i) for i in itertools.combinations(nums, 3))
    n_doc = {}
    for i in n_list:
        if i not in n_doc.keys():
            n_doc[i] = 1
        else:
            n_doc[i] += 1
    n_set = set(n_doc.keys())
    n_max = max(n_set)
    for i in range(2, int(n_max**0.5 + 1)):
        n_set -= set(range(2*i, n_max+1, i))
    answer = 0
    for i in n_set:
        answer += n_doc[i]
    return answer

 

59번 덧칠하기

> 처음 시도한 답안

> 시간초과와 몇 문제 실패했는데, 아직 이유는 잘 모르겠다 ㅠㅠ

def solution(n, m, section):
    sections = [1]*n
    for i in section :
        sections[i-1] = 0
    cnt = 0
    i = 0
    while sections < [1]*n:
        if sections[section[i]-1:section[i]-1+m] < [1]*m:
            sections[section[i]-1:section[i]-1+m] = [1]*m
            cnt +=1
            i +=1
        else :
            i +=1
            continue
    return cnt

> 처음 칠하는 section + m을 더하고 닿는 부분들은 pass하고 닿지 않는 부분부터 + m 하는 방식

def solution(n, m,section):
    answer = 0
    p = 0
    for i in section:
        if i > p :
            p = i+m-1
            answer += 1
    return answer

> 다른 답안

def solution(n, m, section):
    answer = 0
    while len(section) > 0:
        temp = section[0] + m
        while len(section) != 0 and temp > section[0]:
            section.pop(0)
        answer += 1        

    return answer

60번 기사단원의 무기

def solution(number, limit, power):
    answer = 0
    a = []
    for i in range(1, number+1): # 1부터 number까지
        div = 0
        for j in range(1,int(i**0.5)+1): # 약수의 개수 구하기
            if i%j == 0 :     # 약수는 서로 짝이 있다는 성질이 있기에 이를 활용해 개수를 줄여줌 
                div += 1
                if j**2 != i: # 제곱이 되는 수는 중복 방지
                    div +=1
        if div > limit :
            div = power
        a.append(div)
    answer = sum(a)
    return answer