Search code examples
pythonstringinput

I can't reproduce input embedded in a contest interface


I solved a contest task but couldn't submit it because I can't reproduce input embedded in the contest interface. Input is defined in 4 strings, each can be empty but has at its end a symbol for line break (I guess "\n"). If not empty the strings consist of 3-lettered weekdays with 1 space between:

MON\n
MON WED\n
\n
FRI THU SUN\n

I've tried:

text = []
while True:
    line = input()
    if line:
        text.append(line)
    else:
        break
text = [line.rstrip() for line in text]

But it requires to enter the input a 5th time to make the break-condition work, failing the solution. I'm not sure whether the compiler considers "\n" -only strings as not empty. sys.stdin.readlines() requires CTRL-D after input, so cannot be used. I need the input of 4 strings with "\n" in the end (no matter if they are empty or full), to be put into a list (ideally a list of lists):

[['WED', 'FRI'], ['MON', 'TUE'], [], ['MON', 'WED', 'THU', 'SAT', 'FRI', 'SUN', 'TUE']]  # ideally
['WED FRI', 'MON TUE', '', 'MON WED THU SAT FRI SUN TUE']  # also okay

Also, I checked the input from the contest system in PyCharm's debug console. Either way I entered, it didn't clear the "\n" sign from strings. How to make the debugger understand a "\n"-sign?

MON\n
MON"\n"
MON'\n'
"MON\n"  # neither of them cleared the "\n" from the string.

Solution

  • If I got it right you want to read input from console and store it in a list of lists in this case since there is always 4 lines of input you could actually do it like this

    text = []
    for _ in range(4):
        line = [weekday for weekday in input().split()]
        text.append(line)
    
    print(text)
    

    so for example if input in console is

    Wed Thu Fri
    
    Sat Sun Mon Tue
    Tue Sun
    

    then the value of text which gets printed is [['Wed', 'Thu', 'Fri'], [], ['Sat', 'Sun', 'Mon', 'Tue'], ['Tue', 'Sun']] And if for any reason the number of lines can be actually greater than 4 then in this case it will be necessary to provide the number of lines and so code becomes like this

    text = []
    
    num_lines = int(input())
    for _ in range(num_lines):
        line = [weekday for weekday in input().split()]
        text.append(line)
    
    print(text)
    

    I hope this is what you were looking for.