230427_제로베이스데이터스쿨_06031~06050

김지태·2023년 4월 28일
0
post-thumbnail

06_031 선형 검색 알고리즘 1

def searchNumberByLineAlgorithm(ns, sn):

searchResultIdx = -1

print(f'Numbers: {ns}')
print(f'Search Numbers: {sn}')

n = 0
while True:

    if n == len(ns):
        print('Search FAIL!!')
        break

    if ns[n] == sn:
        searchResultIdx = n
        print('Search SUCCESS!!')
        print(f'Search result INDEX: {searchResultIdx}')
        break

    n += 1

return searchResultIdx

import lineMod
import random

if name == 'main':

rNums = random.sample(range(1, 21), 10)
searchNum = int(input('input search number: '))

resultIdx = lineMod.searchNumberByLineAlgorithm(rNums, searchNum)
if resultIdx == -1:
    print('No resutls found')
    print(f'search result index: {resultIdx}')

else:
    print('>>> Search Results <<<')
    print(f'search result index: {resultIdx}')
    print(f'search result number: {rNums[resultIdx]}')

06_032 검색알고리즘2

binaryMod.py

def searchNumberByBinaryAlgorithm(ns, sn):

searchResultIdx = -1

staIdx = 0
endIdx = len(ns) - 1
midIdx = (staIdx + endIdx) // 2
midVal = ns[midIdx]

print(f'staIdx: {staIdx}, endIdx: {endIdx}')
print(f'midIdx: {midIdx}, midVal: {midVal}')

while sn >= ns[0] and sn <= ns[len(ns) - 1]:

    if sn == ns[len(ns) - 1]:
        searchResultIdx = len(ns) - 1
        break

    if staIdx + 1 == endIdx:
        if ns[staIdx] != sn and ns[endIdx] != sn: break

    if sn > midVal:
        staIdx = midIdx
        midIdx = (staIdx + endIdx) // 2
        midVal = ns[midIdx]
        print(f'+staIdx: {staIdx}, endIdx: {endIdx}')
        print(f'+midIdx: {midIdx}, midVal: {midVal}')

    elif sn < midVal:
        endIdx = midIdx
        midIdx = (staIdx + endIdx) // 2
        midVal = ns[midIdx]
        print(f'-staIdx: {staIdx}, endIdx: {endIdx}')
        print(f'-midIdx: {midIdx}, midVal: {midVal}')

    elif sn == midVal:
        searchResultIdx = midIdx
        break

return searchResultIdx

ex.py

import binaryMod
import random

if name == 'main':

nums = [1, 2, 4, 6, 7, 8, 10, 11, 13, 15, 16, 17, 20, 21, 23, 24, 27, 28]
searchNum = int(input('input search number: '))

resultIdx = binaryMod.searchNumberByBinaryAlgorithm(nums, searchNum)
print(f'nums: {nums}')

if resultIdx == -1:
    print('No results found.')
    print(f'search result index: {resultIdx}')

else:
    print('>>> Search Results <<<')
    print(f'search result index: {resultIdx}')
    print(f'search result number: {nums[resultIdx]}')
    
    

06_033 순위알고리즘1

rankMod.py

def rankAlgorithm(ns):

ranks = [0 for i in range(len(ns))]

for idx, n1 in enumerate(ns):
    for n2 in ns:
        if n1 < n2:
            ranks[idx] += 1

print(f'nums: {ns}')
print(f'ranks: {ranks}')

for i, n in enumerate(ns):
    print(f'num: {n} \t rank:{ranks[i] + 1}')

sortedNums = [0 for n in range(len(ns))]

for idx, rank in enumerate(ranks):
    sortedNums[rank] = ns[idx]

return sortedNums

ex.py

import random
import rankMod

if name == 'main':
nums = random.sample(range(50, 101), 20)
sNums = rankMod.rankAlgorithm(nums)
print(f'sNums:\n{sNums}')

06_034 순위알고리즘2

bubbleMod.py

import copy

def sortBybubleSortAlgorithm(ns, asc=True):

c_ns = copy.copy(ns)

length = len(c_ns) - 1
for i in range(length):
    for j in range(length-i):

        if asc:
            if c_ns[j] > c_ns[j+1]:
                c_ns[j], c_ns[j+1] = c_ns[j+1], c_ns[j]
        else:
            if c_ns[j] < c_ns[j+1]:
                c_ns[j], c_ns[j+1] = c_ns[j+1], c_ns[j]

        print(f'ns: {c_ns}')
    print()

