프로그래머스 연습문제 - 멀리 뛰기 (level2)

j_wisdom_h·2022년 11월 29일
0

CodingTest

목록 보기
21/58
post-thumbnail

프로그래머스 연습문제 -멀리 뛰기 (level2)

문제설명


제한사항 & 입출력 예


MySolution

import math

def solution(n):
    answer = 1

    p = 1
    while  n-p >= p:
        answer += math.comb(n-p, p)
        p += 1

    return answer%1234567

공부한 것

math.comb(n,r) , math.perm(n, r)

https://www.w3schools.com/python/ref_math_comb.asp

순열과 조합을 구하기 위해 itertools 모듈을 사용을 사용하는데,
지금과 같은 경우 개수만 필요하다.

파이썬은 math 함수에서 그 기능을 제공한다!(단, python 3.8 이상 )


# Import math Library
import math

# Initialize the number of items to choose from
n = 7

# Initialize the number of possibilities to choose
k = 5

# Print total number of possible combinations
# 21
print (math.comb(n, k))

itertools 모듈을 이용해 순열, 조합의 모든 경우의 수 구하기

순열(Permutation) : 순서를 고려하여 뽑는 경우의 수

nPr : 서로 다른 n개에서 r개를 택하여 일렬로 나열하는 경우의 수.
nPr = n!/(n-r)! = nx(n-1)x(n-2)x...x(n-r+1)

조합(combination) : 조합은 순서를 생각하지 않고 뽑는 경우의 수

nCr : 서로 다른 n개에서 순서를 생각하지 않고 r개를 택하는 경우의 수.
( 조합은 순서를 생각하지 않으므로 (A,B)와 (B,A)를 같은 것으로 본다.)
nCr = n!/(n-r)!r! = nPr/r! = {n(n-1)(n-2)...(n-r+1)}/r!

from itertools import permutations
from itertools import combinations

setA = [1,2,3]

data = list[ itertools.permutation(setA, 2) ]
#[(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]

setB = ['A', 'B', 'C']

data = list(itertools.combinations(setB, 2))
#[ (A, B), (A, C), (B, C) ]
profile
뚜잇뚜잇 FE개발자

0개의 댓글