링크 : https://www.hackerrank.com/contests/pythonist3/challenges/iterables-and-iterators/problem
Constraints
All the letters in the list are lowercase English letters.
Sample Input
4
a a c d
2
Sample Output
0.8333
Explanation
All possible unordered tuples of length comprising of indices from to are:
Out of these combinations, of them contain either index or index which are the indices that contain the letter 'a'.
Hence, the answer is (5/6 = 0.8333).
Solutions
from itertools import combinations
def solve(n, char, k):
char = sorted(char)
cnt = char.count('a')
data = [i for i in range(len(char))]
results = list(combinations(data, k))
total = 0
for result in results:
if result[0] < cnt:
total += 1
exe = total / len(results)
return round(exe, 3)
if __name__ == '__main__':
n = int(input())
char = list(map(str, input().split(' ')))
k = int(input())
print(solve(n, char, k))
'a'의 개수를 count한 cnt값을 저장하고
조합 결과는 (1,2),(2,3) 이런식으로 작은 수가 첫번째로 존재한다.
따라서 첫번째 값이 cnt보다 작으면 무조건 'a'를 포함한 조합의 경우이다.
조합의 경우의 수를 list로 저장하기
from itertools import combinations
list(combinations(data, k))
'Programming > Etc' 카테고리의 다른 글
소수 찾기 효율성 (0) | 2022.02.15 |
---|---|
Programmers / 문자열 압축 (0) | 2022.02.11 |
Hackerrank / Write a function (0) | 2022.01.18 |
Hackerrank / ginorts (0) | 2021.12.03 |
programmers / 로또 최고 최저 (0) | 2021.12.01 |