1759번: 암호 만들기
첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.
www.acmicpc.net
<My code>
from itertools import combinations
l, c = map(int, input().split())
array = input().split()
array.sort()
case = combinations(array, l)
aeiou = ['a', 'e', 'i', 'o', 'u']
count = 0
for i in case:
for j in range(5):
if aeiou[j] in i:
count += 1
if count == 0:
count = 0
continue
if count >= l-1:
count = 0
continue
print(''.join(i))
count = 0
combinations를 이용해서 풀었다.
정답을 받긴 했지만 코드가 너무 지저분해서 아쉬움이 남는다.
<Reference code>
from itertools import combinations
vowels = ['a', 'e', 'i', 'o', 'u']
l, c = map(int, input().split())
array = input().split()
array.sort()
for password in combinations(array, l):
count = 0
for i in password:
if i in vowels:
count += 1
if count >= 1 and count <= l - 2:
print(''.join(password))
1. 조합을 변수에 삽입하는 과정 생략하고 바로 for 문에 삽입
2. count 변수 for 문 안에서 생성
조금 더 간결한 코드를 만들 수 있었다.
'PS > 백준' 카테고리의 다른 글
백준 17269 이름궁합 테스트 with Python (0) | 2021.02.09 |
---|---|
백준 10539 수빈이와 수열 with Python (0) | 2021.02.08 |
백준 1439 뒤집기 with Python (0) | 2021.02.06 |
백준 5585 거스름돈 with Python (0) | 2021.02.06 |
백준 2606 바이러스 with Python (0) | 2021.02.03 |