제로베이스 데이터취업스쿨 DAY8 파이썬 중급문제풀이3~7

NAYOUNG KIM·2023년 3월 15일
0

제로베이스 교육

목록 보기
8/54
post-thumbnail
  1. 모듈 - 순열
def getPermutationCnt(n,r):
    result = 1
    for n in range(n, (n-r), -1):
        result *= n
    return result
import permutation as pt

numN = int(input('numN : '))
numR = int(input('numR : '))

print(pt.getPermutationCnt(numN, numR))
from itertools import permutations

def getPermutations(ns, r):
    pList = list(permutations(ns, r))
    print(f'개수 : {len(pList)}')

    for n in permutations(ns,r):
        print(n, end='')
import permutation as pt

listVar = [1,2,3,4,5,6,7,8]
rVar = 3
pt.getPermutations(listVar, rVar)
  1. 모듈 - 조합
def getCombinationCnt(n,r):
    resultP = 1
    resultR = 1
    resultC = 1

    for n in range(n, (n-r), -1):
        resultP *= n

    for n in range(r, 0, -1):
        resultR *= n

    resultC = int(resultP / resultR)

    return resultC
import combination as ct

numN = int(input('numN : '))
numR = int(input('numR : '))

print(ct.getCombinationCnt(numN, numR))
from itertools import combinations

def getCombination(ns,r):
    cList = list(combinations(ns, r))
    print(f'개수 : {len(cList)}')

    for n in combinations(ns,r):
        print(n, end='')
listVar = [1,2,3,4,5,6,7,8]
rVar = 3
ct.getCombination(listVar, rVar)
  1. 모듈 - 수입과 공과금
income = 0
waterPrice = 0; elecPrice = 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 setElecPrice(ep):
    global elecPrice
    elecPrice = ep

def getElecPrice():
    return elecPrice

def setGasPrice(gp):
    global gasPrice
    gasPrice = gp

def getGasPrice():
    return gasPrice

def getUtilityBill():
    result = waterPrice + elecPrice + gasPrice
    return result

def getUtilityRate():
    result = getUtilityBill() / getIncome() * 100
    return result
import utilityBill as ut
intputIncome = int(input('수입:'))
w = int(input('수도세:'))
e = int(input('전기세:'))
g = int(input('가스요금:'))

ut.setIncome(intputIncome)
ut.setWaterPrice(w)
ut.setElecPrice(e)
ut.setGasPrice(g)

print(f'총 공과금 비용: {ut.getUtilityBill()}')
print(f'수입 대비 공과금 비율: {int(ut.getUtilityRate())}%')
  1. 모듈 - 계산 패키지와 모듈
# basic_operator.py
def add(n1, n2):
    return n1+n2

def sub(n1, n2):
    return n1-n2

def mul(n1, n2):
    return n1*n2

def div(n1, n2):
    return round(n1/n2, 2)
# developer_operator.py
def mod(n1, n2):
    return n1%n2

def flo(n1, n2):
    return n1//n2

def exp(n1, n2):
    return n1**n2
# circle_area.py
def calCircleArea(r):
    return round( r ** 2 * 3.14 ,2)
# triangle_square_area.py
def calTriangleArea(w,h):
    return w * h / 2

def calSquareArea(w,h):
    return w * h
# ex.py
from arithmetic import basic_operator as bo
from arithmetic import developer_operator as do

from shape import circle_area as ca
from shape import triangle_square_area as tsa

num1 = 10
num2 = 20
print(bo.add(num1, num2))
print(bo.sub(num1, num2))
print(bo.mul(num1, num2))
print(bo.div(num1, num2))

print(do.mod(num1, num2))
print(do.flo(num1, num2))
print(do.exp(num1, num2))

width = 10
height = 20
r = 5

print(ca.calCircleArea(r))
print(tsa.calTriangleArea(width, height))
print(tsa.calSquareArea(width, height))
  1. 클래스 - 회원가입, 로그인
class Member:
    def __init__(self, i, p):
        self.id = i
        self.pw = p

