0

I'm writing a solution to a programming challenge and I was getting the answer wrong, but only for really long inputs. The issue doesn't appear to be my algorithm, but that the python input function cuts off characters after reaching a certain length (2^14 or just shy of 16,384 characters). I am copying and pasting the 26,000+ character input from a file to the console and the entire input appears when I paste it, but when I print it only the first 16,381 characters are read.

The challenge provides 2 inputs that are each over 26,000 characters in length. The input function only gets the first 16,000-ish before cutting off the rest. Doing a 2nd input() doesn't get the remaining characters but gets the next input instead.

In the competition, I have no control over how the data is provided - it's provided as console input so file reading won't work unless there's some way to make file reading read from the console stream.

How do I retrieve really long inputs without installing any additional python libraries? Is there something that would allow me to read the inputs as individual words similar to the java Scanner's next() function? I don't need to save the entire input, I only need to process the whole thing character for character and do some math with it, but I can't do that if I can only pull 60% of the input.

line = input()
print(len(line))

Providing the above code with a single 26,000+ character input (copy and pasted from somewhere), and the printed result is 16,381 characters long.

3
  • input() reads up to first new line (which it discards). For more low-level have a look at sys.stdin, which is used in the background for input() calls anyway. Also stackoverflow.com/q/1450393/4046632 Commented Mar 18, 2024 at 6:50
  • 1
    Does this answer your question? python truncating inputs from shell to specific number This is not a problem with Python its a problem with the shell you are using (I can reproduce your problem with Windows CMD). Try redirecting stdin to a file and you'll see that Python will then read the line properly. Commented Mar 18, 2024 at 7:42
  • Oh, it seems this actually is a Python issue and not a CMD issue. Commented Mar 18, 2024 at 7:52

2 Answers 2

1

Your data almost certainly includes one (or more) newline characters.

input() reads up to and excluding the first newline character.

You can read data from the standard input stream as follows:

import sys

data = sys.stdin.read()
print(len(data))

Consuming the input in this way is impervious to newline characters - i.e., it stops at EOF

Note:

This technique is not well-suited to interactive input

Sign up to request clarification or add additional context in comments.

Comments

0

To be clear, the 26,000 characters long input was a single input with 0 new lines (again, this was based on a competition challenge problem and the data in the problem set was huge).

I did, however, find out the issue actually had to do with the IDE I was using. I was using an executable (not installed) version of VSCode. When running the program in VSCode, the input() function was truncated @ a little over 16,000 characters. When running the same program in Idle, no such truncation occurred and I was able to get the correct answers on the remainder of my program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.