Python/Python Challenge
[Python Challenge]파이썬 챌린지 Level 3 풀이
Mr.robot 2
2022. 7. 19. 15:09
처음에 "One small letter, surrounded by EXACTLY three big bodyguards on each of its sides." 이것이 무슨의미인지 몰라서 계속 해맸다.
그래서 AAAcDDD 이런 글자를 도출하는 코드를 작성하였는데 값이 너무 많이나와서 생각해보니 주변에 3개의 대문자라는 것은 4개의 대문자가 오면 안되는 것이였다.
그래서 양쪽에 aDDDaCCCa 이런식으로 와야하는 것이다.
이를 if 문으로 작성하긴 했는데 너무 비효율적이다. 나머지 하나는 친구가 작성한것데 이게 훨 나은거 같기도.
더보기
import re
data = open('pc.txt', 'r').read()
a = False
b = 0
c = 0
x = ""
find = ""
for i in data:
# 소문자일 때 들어가는 if문
if(a == False and c != 3):
if(i.islower() == True):
a = True
c += 1 # 소문자를 새는 곳
x += i
if(c == 3):
find += x
find += ","
x = ""
a = False
c = 0
elif(i.islower() == False):
a = False
b = 0
c = 0
x = ""
elif(a == True and i.islower() == True):
if(c == 1 or c == 2 or c == 3):
a = True
b = 0
c = 1
x = i
# 대문자일 때 들어가는 if 문
elif(a == True and b != 3):
if(i.isupper() == True):
b += 1
x += i
if(b == 3 and c == 1):
a = False
b = 0
elif(b == 3 and c == 2):
a = False
b = 0
else: # 영문을 제외한 특수문자 및 수자를 처리해야할 곳
a = False
b = 0
c = 0
x = ""
result = ""
find_list = find.split(',')
find_list = list(filter(None, find_list))
for i in find_list:
result += i[4]
print(result)
이건 친구가 만든 코드에 내가 수정을 하여 더 보기 쉽게 해놓았다.
더보기
chall_data = open("pc.txt", 'r', encoding='UTF-8')
find_data = chall_data.read()
new_string = find_data.replace("\n", "")
check_data = []
data = []
for i in range(0, len(new_string)-9):
if new_string[i].isupper() == True:
if new_string[i+1].isupper() == True:
if new_string[i+2].isupper() == True:
if new_string[i+4].isupper() == True:
if new_string[i+5].isupper() == True:
if new_string[i+6].isupper() == True:
if new_string[i+3].isupper() == False and new_string[i-1].isupper() == False and new_string[i+7].isupper() == False:
check_data.append(new_string[i-1:i+8])
data.append(new_string[i+3])
chall_data.close()
result = ""
for i in data:
result += i
print(check_data)
print(result)
다음 Level 4 링크 http://www.pythonchallenge.com/pc/def/linkedlist.php