관리 메뉴

RUBY

평균(파이썬) 본문

프로그래밍 언어/Python

평균(파이썬)

ruby-jieun 2023. 1. 25. 22:17

 

 

 

평균


 

 

 

 

평균

여러 수나 양의 중간값을 갖는 수를 평균이라고 한다

 

 

 

 

0~99까지의 수 중 30개의 랜덤 숫자의 평균

import random

nums = random.sample(range(0, 100), 30)
print(f'nums: {nums}')

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

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

 

 

 

50이상 90이하 수들의 평균

import random

nums = random.sample(range(0, 100), 30)
print(f'nums: {nums}')

targetNums = []
total = 0
for n in nums:
    if n >= 50 and n <= 90:
        total += n
        targetNums.append(n)

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

 

 

 

정수들의 평균

nums = [4, 5.12, 0, 5, 7.34, 9.1, 9, 3, 3.159, 1, 11, 12.789]
print(f'nums: {nums}')

targetNums = []
total = 0
for n in nums:
    if n - int(n) == 0:
        total += n
        targetNums.append(n)

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

 

 

 

 

실수(소수)들의 평균

nums = [4, 5.12, 0, 5, 7.34, 9.1, 9, 3, 3.159, 1, 11, 12.789]
print(f'nums: {nums}')

targetNums = []
total = 0
for n in nums:
    if n - int(n) != 0:
        total += n
        targetNums.append(n)

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

 

 

 

 

다음은 어떤 체조선수의 점수이다.평균을 구하고 순위를 정하는 알고리즘을 만들어보자.

class Top5Players:

    def __init__(self, cs, ns):
        self.currentScores = cs
        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

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('현재 전체 순위')
print(f'1위 → {top5PlayerScores[0]}')
print(f'2위 → {top5PlayerScores[1]}')
print(f'3위 → {top5PlayerScores[2]}')
print(f'4위 → {top5PlayerScores[3]}')
print(f'5위 → {top5PlayerScores[4]}')

total = 0
average = 0

for n in scores:
    total += n

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

print(f'\n총합점수: {total}')
print(f'평균점수: {average}')

tp = Top5Players(top5PlayerScores, average)
tp.setAlignScore()
top5PlayerScores = tp.getFinalTop5Scores()
print('\n최종 전체 순위')
print(f'1위 → {top5PlayerScores[0]}')
print(f'2위 → {top5PlayerScores[1]}')
print(f'3위 → {top5PlayerScores[2]}')
print(f'4위 → {top5PlayerScores[3]}')
print(f'5위 → {top5PlayerScores[4]}')
현재 전체 순위
1위 → 9.12
2위 → 8.95
3위 → 8.12
4위 → 7.9
5위 → 7.88

총합점수: 83.9
평균점수: 8.39

최종 전체 순위
1위 → 9.12
2위 → 8.95
3위 → 8.39
4위 → 8.12
5위 → 7.9

'프로그래밍 언어 > Python' 카테고리의 다른 글

하노이의 탑(파이썬)  (0) 2023.01.25
재귀 알고리즘(파이썬)  (0) 2023.01.25
근삿값(파이썬)  (0) 2023.01.25
최빈값(파이썬)  (0) 2023.01.25
최솟값(파이썬)  (0) 2023.01.25
Comments