return c_ns

ex.py

import random
import bubleMod

if name == 'main':
nums = random.sample(range(1, 20), 10)
print(f'not sorted nums: {nums}')

result = bubleMod.sortBybubleSortAlgorithm(nums)
print(f'sorted nums by ASC: {result}')

result = bubleMod.sortBybubleSortAlgorithm(nums, asc=False)
print(f'sorted nums by DESC: {result}')

06_035 정렬알고리즘1

insertMod.py

import copy

def sortInsertSortAlgorithm(ns, asc=True):

c_ns = copy.copy(ns)

for i1 in range(1, len(c_ns)):
    i2 = i1 - 1
    cNum = c_ns[i1]

    if asc:     # ascending
        while c_ns[i2] > cNum and i2 >= 0:
            c_ns[i2 + 1] = c_ns[i2]
            i2 -= 1

    else:       # descending
        while c_ns[i2] < cNum and i2 >= 0:
            c_ns[i2 + 1] = c_ns[i2]
            i2 -= 1

    c_ns[i2+1] = cNum
    print(f'nums: {c_ns}')

return c_ns

ex.py

import random
import insertMod

if name == 'main':
nums = random.sample(range(1, 20), 10)

print(f'not sorted nums:\n{nums}')
result = insertMod.sortInsertSortAlgorithm(nums)
print(f'sorted nums by ASC:\n{result}')

print(f'not sorted nums:\n{nums}')
result = insertMod.sortInsertSortAlgorithm(nums, asc=False)
print(f'sorted nums by DESC:\n{result}')

06_036 정렬알고리즘2

selectMod.py

import copy

def sortSelectSortAlgorithm(ns, asc=True):

c_ns = copy.copy(ns)

for i in range(len(c_ns) - 1):
    minIdx = i

    for j in range(i + 1, len(c_ns)):

        if asc:     # ascending
            if c_ns[minIdx] > c_ns[j]:
                minIdx = j
        else:       # descending
            if c_ns[minIdx] < c_ns[j]:
                minIdx = j

    c_ns[i], c_ns[minIdx] = c_ns[minIdx], c_ns[i]

    print(f'nums: {c_ns}')

return c_ns

ex.py

import random
import selectMod

if name == 'main':
nums = random.sample(range(1, 20), 10)

print(f'not sorted nums:\n{nums}')
result = selectMod.sortSelectSortAlgorithm(nums)
print(f'sorted nums by ASC:\n{result}')

print(f'not sorted nums:\n{nums}')
result = selectMod.sortSelectSortAlgorithm(nums, asc=False)
print(f'sorted nums by DESC:\n{result}')

06_037 정렬알고리즘3

maxMod.py

class MaxAlgorithm:

def __init__(self, ns):
    self.nums = ns
    self.maxNum = 0
    self.maxNumCnt = 0

def setMaxNum(self):
    self.maxNum = 0

    for n in self.nums:
        if self.maxNum < n:
            self.maxNum = n

def getMaxNum(self):
    self.setMaxNum()

    return self.maxNum

def setMaxNumCnt(self):
    self.setMaxNum()

    for n in self.nums:
        if self.maxNum == n:
            self.maxNumCnt += 1

def getMaxNumCnt(self):
    self.setMaxNumCnt()

    return self.maxNumCnt

ex.py

import random
import maxMod

if name == 'main':

nums = []
for n in range(30):
    nums.append(random.randint(1, 50))

print(f'nums:\n{nums}')
ma = maxMod.MaxAlgorithm(nums)
print(f'max num: {ma.getMaxNum()}')
print(f'max num cnt: {ma.getMaxNumCnt()}')

06_038 정렬알고리즘4

minMod.py

class MinAlgorithm:

def __init__(self, ns):
    self.nums = ns
    self.minNum = 0
    self.minNumCnt = 0

def setMinNum(self):
    self.minNum = 51

    for n in self.nums:
        if self.minNum > n:
            self.minNum = n

def getMinNum(self):
    self.setMinNum()

    return self.minNum

def setMinNumCnt(self):
    self.setMinNum()

    for n in self.nums:
        if self.minNum == n:
            self.minNumCnt += 1

def getMinNumCnt(self):
    self.setMinNumCnt()

    return self.minNumCnt

ex.py

import random
import minMod

if name == 'main':

nums = []
for n in range(30):
    nums.append(random.randint(1, 50))

