Problem 19 Matching Brackets how do I split input in the test cases when using Python?

Back to General discussions forum

Chrisislearning     2025-01-03 20:21:10

Python doesn't accept multiline input and the input that we're given contain test cases that are only separated by new line. When copied into a Python program, all those separate lines are changed into a single long line of different symbols. We can't separate them with spaces (for example with help of input() function) as there are also spaces in test cases themself. How do I copy and paste this input without altering it to have, for example, "\n" at the end of each line?

gardengnome     2025-01-03 22:01:41
User avatar

I think there are at least three options:

  1. You copy & paste the input into a file and redirect standard input to use this file.
  2. You copy & paste the input into a file and read the data from the file in your program.
  3. You copy & paste the input and surround it by tripple " in Python, e.g. data = """example""".
Chrisislearning     2025-01-03 22:39:34

Thx. I quickly checked the option nr 3 and it worked, however I am a bit sad that for this solution I need to manualy paste the input field into my code (so the code can't accept data from another program for example). I guess in real life we wouldn't have this problem.

gardengnome     2025-01-03 22:46:35
User avatar

Do you use Python from the command line or do you use an IDE just as PyCharm?

To use stdin, you can do something like this:

n = int(input())
for i in range(n):
    s = input()
    ...
Chrisislearning     2025-01-03 22:50:00

I was using Sublime but this website expects you to use input(). Sublime doesn't have that functionality from the get go and I couldn't be bothered with watching 90 minutes tutorial on how to mod it so I came back to Jupyter notebook.

gardengnome     2025-01-03 22:56:34
User avatar

You do not have to use CodeAbbey to run your code. It's perfectly fine to run it locally as you see fit, and then copy & paste your result plus code over here.

Rodion (admin)     2025-01-04 09:50:18
User avatar

Thanks to Mathias for quick help,

just I dare to add my two cents:

When copied into a Python program,

It is something which is strongly discouraged, normally computer programs shouldn't work with input data embedded into their source, right? :)

Sublime doesn't have that functionality from the get go

I'm not sure what you mean. Sublime is just a text editor as I remember, it is not Python. You normally have some Python interpreter on your machine which you can run separately (in the command line), or probably use IDLE environment, if it is installed along with Python - or use some text editor (Sublime, VIM, Notepad++ etc). In the last case you generally can edit your code in the editor but you should run it similarly to the 1st option, in the separate console window or console built-in in the editor (if it is present). One of the most advanced editors of this kind is PyCharm, but I don't remember well, it's probably not free.

Concluding, it is very important to learn how to run Python at all (rather than clicking some magic button in your editor). Regretfully this small site has no special exercise to help you learning "environment and infrastructure" but it is nevertheless very important.

gardengnome     2025-01-04 11:21:43
User avatar

PyCharm has a free Community Edition which is more than sufficient. Suche IDEs handle calling python with the right parameters for you, but as Rodion wrote it's good to know the basics, i.e. something like python3 ./python_program.py < ./data.txt. In this example, we call python3 to execute the program python_program.py, and the contents of the file data.txt is presented as stdin to the program.

Please login and solve 5 problems to be able to post at forum