Prepare > Python > Basic Data Types > Finding the percentage

2023. 6. 26. 20:21HackerRank-Python

 

Finding the percentage | HackerRank

Store a list of students and marks in a dictionary, and find the average marks obtained by a student.

www.hackerrank.com

 

문제


he provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students. Print the average of the marks array for the student name provided, showing 2 places after the decimal.

 

 

 

 

코드


if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    
    target = student_marks[query_name]

    print('{:.2f}'.format(sum(target)/len(target)))

 

 

노트


목표학생 점수 뽑아내기

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    
    print(student_marks[query_name])
    print(type(student_marks[query_name]))
[52.0, 56.0, 60.0]
<class 'list'>
  • 목표한 학생의 점수는 리스트에 담겨있구나!
  • 이제 평균을 구해야지

 

 

평균 구하기

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    

    target = student_marks[query_name]
    print(
        sum(target)/len(target)
    )
56.0
  • 근데 자릿수가....흠

 

 

자릿수 맞추기

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    
    target = student_marks[query_name]

    print('{:.2f}'.format(sum(target)/len(target)))
  • f-string 방식도 써볼까
print(
        f'{sum(target)/len(target):.2f}'
        )
  • numpy array로 바꿔서 target.mean() 하는 방법도 있을 듯
  • 콜론(:)을 붙이고 소숫점 아래 N 자리까지 나타내겠다고 지정해줌
  • ( :.2f) 는 앞의 문자열에 소숫점아래 두 자리까지 나타낸다

 

 

 

두 번째 시도

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    avg = sum(student_marks.get(query_name))/len(student_marks.get(query_name))
    print(f'{avg:.2f}')
  • f-string으로 자릿수 표현

 

참조


 

[Python3] 자릿수 맞추기

소수점 자릿수 맞추기0으로 채우기d 앞의 숫자를 조절하여 몇 자리로 채울지 정할 수 있다.위 둘을 조합하면 아래처럼 소수점과 출력 숫자의 개수를 같이 조절할 수 있다.여기서 0으로 12개를 꽉

velog.io

 

 

[python] 파이썬 format 함수 (문자열 포매팅 방법 1)

안녕하세요. BlockDMask 입니다. 파이썬에서 문자열 포매팅 방법은 %와 서식기호를 이용한 방법, format 함수를 이용한 방법, f-string을 이용한 방법 이렇게 세가지가 있다고 볼 수 있습니다. 오늘은 파

blockdmask.tistory.com

 

 

파이썬 코딩 도장: 24.2 문자열 서식 지정자와 포매팅 사용하기

파이썬은 다양한 방법으로 문자열을 만들 수 있습니다. 그중에서 서식 지정자(format specifier)로 문자열을 만드는 방법과 format 메서드로 문자열을 만드는 문자열 포매팅(string formatting)에 대해 알아

dojang.io