Prepare > Python > Sets > Introduction to Sets

2023. 7. 17. 22:19HackerRank-Python

 

[python] 파이썬 set (집합) 자료형 정리 및 예제

안녕하세요. BlockDMask 입니다. 오늘은 파이썬에서 집합 자료형인 set 자료형에 대해서 이야기 해보려 합니다. 집합 자료형은 다른 자료형의 중복 제거할때 사용을 하기도 하는데요. 자세한것은 예

blockdmask.tistory.com

 

문제


Now, let's use our knowledge of sets and help Mickey.

Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.

 

=> Ms. Gabriel Williams는 District College의 식물학 교수입니다. 어느 날 그녀는 학생인 미키에게 그녀의 온실에 있는 높이가 다른 모든 식물의 평균을 계산해 보라고 했습니다. 미키를 도와주세요.

 

Input Format

The first line contains the integer,N , the size of arr.
The second line contains the N space-separated integers, arr[i].

Returns

float: the resulting float value rounded to 3 places after the decimal

 

 

 

 

코드


def average(array):
    arr = set(array)

    return sum(arr) / len(arr)

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)

 

 

 

 

노트


set 자료형

  • 집합 자료형
  • 순서 무작위
  • 고유한 값들, 중복 불가
  • 추가, 수정, 삭제 가능
s1 = set(1, 2, 3)
s2 = set([1, 2, 3])
s3 = {1, 2, 3}

# 모두 동일한 set 생성

 

 

 

 

참조


 

[python] 파이썬 set (집합) 자료형 정리 및 예제

안녕하세요. BlockDMask 입니다. 오늘은 파이썬에서 집합 자료형인 set 자료형에 대해서 이야기 해보려 합니다. 집합 자료형은 다른 자료형의 중복 제거할때 사용을 하기도 하는데요. 자세한것은 예

blockdmask.tistory.com