User Tools

Site Tools


python3:input_output

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python3:input_output [2018/08/14 16:43]
jguerin
python3:input_output [2018/08/14 19:04] (current)
jguerin
Line 3: Line 3:
  
 ===== Input Basics ===== ===== Input Basics =====
-Input in Python3 is handled via the ''%%input()%%'' function, and reads a newline-terminated string from standard input.((Unlike C++ and Java, input is line-based rather than token-based.)) Additional processing is done to the resulting string.+Input in Python3 is handled via the ''%%input()%%'' function, and reads a newline-terminated string from standard input.((Unlike C++ and Java, input is line-based rather than token-based.)) Any additional processing is done to the resulting string.
  
 <code python> <code python>
Line 59: Line 59:
 ===== Advanced Input ===== ===== Advanced Input =====
 ''input()'' will scale well for many easy and mid-level contest problems. For mid to upper problems with significant bounds on reads and writes ''input()'' will carry an increased risk of //time limit exceeded// judgements.((There is no guaranteed cutoff, but for many problems ''input()'' is //increasingly likely// to fail around 10<sup>3</sup>≤//n//≤10<sup>4</sup> lines.)) ''input()'' will scale well for many easy and mid-level contest problems. For mid to upper problems with significant bounds on reads and writes ''input()'' will carry an increased risk of //time limit exceeded// judgements.((There is no guaranteed cutoff, but for many problems ''input()'' is //increasingly likely// to fail around 10<sup>3</sup>≤//n//≤10<sup>4</sup> lines.))
 +
 +=== stdin.readline() ===
 +''readline()'' is identical to the interface provided by ''input()'' except it //retains// the ''\n'' that is used to terminate the read.((This may require special handling in certain circumstance and can be safely ignored in others. E.g., ''.split()'' will //not// behave differently vs. ''input()''.))
  
 <code python> <code python>
Line 66: Line 69:
 </code> </code>
  
-<code python> 
->>> from sys import * 
->>> x = stdin.readline().split() 
-Hello World! # x=["Hello", "World!"] 
-</code> 
  
-''readlines()'' is stopped by an End Of File (EOF) character.  You can send this from the terminal with <ctrl-D>+=== stdin.readlines() === 
 +''readlines()'' processes an entire file, terminated by an //end of file// character.((This behavior can be simulated on the terminal with ''<ctrl>+d''.))
  
 <code python> <code python>
Line 80: Line 79:
 To Everyone! # x=["Hello\n", "To Everyone!\n"] To Everyone! # x=["Hello\n", "To Everyone!\n"]
 </code> </code>
 +
 +''<ctrl>+d'' would be used to terminate this example after the ''!''.
 +
 +''readlines()'' loads //and// stores an entire file into memory (as a list) its performance will exceed other options in Python3 for all but the most enormous files.((See the table below for an example where ''readlines()'' required storing ∼1GB of data.))
 +
  
 ==== Benchmarks ==== ==== Benchmarks ====
Line 98: Line 102:
 ))                 | ))                 |
  
 +The above tests were designed to showcase minimal reading functionality other than temporary storage.((We deliberately avoided additional processing such as typecasts, ''map()'', and ''split()'', as these are non-IO considerations in Python3.))
  
  
  
python3/input_output.1534283014.txt.gz · Last modified: 2018/08/14 16:43 by jguerin