인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

swldpf1238's profile image
swldpf1238

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

2. Kth number

자동채점문의드립니다

Written on

·

329

0

안녕하세요,

문제를 보고 혼자서 아래와 같이 코드를 작성해서 VSCODE에서 돌리니까 답은 나오는데..

자동채점기로 돌리면 시간 초과가 뜨는거 같습니다. 혹시 이유를 알 수 있을까요?

(Judge(Python-5)로 진행해도 동일하게 TIME LIMIT 발생합니다)

import sys
#sys.stdin = open('2. K번째 수\in5.txt', 'rt')
data = sys.stdin.readlines()
t = int(data[0].strip())
for i in range(t): 
    n, s, e, k = map(int, data[i*2+1].strip().split(' '))
    num_lst = list(map(int, data[i*2+2].strip().split(' ')))
    num_lst = num_lst[s-1:e]
    num_lst.sort()
    print(f'#{i+1} {num_lst[k-1]}')
코테 준비 같이 해요! python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

채점기는 공식홈페이지에서 다운받은 파이썬 정식버젼이 채점을 합니다. 

정식버젼 IDLE로 위 코드를 실행해보면 콘솔입력을 받지 못하고 있습니다. 

readlines() 를 쓰지 말고 아래 코드처럼 readline를 써서 한 줄씩 읽어 드리면 어떨까요?

import sys
#sys.stdin = open('in1.txt', 'rt')
input=sys.stdin.readline
T=int(input())
for t in range(T):
    n, s, e, k=map(int, input().split())
    a=list(map(int, input().split()))
    a=a[s-1:e]
    a.sort()
    print("#%d %d" %(t+1, a[k-1]))
swldpf1238's profile image
swldpf1238

asked

Ask a question