[제로베이스 데이터 취업스쿨] 23.06.27 스터디 노트

김준호·2023년 6월 27일
0
post-thumbnail

알고리즘 문제풀이 2

문제17) 재귀 알고리즘1

sales = [12000,13000,12500,11000,10500,98000,91000,91500,105000,115000,120000,125000]

def salesUpandDown(ss):

    if len(ss) == 1:
        return ss

    print(f'salse : {ss}')
    currentSales = ss.pop(0)    #1200
    nextSales = ss[0]

    increase = nextSales - currentSales
    if increase > 0:
        increase = '+' + str(increase)
    print(f'매출 증감액 : {increase}')
    return salesUpandDown(ss)


if __name__ == '__main__':
    salesUpandDown(sales)
salse : [12000, 13000, 12500, 11000, 10500, 98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : +1000
salse : [13000, 12500, 11000, 10500, 98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : -500
salse : [12500, 11000, 10500, 98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : -1500
salse : [11000, 10500, 98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : -500
salse : [10500, 98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : +87500
salse : [98000, 91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : -7000
salse : [91000, 91500, 105000, 115000, 120000, 125000]
매출 증감액 : +500
salse : [91500, 105000, 115000, 120000, 125000]
매출 증감액 : +13500
salse : [105000, 115000, 120000, 125000]
매출 증감액 : +10000
salse : [115000, 120000, 125000]
매출 증감액 : +5000
salse : [120000, 125000]
매출 증감액 : +5000

문제18) 재귀 알고리즘2

recusionModuel.py

class NumsSum:

    def __init__(self,n1,n2):
        self.bigNum = 0
        self.smallNum = 0
        self.setN1N2(n1,n2)
	
    #입력받은 두 수의 크기 비교
    def setN1N2(self,n1,n2):
        self.bigNum = n1
        self.smallNum =n2

        if n1 < n2:
            self.bigNum = n2
            self.smallNum = n1

    #n까지 합
    def addNum(self,n):
        if n <= 1:
            return n

        return n + self.addNum(n-1)
	
    #큰 수 바로 이전의 수까지 합에서 작은수 까지 합을 빼면 결과
    def sumBetweenNums(self):
        return self.addNum(self.bigNum -1) - self.addNum(self.smallNum)

recusion.py (실행파일)

import recusionModuel as rm

num1 = int(input('input number1: '))
num2 = int(input('input number2: '))

ns = rm.NumsSum(num1, num2)
result = ns.sumBetweenNums()
print(f'result : {result}')
input number1: 3
input number2: 10
result : 39

문제19) 평균 알고리즘1

생각할 점
1. 선수의 최고점, 최소점을 지우는 알고리즘
2. 최고점, 최소점 지운 후 총점, 평균 구하기、 구한 평균을 가지고 순위리스트에 넣어야 한다.
3. 근사값 알고리즘을 구현해 해당 순위에 선수평균점 넣기

maxAlgo.py (최고점 삭제 알고리즘)

class MaxAlgorithm:

    def __init__(self,ss):
        self.scores = ss
        self.maxScore = 0
        self.maxIdx = 0

    def removeMaxScore(self):
        self.maxScore = self.scores[0]

        for i, n in enumerate(self.scores):
            if self.maxScore < n:
                self.maxScore = n
                self.maxIdx = i
        print(f'self.maxScore: {self.maxScore}')
        print(f'self.maxIdx: {self.maxIdx}')

        self.scores.pop(self.maxIdx)
        print(f'scores : {self.scores}')

minAlgo.py

class MinAlgorithm:

    def __init__(self,ss):
        self.scores = ss
        self.minScore = 0
        self.minIdx = 0

    def removeMinScore(self):
        self.minScore = self.scores[0]

        for i, n in enumerate(self.scores):
            if self.minScore > n:
                self.minScore = n
                self.minIdx = i
        print(f'self.maxScore: {self.minScore}')
        print(f'self.maxIdx: {self.minIdx}')

        self.scores.pop(self.minIdx)
        print(f'scores : {self.scores}')

nearAlgo.py (근사값 구해 평균점수 넣는 알고리즘)

class Top5Players:

    def __init__(self,cts, ns):
        self.currentScores = cts
        self.newScore = ns

    def setAlignScore(self):
        nearIdx = 0
        minNum = 10.0

        for i, s in enumerate(self.currentScores):
            absNum = abs(self.newScore - s)
            if absNum < minNum:
                minNum = absNum
                nearIdx = i

        if self.newScore >= self.currentScores[nearIdx]:
            for i in range(len(self.currentScores)-1, nearIdx, -1):
                self.currentScores[i] = self.currentScores[i-1]

            self.currentScores[nearIdx] = self.newScore

        else:
            for i in range(len(self.currentScores) - 1, nearIdx+1, -1):
                self.currentScores[i] = self.currentScores[i - 1]

            self.currentScores[nearIdx+1] = self.newScore

    def getFinalTop5Scores(self):
        return self.currentScores

average.py(실행파일)

import maxAlgo
import minAlgo
import nearAlgo

top5Scores = [9.12, 8.95, 8.12, 6.90, 6.18]
scores = [6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 8.2, 6.2, 5.8]
print(f'{scores}')

#최대 최소 알고리즘 이용한 최대최소 점수 삭제
ma = maxAlgo.MaxAlgorithm(scores)
result = ma.removeMaxScore()
ma2 = minAlgo.MinAlgorithm(scores)
result = ma2.removeMinScore()


#최대최소 지워진 자료구조의 총점, 평균
total = 0
average = 0

for n in scores:
    total += n

average = round(total / len(scores),2)
print(f'total : {round(total,2)}\t average : {average}')

na = nearAlgo.Top5Players(top5Scores,average)
na.setAlignScore()
top5Scores = na.getFinalTop5Scores()

print(f'top5playerScores : {top5Scores}')
[6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 8.2, 6.2, 5.8]
self.maxScore: 8.2
self.maxIdx: 7
scores : [6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 6.2, 5.8]
self.maxScore: 5.8
self.maxIdx: 8
scores : [6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 6.2]
total : 56.0	 average : 7.0
top5playerScores : [9.12, 8.95, 8.12, 7.0, 6.9]

문제20) 평균 알고리즘2

kor_avg = 88;eng_avg = 82;mat_avg = 90;sci_avg = 78;his_avg = 92

hong_kor_score = 85;hong_eng_score = 90;hong_mat_score = 82;hong_sci_score = 88;hong_his_score = 100

stu19Cnt_kor_total = kor_avg * 20 - hong_kor_score
stu19Cnt_eng_total = eng_avg * 20 - hong_eng_score
stu19Cnt_mat_total = mat_avg * 20 - hong_mat_score
stu19Cnt_sci_total = sci_avg * 20 - hong_sci_score
stu19Cnt_his_total = his_avg * 20 - hong_his_score

stu19Cnt_kor_avg = stu19Cnt_kor_total / 19
stu19Cnt_eng_avg = stu19Cnt_eng_total / 19
stu19Cnt_mat_avg = stu19Cnt_mat_total / 19
stu19Cnt_sci_avg = stu19Cnt_sci_total / 19
stu19Cnt_his_avg = stu19Cnt_his_total / 19

kor_gap = hong_kor_score - stu19Cnt_kor_avg
eng_gap = hong_eng_score - stu19Cnt_eng_avg
mat_gap = hong_mat_score - stu19Cnt_mat_avg
sci_gap = hong_sci_score - stu19Cnt_sci_avg
his_gap = hong_his_score - stu19Cnt_his_avg


print(f'국어 점수 차이 : {"+"+str(round(kor_gap,2)) if kor_gap > 0 else round(kor_gap,2)}')
print(f'영어 점수 차이 : {"+"+str(round(eng_gap,2)) if eng_gap > 0 else round(eng_gap,2)}')
print(f'수학 점수 차이 : {"+"+str(round(mat_gap,2)) if mat_gap > 0 else round(mat_gap,2)}')
print(f'과학 점수 차이 : {"+"+str(round(sci_gap,2)) if sci_gap > 0 else round(sci_gap,2)}')
print(f'국사 점수 차이 : {"+"+str(round(his_gap,2)) if his_gap > 0 else round(his_gap,2)}')

stu19Cnt_total = stu19Cnt_kor_avg +stu19Cnt_eng_avg +stu19Cnt_mat_avg +stu19Cnt_sci_avg +stu19Cnt_his_avg
stu19Cnt_avg = stu19Cnt_total / 5

hong_total = hong_kor_score+ hong_eng_score+hong_mat_score+hong_sci_score+hong_his_score
hong_avg = hong_total / 5

avg_gap = round(hong_avg - stu19Cnt_avg,2)
print(f'평균 점수 차이 : {"+"+str(round(avg_gap,2)) if avg_gap > 0 else round(avg_gap,2)}')
국어 점수 차이 : -3.16
영어 점수 차이 : +8.42
수학 점수 차이 : -8.42
과학 점수 차이 : +10.53
국사 점수 차이 : +8.42
평균 점수 차이 : +3.16
profile
취업공부

0개의 댓글