class MemberRepository:

    def __init__(self):
        self.members = {}

    def addMember(self, m):
        self.members[m.id] = m.pw

    def loginMember(self, i, p):
        isMember = i in self.members

        if isMember and self.members[i] == p:
            print(f'{i}: log-in success!!')
        else:
            print(f'{i}: log-in fail!!')

    def removeMember(self, i, p):
        del self.members[i]

    def printMembers(self):
        for mk in self.members.keys():
            print(f'ID:{mk}\t\t', end='')
            print(f'PW:{self.members[mk]}')
import member as mb

mems = mb.MemberRepository()

for i in range(3):
    mId = input('아이디: ')
    mPw = input('비밀번호: ')
    mem = mb.Member(mId, mPw)
    mems.addMember(mem)

mems.printMembers()
# 4.책 정보
class Book:
    def __init__(self, n, p, i):
        self.bName = n
        self.bPrice = p
        self.bIsbn = i

class BookRepository:

    def __init__(self):
        self.bDic = {}

    def registBook(self, b):
        self.bDic[b.bIsbn] = b

    def removeBook(self, i):
        del self.bDic[i]

    def printBooksInfo(self):
        for i in self.bDic.keys():
            b = self.bDic[i]
            print(f'{b.bName}, {b.bPrice}, {b.bIsbn}')

    def printBookInfo(self, i):
        if i in self.bDic.keys():
            b = self.bDic[i]
            print(f'{b.bName}, {b.bPrice}, {b.bIsbn}')
        else :
            print('검색결과가 없습니다.')
import book as b

myBRepository = b.BookRepository()

myBRepository.registBook(b.Book('python', 20000, '1234567890'))
myBRepository.registBook(b.Book('java', 18000, '9999567890'))
myBRepository.registBook(b.Book('c', 16000, '8888567890'))

myBRepository.printBooksInfo()
myBRepository.printBookInfo('1234567890')
myBRepository.removeBook('1234567890')
myBRepository.printBooksInfo()
# 5.주사위게임
import random
class Dice:
    def __init__(self):
        self.cNum = 0
        self.uNum = 0

    def setCnum(self):
        self.cNum = random.randint(1,6)

    def setUnum(self):
        self.uNum = random.randint(1,6)

    def startGame(self):
        print('start game!')
        self.setCnum()
        self.setUnum()
        self.printResult()

    def printResult(self):
        print(f'컴퓨터:{self.cNum}\t유저:{self.uNum}')

        if self.cNum > self.uNum:
            print('컴퓨터 승!')
        elif self.cNum < self.uNum:
            print('유저 승!')
        elif self.cNum == self.uNum:
            print('무승부!')
import dice
game = dice.Dice()
game.startGame()
# 6.mp3 player
import random
from time import sleep

class Song:
    def __init__(self, t, s, pt):
        self.title = t
        self.singer = s
        self.play_time = pt
        
class Player:
    def __init__(self):
        self.songList = []
        self.isLoop = False

    def addSong(self, s):
        self.songList.append(s)

    def play(self):
        if self.isLoop:
            while self.isLoop:
                for s in self.songList:
                    print(f'title:{s.title}\tsinger:{s.singer}\t\tplay time:{s.play_time}')
                    sleep(s.play_time)
        else:
            for s in self.songList:
                print(f'title:{s.title}\tsinger:{s.singer}\t\tplay time:{s.play_time}')
                sleep(s.play_time)

    def suffle(self):
        random.shuffle(self.songList)

    def setIsloop(self, flag):
        self.isLoop = flag

    def printSongs(self):
        for s in self.songList:
            print(f'제목: {s.title}\t 가수: {s.singer}\t ({s.play_time}초)')

import mp3player as mp3

s1 = mp3.Song('벚꽃엔딩', '버스커버스커', 2)
s2 = mp3.Song('봄처녀', '선우정아', 1)
s3 = mp3.Song('봄사랑벚꽃말고', '아이유', 3)

player = mp3.Player()
player.addSong(s1)
player.addSong(s2)
player.addSong(s3)

#player.suffle()
#player.printSongs()
player.setIsloop(True)
player.play()
profile
21세기 주인공

0개의 댓글