import random
nums = random.sample(range(0,100),10)
print(f'nums : {nums}')
total = 0
for n in nums:
total += n
average = total / len(nums)
print(f'average : {round(average,2)}')
Python
복사
class Top5Players:
def __init__(self,cs,ns): # 현재 점수와 새 점수를 받아온다.
self.currentScores = cs
self.newScore = ns
# 새 점수가 현재 점수에서 어느 순위에 들어가야되는지 확인
def setAlignScore(self):
nearIdx = 0 # 가까운 점수의 위치 저장
nearScore = 0 # 가까운 점수 저장
minNum = 10.0 # 절댓값의 차이와 비교해줄 변수 -> 나올 수 없는 큰 값으로 초기화
for i, s in enumerate(self.currentScores):
absNum = abs(self.newScore - s)
if absNum < minNum:
minNum = absNum
nearIdx = i
nearScore = s
if self.newScore >= nearScore: # 앞에 들어가야된다면 (더 크면 앞으로 들어간다)
for i in range(len(self.currentScores)-1, nearIdx, -1): # nearIdx 뒤에서부터의 값들을 뒤로 미뤄줘야 한다.
self.currentScores[i] = self.currentScores[i - 1]
self.currentScores[nearIdx] = self.newScore # nearIdx 위치에는 새로 들어온 점수의 값을 집어넣는다.
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
Python
복사
import near
scores = [8.9, 7.6, 8.2, 9.1, 8.8, 8.1, 7.9, 9.4, 7.2, 8.7]
top5PlayerScores = [9.12, 8.95, 8.12, 7.90, 7.88]
print(f'top5PlayerScores: {top5PlayerScores}')
total = 0; average = 0
for n in scores:
total += n
average = round(total / len(scores),2)
print(f' total: {total}')
print(f' average: {average}')
tp = near.Top5Players(top5PlayerScores, average)
tp.setAlignScore()
top5PlayerScores = tp.getFinalTop5Scores()
print(f'top5PlayerScores : {top5PlayerScores}')
Python
복사
class Top5Players:
def __init__(self, cs, ns): # 현재 점수와 새 점수를 받아온다.
self.currentScores = cs
self.newScore = ns
def AlignScore(self):
nearIdx = 0
diffNum = 10.0
for i, s in enumerate(self.currentScores):
absNum = abs(self.newScore - s)
if absNum < diffNum:
diffNum = 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 getFinal(self):
return self.currentScores
Python
복사
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'max score : {self.maxScore}')
self.scores.pop(self.maxIdx)
print(f'최댓값 삭제한 뒤 scores : {self.scores}')
Python
복사
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'min score : {self.minScore}')
self.scores.pop(self.minIdx)
print(f'최솟값 삭제한 뒤 scores : {self.scores}')
Python
복사
# 사용자가 정수 2개를 입력하면 작은 정수와 큰 정수 사이의 모든 합을 구하는 재귀 프로그램을
# 만들자.
import near, max, min
top5Score = [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]
print(f'scores : {scores}')
maxA = max.MaxAlgorithm(scores)
maxA.removeMaxScore()
minA = min.MinAlgorithm(scores)
minA.removeMinScore()
total = 0
average = 0
for i in scores:
total += i
average = round(total / len(scores),2)
print(f'total : {round(total,2)}')
print(f'average : {average}')
tp = near.Top5Players(top5Score,average)
tp.AlignScore()
top5Score_New = tp.getFinal()
print(f'new top5 player score : {top5Score_New}')
Python
복사
# 사용자가 정수 2개를 입력하면 작은 정수와 큰 정수 사이의 모든 합을 구하는 재귀 프로그램을
# 만들자.
import near, max, min
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
Stu19_kor_tot = kor_avg * 20 - hong_kor_score
Stu19_eng_tot = eng_avg * 20 - hong_eng_score
Stu19_mat_tot = mat_avg * 20 - hong_mat_score
Stu19_sci_tot = sci_avg * 20 - hong_sci_score
Stu19_his_tot = his_avg * 20 - hong_his_score
avg19_kor = Stu19_kor_tot / 19
avg19_eng = Stu19_eng_tot / 19
avg19_mat = Stu19_mat_tot / 19
avg19_his = Stu19_his_tot / 19
avg19_sci = Stu19_sci_tot / 19
kor_gap = hong_kor_score - avg19_kor
eng_gap = hong_eng_score - avg19_eng
mat_gap = hong_mat_score - avg19_mat
his_gap = hong_his_score - avg19_his
sci_gap = hong_sci_score - avg19_sci
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(his_gap,2)) if his_gap > 0 else round(his_gap,2)}')
print(f'과학 점수 차이 : {"+" + str(round(sci_gap,2)) if sci_gap > 0 else round(sci_gap,2)}')
# 홍길동 학생을 제외하고 난 19명의 전체 과목 평균
avg19 = round((avg19_kor + avg19_eng + avg19_mat + avg19_his + avg19_sci) / 5,2)
hong_total = hong_kor_score + hong_eng_score + hong_mat_score + hong_sci_score + hong_his_score
hong_avg = round(hong_total / 5,2)
avg_gap = hong_avg - avg19
print(f'전체 평균 점수 차이 : {"+" + str(round(avg_gap,2)) if avg_gap > 0 else round(avg_gap,2)}')
Python
복사