https://www.acmicpc.net/problem/1157
S = list(input().upper()) #대소문자 변환
Al = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
cntS = [0] * len(Al)
for i in range(len(S)) :
for j in Al :
if S[i] in j :
cntS[Al.index(j)] += 1
cntS_dup = list(cntS)
while 0 in cntS_dup :
cntS_dup.remove(0)
if len(set(S)) == len(set(cntS_dup)):
print(Al[cntS.index(max(cntS))])
else :
print("?")
예제는 다 실행됐으나 백준에선 틀렸다고 채점이 나옴..
그래서 반례를 찾을 때까지 보류ㅠㅠㅠㅠ
하 얼마나 잡고 있었는지.
💡 해결됨!!! 반례 찾음
Input:
ABCDEFGHIJKLMNOPQRSTUVWXYZA
Output:
?
Correct output:
A
# 수정된 코드
S = list(input().upper())
Al = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
cntS = [0] * len(Al)
for i in range(len(S)) :
for j in Al :
if S[i] in j :
cntS[Al.index(j)] += 1
cntS_dup = list(cntS)
if cntS_dup.count(max(cntS)) == 1 :
print(Al[cntS.index(max(cntS))])
else :
print("?")
count() 메소드 쓰면 cntS 복사해서 0 제거할 필요 없음
그래서 새로운 풀이. 훨씬 간단하다(보편적으로 이렇게 푸는 듯)
S = input().upper()
S_list = list(set(S))
cnt = list()
for i in S_list :
cnt.append(S.count(i))
if cnt.count(max(cnt)) > 1 :
print("?")
else :
print(S_list[cnt.index(max(cnt))])
리스트의 내용을 복사해서 새로운 변수에 넣고 싶을 때, 그냥 복사하면 안 되냐 생각할 수 있다.
list1 = [1, 2, 3, 4]
list2 = list1
print(list1)
>>> [1, 2, 3, 4]
print(list2)
>>> [1, 2, 3, 4]
위를 보면 잘 복사됐다고 생각할 수 있지만 아님. 주의해야 한다.
정확히 말하면 list2는 list1의 메모리 주소값을 복사한 것이다. 즉, 같은 메모리를 참조하기 때문에 list1이 수정되면 list2도 동시에 수정된다.
결국 복사가 이뤄지기 위해서는 list1와 list2이 서로 다른 메모리를 참조하도록 해주면 된다. 파이썬 리스트를 다른 리스트에 복사하는 방법은 4가지가 있다.
list1 = [1, 2, 3, 4]
list2 = list1[:]
list1 = [1, 2, 3, 4]
list2 = list(list1)
list1 = [1, 2, 3, 4]
list2 = list1.copy()
list1 = [1, 2, 3, 4]
list2 = [] + list1
리스트의 메소드.
li = [1, 3, 5, 7, 9]
li.remove(5)
print(li)
[1, 3, 7, 9]
s = "Hello World"
lowercase =s.lower()
print(lowercase)
uppercase = s.upper()
print(uppercase)
hello world
HELLO WORLD
문자열, 리스트, 튜플, 집합 등에서 사용 가능함
a = [1, 1, 1, 2, 3]
a.count(1)
['a', 'b', 'c', 'abc'].count('abc')
3
1