H-Index
문제 설명
H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다.
어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값이 이 과학자의 H-Index입니다.
어떤 과학자가 발표한 논문의 인용 횟수를 담은 배열 citations가 매개변수로 주어질 때, 이 과학자의 H-Index를 return 하도록 solution 함수를 작성해주세요.
제한사항
과학자가 발표한 논문의 수는 1편 이상 1,000편 이하입니다.
논문별 인용 횟수는 0회 이상 10,000회 이하입니다.
입출력 예
citations return
[3, 0, 6, 1, 5] 3
입출력 예 설명
이 과학자가 발표한 논문의 수는 5편이고, 그중 3편의 논문은 3회 이상 인용되었습니다. 그리고 나머지 2편의 논문은 3회 이하 인용되었기 때문에 이 과학자의 H-Index는 3입니다.
지역변수 공부하기.
def solution(citations):
citations.sort()
l = len(citations)
print(citations, l)
result = {}
for i in range(len(citations)):
count == 0
for j in range(i+1, len(citations)):
if j > i:
count += 1
result[count] = l - count
print(result)
Traceback (most recent call last):
File "/Users/sihwan/algorithm/pro1.py", line 15, in <module>
print(solution([3, 0, 6, 1, 5]))
File "/Users/sihwan/algorithm/pro1.py", line 7, in solution
count == 0
UnboundLocalError: local variable 'count' referenced before assignment
나중엔 결국 리스트를 sort시켜서 len(citations)-index 로 해서 처리했음.
2차
def solution(citations):
citations.sort()
l = len(citations)
print(citations, l)
result = {}
for i in range(len(citations)):
result[len(citations) - i] = i
print(result)
for key, value in result.items():
print(key, value)
[0, 1, 3, 5, 6] 5
{5: 0, 4: 1, 3: 2, 2: 3, 1: 4}
5 0
4 1
3 2
2 3
1 4
None
근데 가만보니 필요한게 요소값과 이상 값만 있으면 됌.
def solution(citations):
citations.sort()
l = len(citations)
result = {}
for i in range(len(citations)):
l = len(citations) - i # 이상 값
if l >= i:
result[citations[i] * l] = citations[i]
x = max(result.keys())
return result[x]
테스트 1 〉 실패 (0.18ms, 10.3MB)
테스트 2 〉 실패 (0.36ms, 10.3MB)
테스트 3 〉 실패 (0.34ms, 10.3MB)
테스트 4 〉 실패 (0.25ms, 9.95MB)
테스트 5 〉 실패 (0.33ms, 10.2MB)
테스트 6 〉 실패 (0.36ms, 10.2MB)
테스트 7 〉 실패 (0.14ms, 10.2MB)
테스트 8 〉 실패 (0.03ms, 10.1MB)
테스트 9 〉 실패 (0.04ms, 10.2MB)
테스트 10 〉 실패 (0.16ms, 10.3MB)
테스트 11 〉 실패 (0.38ms, 10.2MB)
테스트 12 〉 실패 (0.06ms, 9.98MB)
테스트 13 〉 실패 (0.38ms, 10.3MB)
테스트 14 〉 실패 (0.33ms, 10MB)
테스트 15 〉 실패 (0.36ms, 10.3MB)
테스트 16 〉 통과 (0.01ms, 10.2MB)
입출력 예제는 맞았는데 나머지가 다틀림.
입출력 표본이 적으니 설계가 힘들다.
어디서 이해를 잘못했는지 파악하는게 먼저 이다.
일단 return 되야하는건 이상 값 즉 h이다.
def solution(citations):
citations.sort()
l = len(citations)
print(citations, l)
for i in range(len(citations)):
x = l - i # 이상 값
y = x - i # 이하 값
if citations[i] >= x and x > y:
return x
테스트 1 〉 통과 (0.12ms, 10.1MB)
테스트 2 〉 통과 (0.11ms, 10.2MB)
테스트 3 〉 통과 (0.17ms, 9.98MB)
테스트 4 〉 통과 (0.15ms, 10.2MB)
테스트 5 〉 통과 (0.18ms, 10.3MB)
테스트 6 〉 통과 (0.21ms, 10.1MB)
테스트 7 〉 통과 (0.08ms, 10.1MB)
테스트 8 〉 통과 (0.02ms, 10.2MB)
테스트 9 〉 실패 (0.03ms, 10.1MB)
테스트 10 〉 통과 (0.09ms, 10.2MB)
테스트 11 〉 통과 (0.24ms, 10.2MB)
테스트 12 〉 통과 (0.04ms, 10.1MB)
테스트 13 〉 통과 (0.19ms, 10.1MB)
테스트 14 〉 통과 (0.19ms, 10.2MB)
테스트 15 〉 통과 (0.21ms, 10.2MB)
테스트 16 〉 실패 (0.01ms, 10.1MB)
def solution(citations):
citations.sort()
l = len(citations)
print(citations, l)
for i in range(l):
x = l - i # 이상 값
y = x - i # 이하 값
if citations[i] >= x and x > y:
return x
return 0
테스트 9 〉 실패 (0.03ms, 10.2MB)
실패한 테스트가 이제 1개로 좁혀짐
def solution(citations):
citations.sort()
l = len(citations)
for i in range(l):
x = l - i # 이상 값
y = x - i # 이하 값
if citations[i] >= x and x >= y:
return x
return 0
x > y 에서 x >= y 로 수정하면서 테스트케이스 전부 통과함.