This article is the first post. I would like to write down what I was interested in, so I would appreciate your favor.
The paiza skill check is a programming skill test that can be taken at paiza, a site operated by paiza Co., Ltd., which aims to develop IT human resources and support job hunting. Users are ranked according to the difficulty of the problems they have cleared in this skill check, and may be scouted by the company that saw the code.
** Example of questions Escape from the troublesome copy and paste work when debugging ** Most programming test questions, including paiza's skill checks, have input and output examples as examples, so you'll start with that example. In the case of paiza, you can try it online from the bottom of the problem screen and check the operation, but as the difficulty increases and the code becomes longer, you will want to debug it in small pieces with Jupyter Notebook etc. ( At least I will). In that case, you will have to copy and paste the input example, but it is ** troublesome ** to copy each line line by line, but ** I also want to write it in the same form as when I submitted it ** .. So I want to do something, let's do something. So I managed to do it.
To put it simply, I overloaded the ** ʻinput ()` function, or overwrote it **.
The following image is an input example of Introduction to paiza learning skill check. this The goal is to create a function that can be used simply by substituting it like this. ・ ・ ・
paiza_test.py
def preinput(input_text):
out=input_text.split("\n")
for i in out:
yield i
def input(reset=False):
global input_text,inp
if reset:del inp,input_text,repeat;return
try:return inp.__next__()
except:
if repeat:inp=preinput(input_text)
return inp.__next__()
def def_input(s,isRepeat=False):
global input_text,inp,repeat
input_text=s
repeat=isRepeat
inp=preinput(input_text)
** Done ** (Miscellaneous). Place this as a py file in the directory to be debugged and import it for use.
debug.ipynb
from paiza_test import*
As before, prepare a variable to which the input example is assigned and throw it into the def_input ()
function, and you are ready **.
debug.ipynb
input_text="""6
apple
book
information
note
pen
pineapple"""
def_input(input_text)
After that, you can write the code you want to debug in ** as-is **.
debug.ipynb
n=int(input())
for i in range(n):
print(input())
output
apple
book
information
note
pen
pineapple
By default, when the input reaches the end, an error will be thrown the next time it is called, but if the argument ʻisRepeat is specified as
Truewhen calling
def_input ()`, it will be output repeatedly from the beginning.
python
def_input(input_text,isRepeat=True)
n=int(input())
for i in range(3*n):
print(input())
output
apple
book
information
note
pen
pineapple
6
apple
book
information
note
pen
pineapple
6
apple
book
information
note
Also, if you call the argument reset
with True
, it will be reset to the state before calling def_input ()
(there is no return value).
def_input(input_text)
input(reset=True)
input() #------> error
3/16 postscript
Fixed the setting timing of ʻisRepeat when calling
def_input () `.
I hope this article has reached people with the same dissatisfaction and helped me ... Besides paiza, I think it can be used for debugging other programming contests such as AtCoder. If you have a better way or improvement, please leave a comment.
Recommended Posts