print(f'nums:\n{nums}')
ma = minMod.MinAlgorithm(nums)
print(f'min num: {ma.getMinNum()}')
print(f'min num cnt: {ma.getMinNumCnt()}')

06_039 최댓값알고리즘1

modeMod.py

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)]

    for n in self.nums:
        self.indexes[n] = self.indexes[n] + 1

def getIndexList(self):
    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()

        if maxNum == 0:
            break

        print(f'[{n:0>3}] {maxNumIdx}세 빈도수: {maxNum}\t', end='')
        print('+' * maxNum)
        self.indexes[maxNumIdx] = 0

        n += 1
        
        
        

maxMod.py

class MaxAlgorithm:

def __init__(self, ns):
    self.nums = ns
    self.maxNum = 0
    self.maxNumIdx = 0

def setMaxIdxAndNum(self):
    self.maxNum = 0
    self.maxNumIdx = 0

    for i, n in enumerate(self.nums):
        if self.maxNum < n:
            self.maxNum = n
            self.maxNumIdx = i

    return self.maxNum

def getMaxNum(self):
    return self.maxNum

def getMaxNumIdx(self):
    return self.maxNumIdx;
  
  

ex.py

import maxMod
import modeMod

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)}명')

maxAlg = maxMod.MaxAlgorithm(ages)
maxAlg.setMaxIdxAndNum()
maxAge = maxAlg.getMaxNum()
print(f'maxAge: {maxAge}세')

modAlg = modeMod.ModeAlgorithm(ages, maxAge)
modAlg.setIndexList()
print(f'IndexList: {modAlg.getIndexList()}')

modAlg.printAges()

06_040 최댓값알고리즘2

nearMod.py

class NearAlgorithm:

def __init__(self, d):
    self.temps = {0:24, 5:22, 10:20, 15:16, 20:13, 25:10, 30:6}
    self.depth = d
    self.nearNum = 0
    self.minNum = 24

def getNearNumber(self):

    for n in self.temps.keys():
        absNum = abs(n - self.depth)
        if absNum < self.minNum:
            self.minNum = absNum
            self.nearNum = n

    return self.temps[self.nearNum]
  

ex.py

import nearMod

depth = int(float(input('input depth: ')))
print(f'depth: {depth}m')

na = nearMod.NearAlgorithm(depth)
temp = na.getNearNumber()
print(f'water temperature: {temp}도')

06_041 최솟값알고리즘1

nearMod.py

class BmiAlgorithm:

def __init__(self, w, h):
    self.BMISection = {18.5:['저체중', '정상'],
                       23:['정상','과체중'],
                       25:['과체중', '비만']}
    self.userWeight = w
    self.userHeight = h
    self.userBMI = 0
    self.userCondition = ''
    self.nearNum = 0
    self.minNum = 25
    

def calculatorBMI(self):
    self.userBMI = round(self.userWeight / (self.userHeight * self.userHeight), 2)
    print(f'userBMI: {self.userBMI}')


def printUserCondition(self):
    for n in self.BMISection.keys():
        absNum = abs(n - self.userBMI)
        if absNum < self.minNum:
            self.minNum = absNum
            self.nearNum = n
    print(f'self.nearNum: {self.nearNum}')

    if self.userBMI <= self.nearNum:
        self.userCondition = self.BMISection[self.nearNum][0]
    else:
        self.userCondition = self.BMISection[self.nearNum][1]
    print(f'self.userCondition: {self.userCondition}')

ex.py

import nearMod

uWeight = float(input('input weight(Kg): '))
uHeight = float(input('input height(m): '))

na = nearMod.BmiAlgorithm(uWeight, uHeight)
na.calculatorBMI()
na.printUserCondition()

06_042 최솟값알고리즘2

maxAlgorithm.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, s in enumerate(self.scores):
        if self.maxScore < s:
            self.maxScore = s
            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}')

minAlgorithm.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, s in enumerate(self.scores):
        if self.minScore > s:
            self.minScore = s
            self.minIdx = i
    print(f'self.minScore: {self.minScore}')
    print(f'self.minIdx: {self.minIdx}')

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

nearAlgorithm.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 getFinalTop5Scroes(self):
    return self.currentScores

ex.py

import maxAlgorithm, minAlgorithm
import nearAlgorithm

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: {scores}')

maxA = maxAlgorithm.MaxAlgorithm(scores)
maxA.removeMaxScore()

