
주요 학습내용
알고리즘 문제풀이(02)
1. 최댓값/최솟값 알고리즘
2. 최빈값 알고리즘
3. 근삿값 알고리즘
4. 평균 알고리즘

def getAvg(ns):
total = 0
for n in ns:
total += n
avg = round(total / len(ns), 2)
return avg
# 2) 최댓값 get
def getMaxScore(ns):
maxScore = ns[0] # 첫번째 인덱스 값으로 초기화 해줌
for n in ns:
if maxScore < n:
maxScore = n
return maxScore
def getDeviation(n1, n2):
# 방법 1) 강의 자료
return round(abs(n1 - n2), 2)
# def getDeviation(ns):
# 방법 2)
# dev = round(getMaxScore(ns) - getAvg(ns), 2)
# return dev
import mod
scores = [100, 64, 94, 66, 75, 58, 99, 76, 96, 74,
54, 73, 88, 70, 68, 50, 95, 89, 69, 98]
scores_avg = mod.getAvg(scores)
score_max = mod.getMaxScore(scores)
score_dev = mod.getDeviation(score_max, scores_avg)
print(f'scores_avg: {scores_avg}')
print(f'score_max: {score_max}')
print(f'score_dev: {score_dev}')
출력 결과 :

def getAvg(ns):
total = 0
for n in ns:
total += n
return round(total / len(ns), 2)
def getMinScore(ns):
minScore = ns[0] # 첫번째 인덱스 값으로 지정(순차적 비교)
for n in ns:
if minScore > n:
minScore = n
return minScore
def getDeviation(n1, n2):
return round(abs(n1-n2), 2)
import minMod
scores = [100, 64, 94, 66, 75, 58, 99, 76, 96, 74,
54, 73, 88, 70, 68, 50, 95, 89, 69, 98]
# 방법 1) 개별 모듈
score_avg = minMod.getAvg(scores)
score_min = minMod.getMinScore(scores)
score_dev = minMod.getDeviation(score_avg, score_min)
print(f'score_avg: {score_avg}')
print(f'score_min: {score_min}')
print(f'score_dev: {score_dev}')
출력 결과 :
1) 클래스 생성
class ScoreManagement:
def __init__(self, ss):
self.scores = ss
self.score_tot = 0
self.score_avg = 0
self.score_min = 0
self.score_max = 0
self.score_dev = 0
def getMinScore(self):
# 1)
if self.scores == None or len(self.scores) == 0: # if, 아직 초기화가 안된 경우
return None
self.score_min = self.scores[0] # 0번째 인덱스를 기준으로 시작
# self.scores가 있을 경우에만 사용할 수 있으므로, 위 첫번째(#1) 항목 넣어줌
for n in self.scores:
if self.score_min > n:
self.score_min = n
return self.score_min
def getMaxScore(self):
if self.scores == None or len(self.scores) == 0: # if, 아직 초기화가 안된 경우
return None
self.score_max = self.scores[0] # 0번째 인덱스를 기준으로 시작
for n in self.scores:
if self.score_max < n:
self.score_max = n
return self.score_max
def getTotalScore(self):
if self.scores == None or len(self.scores) == 0: # if, 아직 초기화가 안된 경우
return None
self.score_tot = 0
for n in self.scores:
self.score_tot += n
return self.score_tot
def getAvgScore(self):
if self.scores == None or len(self.scores) == 0: # if, 아직 초기화가 안된 경우
return None
self.score_avg = round(self.getTotalScore() / len(self.scores), 2)
return self.score_avg
def getMaxDeviation(self):
result = round(abs(self.getAvgScore()-self.getMaxScore()), 2)
return result
def getMinDeviation(self):
result = round(abs(self.getAvgScore() - self.getMinScore()), 2)
return result
import minMaxClass as mmc
scores = [100, 64, 94, 66, 75, 58, 99, 76, 96, 74,
54, 73, 88, 70, 68, 50, 95, 89, 69, 98]
sm = mmc.ScoreManagement(scores)
score_avg = sm.getAvgScore()
score_min = sm.getMinScore()
score_dev_min = sm.getMinDeviation()
score_max = sm.getMaxScore()
score_dev_max = sm.getMaxDeviation()
print(f'score_avg: {score_avg}')
print(f'score_min: {score_min}')
print(f'score_max: {score_max}')
print(f'score_dev_min: {score_dev_min}')
print(f'score_dev_max: {score_dev_max}')
출력 결과 :

