10870
input을 사용한 코드
num = int(input())
temp = list(map(int,input().split()))
findNum = int(input())
count = 0
for i in range(num):
if temp[i] == findNum:
count +=1
print(count)
sys.stdin.readline()을 사용한 코드
import sys
num = int(sys.stdin.readline().strip())
temp = list(map(int,sys.stdin.readline().split()))
findNum = int(input())
count = 0
for i in range(num):
if temp[i] == findNum:
count +=1
print(count)
주의 사항: sys.stdin.readline()을 사용하면 str형으로 저장되므로 type casting을 꼭 해줘야 함.
10871
length, num = map(int, input().split())
temp = list(map(int,input().split()))
res = []
for i in range(length):
if temp[i] < num:
res.append(temp[i])
for i in range(len(res)):
print(res[i], end=" ")
작성 코드
-> 공간을 더 차지함.
n,x = map(int,input().split())
a = input().split()
print(type(a))
print(a)
for i in range(n):
if int(a[i])<x:
print(a[i],end=" ")
고친 코드
-> a = input().split() 이 한 줄로 입력받아, 공백을 제외하고 a 라는 list에 요소가 한 개씩 저장됨.
5597
temp = [i+1 for i in range(30)]
res = []
for i in range(28):
num = int(input())
res.append(num)
res.sort()
i = 0
j = 0
while j < 30:
if i < 28 and res[i] == temp[j]:
i += 1
j += 1
else:
print(temp[j])
j += 1
틀렸다고 나온 코드
temp = [i+1 for i in range(30)]
res =[]
for i in range(28):
num = int(input())
res.append(num)
res.sort()
i = 0
j = 0
while j < 30:
if i<28 and res[i] == temp[j]:
i+=1
#j+=1
else:
print(temp[j])
#j+=1
j+=1
바꾼 코드
-> 이건 맞았다고 함. 물론 좀 더 명확하긴 함.
근데 동작 상으로는 동일한 것 같은데 왜 그런건지.....
그리고 제일 간소화한 코드
num = list(range(1, 31))
for _ in range(28):
submitted = int(input())
num.remove(submitted)
print(num[0])
print(num[1])
공간 복잡도가 1/2 + 간소화 됨
-> 결국 remove되고 나면 남은 번호는 0과 1자리에 2개만 남게 됨.
print(*{*map(int,open(0))}^{*range(1,31)})
숏코딩은 이런 식으로 1줄 출력이 가능
'코딩 테스트 > Python' 카테고리의 다른 글
| 백준, Python) 심화 1단계 3003, 10988, 1157, 2941, 1316, 25206 (0) | 2025.04.16 |
|---|---|
| 백준, Python) 10809, 2675, 2908, 5622, 11718 (0) | 2025.04.09 |
| 백준, Python) 15552, 10951 (0) | 2025.04.07 |
| 백준, Python) 2단계 2525, 2480 (0) | 2025.04.04 |
| 백준, Python) 1단계 입출력과 사칙연산 (0) | 2025.04.02 |