💡순열, 조합, product 설명 링크(필자가 작성한 블로그)
-> 순열, 조합, product
순열 계산 모듈을 만들고 다음 순열 계산 결과를 출력해 보자.
# 수학적인 공식으로 구현한 순열 코드
def getPermutationCnt(n,r, logPrint = True):
result = 1
for n in range(n, n-r, -1):
# log 찍는 코드 - # True이면 로그 찍고 False면 안 찍음
if logPrint: print('n:{}'.format(n))
result = result * n
return result
# 파이썬 itertools 패키지에 있는 순열 모듈 사용 코드
from itertools import permutations # 경우의 수를 다 반환해준다.
def getPermutations(ns, r):
pList = list(permutations(ns, r))
print(f'{len(ns)}P{r} 개수: {len(pList)}')
for n in permutations(ns, r):
print(n, end=', ')
import permutation as pt
numN = int(input('numN 입력: '))
numR = int(input('numR 입력: '))
# print(f'{numN}P{numR}: {pt.getPermutationCnt(numN, numR, logPrint=False)}')
listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3
pt.getPermutations(listVar, rVar)
조합 계산 모듈을 만들고 다음 조합 계산 결과를 출력해 보자.
# 수학적인 공식으로 구현한 조합 코드
def getCombinationCnt(n, r, logPrint=True):
resultP = 1
resultR = 1
resultC = 1
for n in range(n, (n - r), -1):
resultP *= n
if logPrint: print(f'resultP: {resultP}')
for n in range(r, 0, -1):
resultR *= n
if logPrint: print(f'resultR: {resultR}')
resultC = int(resultP / resultR)
if logPrint: print(f'resultC: {resultC}')
return resultC
# 파이썬 itertools 패키지에 있는 조합 모듈 사용 코드
from itertools import combinations # 경우의 수를 다 반환해준다.
def getCombinations(ns, r):
cList = list(combinations(ns, r))
print(f'조합 모듈 - {len(ns)}C{r} 개수: {len(cList)}')
for n in combinations(ns, r):
print(n, end=', ')
import combination as ct
numN = int(input('numN 입력: '))
numR = int(input('numR 입력: '))
print(f'수학공식 - {numN}C{numR}: {ct.getCombinationCnt(numN, numR, logPrint=False)}')
listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3
ct.getCombinations(listVar, rVar)
수입과 공과금을 입력하면 공과금 총액과 수입 대비 공과금 비율을 계산하는 모듈을 만들어보자
income = 0
waterPrice, electricPrice, gasPrice = 0, 0, 0
def setIncome(ic): # 변수의 값을 얻어와 셋팅하는 함수
global income
income = ic
def getIncome(): # 변수의 값을 리턴해주는 함수
return income
def setWaterPrice(wp):
global waterPrice
waterPrice = wp
def getWaterPrice():
return waterPrice
def setElectricPrice(ep):
global electricPrice
electricPrice = ep
def getElectricPrice():
return electricPrice
def setGasPrice(gp):
global gasPrice
gasPrice = gp
def getGasPrice():
return gasPrice
def getUtilityBill():
result = waterPrice + electricPrice + gasPrice
return result
def getUtilityBillRate():
result = getUtilityBill() / getIncome() * 100
return result
import utility as ut
inputIncome = int(input('수입 입력: '))
ut.setIncome(inputIncome)
inputWaterPrice = int(input('수도요금 입력: '))
ut.setWaterPrice(inputWaterPrice)
inputElectricPrice = int(input('전기요금 입력: '))
ut.setElectricPrice(inputElectricPrice)
inputGasPrice = int(input('가스요금 입력: '))
ut.setGasPrice(inputGasPrice)
print(f'공과금: {ut.getUtilityBill()}원')
print(f'수입 대비 공과금 비율: {ut.getUtilityBillRate()}%')
다음과 같이 패키지와 모듈을 만들고 연산 결과를 출력해 보자.
arithmetic package
basic_operator.py
def add(n1, n2):
return round(n1 + n2, 2)
def sub(n1, n2):
return round(n1 - n2, 2)
def mul(n1, n2):
return round(n1 * n2, 2)
def div(n1, n2):
return round(n1 / n2, 2)
developer_operator.py
def mod(n1, n2):
return round(n1 % n2, 2)
def flo(n1, n2):
return round(n1 // n2, 2)
def exp(n1, n2):
return round(n1 ** n2, 2)
shape package
triangle_square_area.py
def calTriangleArea(w, h):
return round(w * h / 2, 2)
def calSquareArea(w, h):
return round(w * h, 2)
circle_area.py
```
def calCircleArea(r):
return round(r ** 2 * 3.14, 2)
```
ex.py (실행 파일)
from arithmetic import basic_operator as bo
from arithmetic import developer_operator as do
from shape import triangle_square_area as tsa
from shape import circle_area as ca
inputNumber1 = float(input('숫자1 입력: '))
inputNumber2 = float(input('숫자2 입력: '))
print(f'{inputNumber1} + {inputNumber2} = {bo.add(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} - {inputNumber2} = {bo.sub(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} * {inputNumber2} = {bo.mul(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} / {inputNumber2} = {bo.div(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} % {inputNumber2} = {do.mod(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} // {inputNumber2} = {do.flo(inputNumber1, inputNumber2)}')
print(f'{inputNumber1} ** {inputNumber2} = {do.exp(inputNumber1, inputNumber2)}')
inputWidth = float(input('가로 길이 입력: '))
inputHeight = float(input('세로 길이 입력: '))
print(f'삼각형 넓이: {tsa.calTriangleArea(inputWidth, inputHeight)}')
print(f'사각형 넓이: {tsa.calSquareArea(inputWidth, inputHeight)}')
inputRadius = float(input('반지름 길이 입력: '))
print(f'원 넓이: {ca.calCircleArea(inputRadius)}')
"이 글은 제로베이스 데이터 취업 스쿨 강의 자료 일부를 발췌한 내용이 포함되어 있습니다."