class MaxAlgorithm:
def __init__(self, ns):
self.nums = ns
self.maxNum = 0
self.maxNumIdx = 0
def setMaxIdxAndNum(self):
self.maxNum = self.nums[0] # 인덱스 0번째부터 순차적으로
for idx, n in enumerate(self.nums):
if self.maxNum < n:
self.maxNum = n
self.maxNumIdx = idx
def getMaxNum(self):
return self.maxNum
def getMaxNumIdx(self):
return self.maxNumIdx
import maxMod
class ModeAlgorithm:
def __init__(self, ns, mn):
self.nums = ns
self.maxNum = mn
self.indexes = []
def setIndexList(self):
self.indexes = [0 for i in range(self.maxNum + 1)]
# +1안해도 47개인데 왜 +1?
# 0세 x => 0부터 48번째까지
for n in self.nums:
self.indexes[n] += 1
def getIndexList(self):
# indexes가 초기화가 안된 경우를 대비하여 아래 구문 추가
if sum(self.indexes) == 0:
return None
else:
return self.indexes
def printAges(self):
n = 1
while True:
maxAlo = maxMod.MaxAlgorithm(self.indexes)
# 인덱스리스트에서 가장 큰 값부터 출력
maxAlo.setMaxIdxAndNum()
maxNum = maxAlo.getMaxNum()
maxNumIdx = maxAlo.getMaxNumIdx() # self.indexes에서는 최빈값 n
# 아직 초기화가 안됐을 경우,
if maxNum == 0:
break
n += 1
print(f'{n:0>3} {maxNumIdx}세 빈도수: {maxNum} \t', end='')
# n:0>3 => 총 3자리로 맞추고, 오른쪽 정렬, 비어있는 자리 = 0
print('+' * maxNum)
self.indexes[maxNumIdx] = 0
n += 1 # 무한루프에 빠지지 않기 위해 추가
import modeMod
import maxMod
ages =[25, 27, 27, 24, 31, 34, 33, 31, 29, 25, 45,
37, 38, 46, 47, 22, 24, 29, 33, 35, 27, 34,
37, 40, 42, 29, 27, 25, 26, 27, 31, 31, 32,
38, 25, 27, 28, 40, 41, 34]
print(f'employee cnt: {len(ages)}명')
# 최댓값 구하기
mm = maxMod.MaxAlgorithm(ages)
mm.setMaxIdxAndNum()
maxAge = mm.getMaxNum()
print(f'max number: {maxAge}세')
# 최빈값 구하기
modem = modeMod.ModeAlgorithm(ages, maxAge)
modem.setIndexList()
print(f'IndexList: {modem.getIndexList()}')
print(f'IndexList: {len(modem.getIndexList())}')
result = modem.printAges()
출력 결과 :

class BmiAlgorithm:
def __init__(self, w, h):
self.BMISection = {18.5: ['저체중', '정상'],
23: ['정상', '과체중'],
25: ['과체중', '비만']}
self.weight = w
self.height = h
self.bmi = 0 # 몸무게 / 키^2
self.condition = '' # 최종적으로 결과 출력
self.nearNum = 0
self.minNum = 25 # 최댓값으로 우선 설정 후 비교해감
def calculatorBMI(self):
self.bmi = round(self.weight/(self.height*self.height), 2)
print(f'self.userBMI: {self.bmi}')
def printUserCondition(self):
for n in self.BMISection.keys():
absNum = abs(n - self.bmi)
if self.minNum > absNum:
self.minNum = absNum
self.nearNum = n
print(f'self.nearNum: {self.nearNum}')
if self.bmi <= self.nearNum:
self.condition = self.BMISection[self.nearNum][0]
else:
self.condition = self.BMISection[self.nearNum][1]
print(f'self.user condition: {self.condition}')
import nearMod
uWeight = float(input('input weight(kg): '))
uHeight = float(input('input height(m): '))
nr = nearMod.BmiAlgorithm(uWeight, uHeight)
nr.calculatorBMI()
nr.printUserCondition()
출력 결과 :

class MaxAlgorithm:
def __init__(self, ss):
self.scores = ss
self.maxScores = 0
self.maxIdx = 0
def removeMaxScore(self):
self.maxScores = self.scores[0] # 첫번째 인덱스 값으로 설정
for idx, n in enumerate(self.scores):
if self.maxScores < n:
self.maxScores = n
self.maxIdx = idx
print(f'self.maxScores: {self.maxScores}')
print(f'self.maxIdx: {self.maxIdx}')
self.scores.pop(self.maxIdx)
print(f'self.scores: {self.scores}')
class MinAlgorithm:
def __init__(self, ss):
self.scores = ss
self.minScores = 0
self.minIdx = 0
def removeMinScore(self):
self.minScores = self.scores[0] # 첫번째 인덱스 값으로 설정
for idx, n in enumerate(self.scores):
if self.minScores > n:
self.minScores = n
self.minIdx = idx
print(f'self.minScores: {self.minScores}')
print(f'self.MinIdx: {self.minIdx}')
self.scores.pop(self.minIdx)
print(f'self.scores: {self.scores}')
class Top5Players:
def __init__(self, cts, ns):
self.currentScores = cts
self.newScore = ns
def setAlignScore(self): # 재정렬하는 함수 만들어줌
nearIdx = 0
minNum = 10.0 # 최고점으로 설정
for idx, n in enumerate(self.currentScores):
absNum = abs(self.newScore - n)
if absNum < minNum:
minNum = absNum
nearIdx = idx
# 왼/오 어느쪽으로 삽입할 지 check
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 getFinalTop5Score(self):
return self.currentScores
import maxAlgorithm as mx
import minAlgorithm as mn
import nearAlgorithm as nr
top5Scores = [9.12, 8.95, 8.12, 6.9, 6.18]
scores = [6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 8.2, 6.2, 5.8]
maxA = mx.MaxAlgorithm(scores)
maxA.removeMaxScore()
minA = mn.MinAlgorithm(scores)
minA.removeMinScore()
total = 0
avg = 0
for n in scores:
total += n
total = round(total, 2)
avg = round(total / len(scores), 2)
print(f'total: {total}')
print(f'average: {avg}')
# 근사값 알고리즘 사용하여 가장 가까운 값 옆에 넣어주기
tp = nr.Top5Players(top5Scores, avg)
tp.setAlignScore()
top5Scores = tp.getFinalTop5Score()
print(f'top5Scores: {top5Scores}')
출력 결과 :