minA = minAlgorithm.MinAlgorithm(scores)
minA.removeMinScore()

total = 0
average = 0

for n in scores:
total += n

average = round(total / len(scores), 2)

print(f'total: {round(total, 2)}')
print(f'average: {average}')

tp = nearAlgorithm.Top5Players(top5Scores, average)
tp.setAlignScore()
top5Scores = tp.getFinalTop5Scroes()
print(f'top5Scores: {top5Scores}')

06_043 최빈값알고리즘1

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)}')

06_044 최빈값알고리즘2

sales = [12000, 13000, 12500, 11000, 10500, 98000, 91000, 91500, 10500, 11500, 12000, 12500]

def salesUpAndDown(ss):

if len(ss) == 1:
    return ss

print(f'sales: {ss}')
currentSales = ss.pop(0)
nextSales = ss[0]
increase = nextSales - currentSales
if increase > 0:
    increase = '+' + str(increase)
print(f'매출 증감액: {increase}')

return salesUpAndDown(ss)

if name == 'main':
salesUpAndDown(sales)

06_045 근삿값알고리즘1

mod.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

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)
  
  

ex.py

import mod

num1 = int(input(f'input number1: '))
num2 = int(input(f'input number2: '))
ns = mod.NumsSum(num1, num2)
result = ns.sumBetweenNums()
print(f'result: {result}')

06_046 근삿값알고리즘2

datas = [32, 'a', 'z', 45, 'G', 39, 50, 'T', 't', 22, 31, 55, 's', 63, 59, 'E']
print(f'datas: {datas}')

ascIIDatas = []
for data in datas:
if str(data).isalpha():
ascIIDatas.append(ord(data))
continue

ascIIDatas.append(data)

print(f'ascIIDatas: {ascIIDatas}')

ranks = [0 for i in range(len(ascIIDatas))]

for idx, data1 in enumerate(ascIIDatas):
for data2 in ascIIDatas:
if data1 < data2:
ranks[idx] += 1

print(f'ranks: {ranks}')

for i, d in enumerate(datas):
print(f'data:{d:>2} \t rank:{ranks[i] + 1}')

06_047 재귀알고리즘1

mergeAlgorithm.py

def mSort(ns, asc=True):
if len(ns) < 2:
return ns

midIdx = len(ns) // 2
leftNums = mSort(ns[0:midIdx], asc=asc)
rightNums = mSort(ns[midIdx:len(ns)], asc=asc)

mergedNums = []
leftIdx = 0; rightIdx = 0
while leftIdx < len(leftNums) and rightIdx < len(rightNums):

    if asc:
        if leftNums[leftIdx] < rightNums[rightIdx]:
            mergedNums.append(leftNums[leftIdx])
            leftIdx += 1
        else:
            mergedNums.append(rightNums[rightIdx])
            rightIdx += 1

    else:
        if leftNums[leftIdx] > rightNums[rightIdx]:
            mergedNums.append(leftNums[leftIdx])
            leftIdx += 1
        else:
            mergedNums.append(rightNums[rightIdx])
            rightIdx += 1

mergedNums += leftNums[leftIdx:]
mergedNums += rightNums[rightIdx:]

print(f'mergedNums: {mergedNums}')
return mergedNums

ex.py

import random
import mergeAlgorithm

rNums = random.sample(range(1, 101), 20)

print(f'not sorted rNums: {rNums}')
print(f'merge sorted rNums by ASC: {mergeAlgorithm.mSort(rNums)}')
print(f'merge sorted rNums by DESC: {mergeAlgorithm.mSort(rNums, asc=False)}')

06_048 재귀알고리즘2

mod.py

def getAvg(ns):

total = 0
for n in ns:
    total += n

return total / len(ns)

def getMax(ns):

maxN = ns[0]
for n in ns:
    if maxN < n:
        maxN = n

return maxN

def getDeviation(n1, n2):
return round(abs(n1 - n2), 2)

ex.py

import mod

scores = [100, 64, 94, 66, 75, 58, 99, 76, 96, 74,
54, 73, 88, 70, 68, 50, 95, 89, 69, 98]

score_avg = mod.getAvg(scores)
score_max = mod.getMax(scores)
deviation = mod.getDeviation(score_avg, score_max)

print(f'score_avg: {score_avg}')
print(f'score_max: {score_max}')
print(f'deviation: {deviation}')

06_049 평균알고리즘1

mod.py

