오늘은 알고리즘 강의 중 '순위' 에 대해서 실습까지 해보았다.
순위는 '랭킹' 과 같다. 어떤 값들의 순서(인덱스) 를 알아내
순서를 정렬해 보았다.
import random
nums = random.sample(range(50,101), 20)
ranks = [0 for i in range(20)]
print(nums)
print(ranks)
for idx, num1 in enumerate(nums):
for num2 in nums:
if num1 < num2:
ranks[idx] += 1
print(f'num: {num1} \t rank: {ranks[idx]+1 }')
결과)
[66, 96, 57, 91, 53, 88, 82, 64, 79, 60, 84, 52, 77, 76, 86, 85, 93, 94, 51, 81]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
num: 66 rank: 14
num: 96 rank: 1
num: 57 rank: 17
num: 91 rank: 4
num: 53 rank: 18
num: 88 rank: 5
num: 82 rank: 9
num: 64 rank: 15
num: 79 rank: 11
num: 60 rank: 16
num: 84 rank: 8
num: 52 rank: 19
num: 77 rank: 12
num: 76 rank: 13
num: 86 rank: 6
num: 85 rank: 7
num: 93 rank: 3
num: 94 rank: 2
num: 51 rank: 20
num: 81 rank: 10
왠지 학교 시험점수를 랭킹별로 나타내거나 어떤 시간차 순으로 무언가를 기록하고 정리해야 하는
상황에서 많이 쓰일 수 있을 것 같다.
두번째 실습은 class 모듈을 따로 만들어 진행했는데,
그래서 그런지 강의를 따라가는 식으로 되었다.
아직은 스스로 하기 어려울 것 같다.
class RankDeviation:
def __init__(self, mss , ess):
self.midStuScos = mss
self.endStuScos = ess
self.midRanks = [0 for i in range(len(mss))]
self.endRanks = [0 for i in range(len(mss))]
self.rankDeviation = [0 for i in range(len(mss))]
def setRank(self, ss, rs):
for idx, sco1 in enumerate(ss):
for sco2 in ss:
if sco1 < sco2:
rs[idx] += 1
def setMidRank(self):
self.setRank(self.midStuScos, self.midRanks)
def getMidRank(self):
return self.midRanks
def setEndRank(self):
self.setRank(self.endStuScos, self.endRanks)
def getEndRank(self):
return self.endRanks
def printRankDeviation(self):
for idx, mRank in enumerate(self.midRanks):
deviation = mRank = self.endRanks[idx]
if deviation > 0:
deviation = '↑' + str(abs(deviation))
elif deviation < 0 :
deviation = '↓' + str(abs(deviation))
else:
deviation = '=' + str(abs(deviation))
print( f'mid_rank: {mRank} \t end_rank: {self.endRank[idx]} \t Deviation: {deviation} ')
모듈 정보이다.
import rankMod as rm
import random
midStuScos = random.sample(range(50,101), 20)
endStuScos = random.sample(range(50,101), 20)
rd = rm.RankDeviation(midStuScos, endStuScos)
rd.setMidRank()
print(f'midStuScors: {midStuScos}')
print(f'mid_rank: {rd.getMidRank()}')
rd.setEndRank()
print(f'endStuScors: {endStuScos}')
print(f'end_rank : {rd.getEndRank()}')
rd.printRankDeviation()
위의 코드로 rankMod 모듈을 불러와서 실행을 한다.
결과는)
midStuScors: [59, 66, 51, 53, 67, 90, 50, 76, 87, 58, 65, 78, 64, 80, 61, 70, 84, 71, 60, 94]
mid_rank: [15, 10, 18, 17, 9, 1, 19, 6, 2, 16, 11, 5, 12, 4, 13, 8, 3, 7, 14, 0]
endStuScors: [92, 73, 54, 66, 68, 63, 82, 93, 64, 75, 86, 62, 55, 99, 60, 87, 53, 94, 84, 88]
end_rank : [3, 10, 18, 12, 11, 14, 8, 2, 13, 9, 6, 15, 17, 0, 16, 5, 19, 1, 7, 4]
mid_rank: 15 end_rank: 3 Deviation: ↑12
mid_rank: 10 end_rank: 10 Deviation: =0
mid_rank: 18 end_rank: 18 Deviation: =0
mid_rank: 17 end_rank: 12 Deviation: ↑5
mid_rank: 9 end_rank: 11 Deviation: ↓2
mid_rank: 1 end_rank: 14 Deviation: ↓13
mid_rank: 19 end_rank: 8 Deviation: ↑11
mid_rank: 6 end_rank: 2 Deviation: ↑4
mid_rank: 2 end_rank: 13 Deviation: ↓11
mid_rank: 16 end_rank: 9 Deviation: ↑7
mid_rank: 11 end_rank: 6 Deviation: ↑5
mid_rank: 5 end_rank: 15 Deviation: ↓10
mid_rank: 12 end_rank: 17 Deviation: ↓5
mid_rank: 4 end_rank: 0 Deviation: ↑4
mid_rank: 13 end_rank: 16 Deviation: ↓3
mid_rank: 8 end_rank: 5 Deviation: ↑3
mid_rank: 3 end_rank: 19 Deviation: ↓16
mid_rank: 7 end_rank: 1 Deviation: ↑6
mid_rank: 14 end_rank: 7 Deviation: ↑7
mid_rank: 0 end_rank: 4 Deviation: ↓4
이런식으로 아주 아름답게 나오는 것을 볼 수가 있다..!
함수가 길어지거나 많아질 경우에는 코드 가독성과 관리를 위해
모듈화를 시켜서 정리해 주는 방법이 색다르면서도 어려우면서도 깔끔하긴 했다.