모듈 문제풀이

송영석·2023년 4월 20일
0

데이터스쿨 14기

목록 보기
7/18

문제1

def examResult(s1, s2, s3, s4, s5):

    passAvgScore = 60
    limitScore = 40

    def total():
        totalScore = s1 + s2 + s3 + s4 + s5
        print(f"총점 : {totalScore}")
        return totalScore

    def average():
        averageScore = total() / 5
        print(f"평균 : {averageScore}")
        return averageScore

    def passORfail():
        print(f"{s1}: Pass") if s1 >= limitScore else print(f"{s1}: Fail")
        print(f"{s2}: Pass") if s2 >= limitScore else print(f"{s2}: Fail")
        print(f"{s3}: Pass") if s3 >= limitScore else print(f"{s3}: Fail")
        print(f"{s4}: Pass") if s4 >= limitScore else print(f"{s4}: Fail")
        print(f"{s5}: Pass") if s5 >= limitScore else print(f"{s5}: Fail")

    def finalPassORFail():
        if average() >= passAvgScore:
            if s1 >= limitScore and s2 >= limitScore and s3 >= limitScore and s4 >= limitScore and s5 >= limitScore:
                print("Final Pass")
            else:
                print("Final Fail")
        else:
            print("Final Fail")

    average()
    passORfail()
    finalPassORFail()
-----------------------------
import passORfail as pf

if __name__ == "__main__":
    sub1 = int(input("과목1 점수 입력: "))
    sub2 = int(input("과목2 점수 입력: "))
    sub3 = int(input("과목3 점수 입력: "))
    sub4 = int(input("과목4 점수 입력: "))
    sub5 = int(input("과목5 점수 입력: "))

    pf.examResult(sub1, sub2, sub3, sub4, sub5)

문제2

def calculatorTotalPrice(goods):

    if len(goods) <= 0:
        print("구매상품이 없습니다.")
        return

    rate = 25
    totalPrice = 0

    rates = {1:5, 2:10, 3:15, 4:20}

    if len(goods) in rates:
        rate = rates[len(goods)]

    for good in goods:
        totalPrice += good * (1 - rate * 0.01)

    return (rate, int(totalPrice))

def formatNumber(n):
    return format(n, ",")
---------------------
import product as pd

if __name__ == "__main__":

    flag = True
    goods = []

    while flag:
        selectNumber = int(input("1. 구매, 2.종료 "))

        if selectNumber == 1:
            goods_price = int(input("상품 가격 입력: "))
            goods.append(goods_price)
        elif selectNumber == 2:
            result = pd.calculatorTotalPrice(goods)
            flag = False

    print(f"할인율: {result[0]}%")
    print(f"합계: {pd.formatNumber(result[1])}원")

문제3

import random

userNums = []
randNums = []
collectNums = []
bonusNum = 0

def setUserNums(nums):
    global userNums
    userNums = nums

def getUserNums():
    return userNums


def setRandNums():
    global randNums
    randNums = random.sample(range(1, 46), 6)

def getUserNums():
    return randNums


def setBonusNum():
    global bonusNum

    while True:
        bonusNum = random.randint(1, 45)
        if bonusNum not in randNums:
            break

def getBonusNum():
    return bonusNum

def lottoResult():
    global userNums
    global randNums
    global collectNums

    collectNums = []
    for userNum in userNums:
        if userNum in randNums:
            collectNums.append(userNum)

    if len(collectNums) == 6:
        print("1등 당첨")
        print(f"번호: {collectNums}")

    elif (len(collectNums) == 5) and (bonusNum in userNums):
        print("2등 당첨")
        print(f"번호: {collectNums}, 보너스 번호: {bonusNum}")

    elif len(collectNums) == 5:
        print("3등 당첨")
        print(f"번호: {collectNums}")

    elif len(collectNums) == 4:
        print("4등 당첨")
        print(f"번호: {collectNums}")

    elif len(collectNums) == 3:
        print("5등 당첨")
        print(f"번호: {collectNums}")

    else:
        print("다음 기회에")
        print(f"기계 번호: {randNums}")
        print(f"보너스 번호: {bonusNum}")
        print(f"유저 번호: {userNums}")
        print(f"일치 번호: {collectNums}")

def startLotto():
    n1 = int(input("번호(1~45) 입력: "))
    n2 = int(input("번호(1~45) 입력: "))
    n3 = int(input("번호(1~45) 입력: "))
    n4 = int(input("번호(1~45) 입력: "))
    n5 = int(input("번호(1~45) 입력: "))
    n6 = int(input("번호(1~45) 입력: "))
    selectNums = [n1, n2, n3, n4, n5, n6]

    setUserNums(selectNums)
    setRandNums()
    setBonusNum()

    lottoResult()
---------------------------
import lotto as lt

lt.startLotto()

문제4

def permutationCnt(n, r, logPrint = True):

    result = 1
    for n in range(n, (n-r), -1):
        if logPrint: print(f"n: {format(n)}")
        result = result * n

    return result

# 모듈 활용 (리스트를 하나씩 다 보고 싶을 때)

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("n 입력: "))
numR = int(input("r 입력: "))

print(f"{numN}P{numR}: {pt.permutationCnt(numN, numR, logPrint=False)}")

# 모듈 활용

listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3

pt.getPermutations(listVar, rVar)

문제5

def combinationCnt(n, r, logPrint = True):

    resultP = 1
    resultR = 1
    resultC = 1

    for n in range(n, (n-r), -1):
        resultP = resultP * n
    if logPrint: print(f"resultP: {resultP}")

    for n in range(r, 0, -1):
        resultR = resultR * n
    if logPrint: print(f"resultR: {resultR}")

    resultC = int(resultP / resultR)
    if logPrint: print(f"resultC: {resultC}")

    return resultC

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.combinationCnt(numN,numR, logPrint=False)}")



listVar = [1, 2, 3, 4, 5, 6, 7, 8]
rVar = 3
ct.getCombinations(listVar, rVar)

문제6

income = 0
waterPrice = 0; electricPrice = 0; gasPrice = 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 getBill():
    result = waterPrice + electricPrice + gasPrice
    return result

def getBillRate():
    result = getBill() / getIncome() * 100
    return result

def formatNum(n):
    return format(n, ",")
--------------------------------
import cost

inputIncome = int(input("수입 입력: "))
cost.setIncome(inputIncome)

inputWaterPrice = int(input("수도요금 입력: "))
cost.setWaterPrice(inputWaterPrice)

inputElectricPrice = int(input("전기요금 입력: "))
cost.setElectricPrice(inputElectricPrice)

inputGasPrice = int(input("가스요금 입력: "))
cost.setGasPrice(inputGasPrice)

print(f"공과금: {cost.formatNum(cost.getBill())}원")
print(f"수입 대비 공과금 비율: {cost.getBillRate()}%")

문제7

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)
-------------------------------
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)
-------------------------------
def calTriangleArea(w, h):
    return round(w * h / 2, 2)

def calSquareArea(w, h):
    return round(w * h, 2)
--------------------------------
def calCircleArea(r):
    return round(r ** 2 * 3.14, 2)
--------------------------------
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)}")
profile
매일매일 작성!!

0개의 댓글