def getAvg(ns):

total = 0
for n in ns:
    total += n

return total / len(ns)

def getMin(ns):

minN = ns[0]
for n in ns:
    if minN > n:
        minN = n

return minN

def getDeviation(n1, n2):
return round(abs(n1 - n2), 2)

def getMaxOrMin(ns, maxFlag = True):

resultN = ns[0]
for n in ns:
    if maxFlag:
        if resultN < n:
            resultN = n
    else:
        if resultN > n:
            resultN = n

return resultN

mod2.py

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

def getMinScore(self):
    if self.scores == None or len(self.scores) == 0:
        return None

    self.score_min = self.scores[0]
    for score in self.scores:
        if self.score_min > score:
            self.score_min = score

    return self.score_min

def getMaxScore(self):
    if self.scores == None or len(self.scores) == 0:
        return None

    self.score_max = self.scores[0]
    for score in self.scores:
        if self.score_max < score:
            self.score_max = score

    return self.score_max

def getTotScore(self):
    if self.scores == None or len(self.scores) == 0:
        return None

    self.score_tot = 0
    for score in self.scores:
        self.score_tot += score

    return self.score_tot

def getAvgScore(self):
    if self.scores == None or len(self.scores) == 0:
        return None

    self.score_avg = round(self.getTotScore() / len(self.scores), 2)
    return self.score_avg

def getMaxDeviation(self):
    result = abs(self.getAvgScore() - self.getMaxScore())
    return round(result, 2)

def getMinDeviation(self):
    result = abs(self.getAvgScore() - self.getMinScore())
    return round(result, 2)

ex.py

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)
scores_min = mod.getMaxOrMin(scores, maxFlag=False)
deviation = mod.getDeviation(scores_avg, scores_min)

print(f'scores_avg: {scores_avg}')
print(f'scores_min: {scores_min}')
print(f'deviation: {deviation}')

import mod2

sm = mod2.ScoreManagement(scores)
print(f'score_avg: {sm.getAvgScore()}')
print(f'score_min: {sm.getMinScore()}')
print(f'score_max: {sm.getMaxScore()}')
print(f'score_min_deviation: {sm.getMinDeviation()}')
print(f'score_max_deviation: {sm.getMaxDeviation()}')

06_050 평균알고리즘2

mod.py

class LottoMode:

def __init__(self, ln):
    self.lottoNums = ln
    self.modeList = [0 for n in range(1, 47)]

def getLottoNumMode(self):

    for roundNums in self.lottoNums:
        for num in roundNums:
            self.modeList[num] = self.modeList[num] + 1

    return self.modeList

def printModeList(self):
    if sum(self.modeList) == 0:
        return None

    for i, m in enumerate(self.modeList):
        if i != 0:
            print(f'번호: {i:>2}, 빈도: {m}, {"*" * m}')

ex.py

import mod

lottoNums = [[13, 23, 15, 5, 6, 39], [36, 13, 5, 3, 30, 16], [43, 1, 15, 9, 3, 38],
[32, 42, 24, 45, 8, 31], [18, 39, 41, 11, 4, 9], [12, 39, 11, 38, 32, 5],
[29, 25, 13, 6, 14, 8], [21, 33, 19, 20, 42, 7], [6, 28, 3, 45, 41, 24],
[42, 15, 8, 5, 35, 4], [14, 4, 35, 24, 29, 3], [15, 20, 6, 37, 34, 39],
[27, 5, 32, 15, 25, 19], [45, 25, 2, 8, 30, 43], [4, 19, 33, 10, 6, 24],
[25, 26, 45, 23, 24, 16], [33, 28, 45, 21, 38, 24], [4, 30, 29, 28, 32, 38],
[11, 28, 12, 2, 42, 3], [40, 29, 16, 8, 9, 28], [6, 9, 37, 30, 3, 35],
[29, 18, 41, 28, 38, 15], [9, 31, 13, 44, 1, 36], [36, 1, 37, 32, 15, 12],
[41, 32, 16, 6, 26, 33], [12, 43, 10, 29, 39, 9], [41, 9, 23, 35, 18, 17],
[35, 38, 3, 28, 36, 31], [21, 44, 4, 29, 18, 7], [20, 23, 6, 2, 34, 44]]

lm = mod.LottoMode(lottoNums)
mList = lm.getLottoNumMode()

print(f'mList: {mList}')

lm.printModeList()

profile
데이터 분석가

0개의 댓글