본문 바로가기
HackerRank/Python

[Python] Basic Data Types (2)

by 넝구리 2022. 4. 14.
Finding the percentage

 

문제 출처 : https://www.hackerrank.com/challenges/finding-the-percentage/problem?isFullScreen=true

 

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

 

<Problem>

name:[marks]의 key/value 쌍을 포함하는 사전이 주어진다.

name은 학생의 이름, marks는 학생의 점수 리스트이다.

제공된 학생 이름에 대한 점수의 평균을 소수점 이하 2자리까지 표시해라.

 

첫 번째 줄에는 학생 수 n을 입력해라.

그 다음 n개의 줄에는 학생의 이름과 점수가 포함되며 각 값은 공백으로 구분해라.

마지막 줄에는 평균 점수를 구할 학생의 이름인 query_name이 포함된다.

 

<Solution>

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()

score_list=student_marks[query_name]

print("{:.2f}".format(sum(score_list)/len(score_list)))

# Asterisk(*)를 통해 가변인자를 사용했다.

# "{ : .숫자f}".format(실수) : 콜론(:) 앞은 format 인자의 번지를 입력하는 부분, 콜론(:) 뒤의 숫자에는 몇 번째 소수점자리까지 나타낼지 입력, ex) "{ : .2f}".format(1.4463) : 소수점 3번째 자리에서 반올림하여 소수점 2번째 자리까지 출력

 

 

Lists

 

문제 출처 : https://www.hackerrank.com/challenges/python-lists/problem?isFullScreen=true 

 

Lists | HackerRank

Perform different list operations.

www.hackerrank.com

 

<Problem>

list를 고려할 때, 다음의 명령을 수행할 수 있다.

1. insert i e : 정수 e를 i 위치에 삽입해라.

2. print : 리스트를 출력해라.

3. remove e : 첫 번째 항목의 정수 e를 삭제해라.

4. append e : 리스트의 끝에 정수 e를 삽입해라.

5. sort : 리스트를 정렬해라.

6. pop : 리스트의 마지막 원소를 제거해라.

7. reverse : 리스트를 뒤집어라.

 

첫 번째 줄에는 명령을 수행할 횟수 n을 입력해라.

그 다음 n개의 각 줄에는 위의 명령 중 하나의 명령을 포함해라.

 

각 명령을 순서대로 수행하고 리스트에서 작동하도록 해라.

print 명령이 수행될 때, 새로운 줄에 리스트가 출력되도록 해라.

 

리스트에 추가된 요소는 정수여야 한다.

 

<Solution>

list=[]

n=int(input())

for i in range(n):
    a=input().split()
    for i in range(1,len(a)):
        a[i]=int(a[i])
    
    if a[0]=="insert":
        list.insert(a[1],a[2])
    elif a[0]=="print":
        print(list)
    elif a[0]=="remove":
        list.remove(a[1])
    elif a[0]=="append":
        list.append(a[1])
    elif a[0]=="sort":
        list.sort()
    elif a[0]=="pop":
        list.pop()
    elif a[0]=="reverse":
        list.reverse()

 

 

Tuples

 

문제 출처 : https://www.hackerrank.com/challenges/python-tuples/problem?isFullScreen=true 

 

Tuples | HackerRank

Learn about tuples and compute hash(T).

www.hackerrank.com

 

<Problem>

n개의 정수를 입력하고 이를 포함하는 튜플 t를 만들어라.

그리고 hash(t)의 결과를 출력해라.

 

첫 번째 줄에는 튜플의 요소 수를 나타내는 정수 n을 입력해라.

두 번째 줄에는 n개의 정수를 공백으로 구분하여 입력해라.

 

<Solution>

n=int(input())
integer_list=map(int,input().split())

t=tuple(integer_list)
print(hash(t))

# hash() : key에 대해 산술 연산을 이용해 데이터 위치를 찾을 수 있는 함수

'HackerRank > Python' 카테고리의 다른 글

[Python] Strings (2)  (0) 2022.05.13
[Python] Strings (1)  (0) 2022.05.11
[Python] Basic Data Types (1)  (0) 2022.04.13
[Python] Introduction  (0) 2022.03.30