본문 바로가기

스크립트

[코딩테스트 입문] 평균 구하기

반응형

평균 구하기

1안) solution.py

def solution(arr):
    answer = 0
    
    answer = sum(arr) / len(arr)
    
    return answer

2안) solution.py

import statistics

def solution(arr):
    answer = 0
    
    answer = statistics.mean(arr)
    
    return answer

출처 - 프로그래머스(코딩테스트 연습) : https://school.programmers.co.kr/learn/courses/30/lessons/12944


파이썬 sum() 함수

a = (1, 2, 3, 4, 5)
x = sum(a)
print(x)
15

파이썬 len() 함수

mylist = ["apple", "orange", "cherry"]

x = len(mylist)

print(x)
3

파이썬 statistics.mean() 메서드

# Import statistics Library
import statistics 

# Calculate average values
print(statistics.mean([1, 3, 5, 7, 9, 11, 13]))
print(statistics.mean([1, 3, 5, 7, 9, 11]))
print(statistics.mean([-11, 5.5, -3.4, 7.1, -9, 22]))
7
6
1.8666666666666667

 

참고URL

- w3schools : https://www.w3schools.com/python/ref_func_sum.asp

- w3schools : https://www.w3schools.com/python/ref_func_len.asp

- w3schools : https://www.w3schools.com/python/ref_stat_mean.asp

- w3schools : https://www.w3schools.com/python/module_statistics.asp

 

728x90
반응형