Python Input Methods for Competitive Programming

The sole drawback of the incredibly user-friendly Python language is its slowness. It is considerably slower than C, C++, and Java. platforms for online programming, if the C/C++ limit is X. Typically, Python provides 5X more time than Java does.

Languages provide a variety of input and output processes to speed up code execution for issues requiring a lot of input and output.

A case in point:

Consider a query asking for the sum of N user-inputted numbers.

Enter the value N.

N integers are input, separated in a line by a single space.

Input:

Output

10

Various Python Solutions to the Problem:

Normal Practice Python: (Python 2.7) (Python 2.7)

  1. A prompt argument is optional for raw input(). The string it produces is also devoid of the final newline character.
  2. The writing function of a particular object is called when print is used to format the inputs (place between the args and relatively more recent at the end).

Output

3
1 2 3
6

A little quicker technique utilising built-in stdin and stdout: 2.7 Python

  1. On the other hand, sys.stdin is a File Object. To receive input from the file, it works much like constructing any other file object. The file in this instance will be a typical input buffer.
  2. stdout.write('Dn') executes more quickly than print 'D'.
  3. Writing everything at once to stdout is even faster.

write("".join(list-comprehension)) however this depends on the input size in terms of memory utilisation.

Output

The time difference:
timing overview (100k lines each)
??????????-
Print: 6.04 s
Write time: 0.122 seconds
Print in 0.121 seconds using Stdout

As we have seen so far, it is always a good idea to take data from the normal system and give input to the standards system in order to increase the code's efficiency, which is always necessary in competitive programming. Would you wish to write these lengthy paragraphs each time you need them, though? What are the advantages of using Python, then.

Let us talk about how to fix this issue. What we can accomplish is make distinct functions that accept inputs of different types and call them as needed.

When you wish to accept a single line of input containing a specific number of numbers

Let us say the input has the following format.

and we desire different variables to refer to them. What we desire is

Thus, the following is how to write a method called get ints():

we no longer need to type this line repeatedly. In order to accept input in this form, you only need to call the get ints() function. The map function is utilised in the method get ints.

When we want to accept an input of an integer list that is provided on a single line

Let us say the input has the following format.

and we want the entire list of integers to be contained in a single variable. What we desire is

Consequently, we will do the following to construct a function called get_list():

You no longer need to type this line repeatedly. To accept input in this format, you only need to call the get ints () function.

When you wish to accept a string input

Let us say the input has the following format.

and we want this string to be stored in a single reference variable. What we desire is

Consequently, we will do the following to construct a function called get string():

You no longer need to type this line repeatedly. All that is necessary to accept input in this format is to call the get string () function.

A buffered pipe io is added: 2.7 Python

  1. To make the output faster, merely place the buffered IO code before you submit code.
  2. io. Bytes IO objects have the advantage of implementing a standard interface, also known as a "file-like" object. Every time read(n) is called on a Bytes IO object, the internal pointer advances.
  3. When registering methods to be called when a programme shuts down normally, the atexit module offers a straightforward interface. A hook called sys. exitfunc is likewise offered by the sys module, but only one function can be registered there. Using the atexit registry.

The standard method frequently runs out of time when dealing with big amounts of data. Maintaining a significant volume of I/O data is made easier by Method 2. The quickest method is 3. Typically, procedures 2 and 3 are helpful for processing input data files larger than 2 or 3 MBs.

For use in Python 3.X versions, take note that the codes shown above are in Python 2.7. Simply use the input () syntax from Python 3.X in favour of raw input (). Rest should be effective.

Reload(module):

Reload a module that was previously imported. Since the parameter is a module object, it must have already undergone a successful import. If we have altered the module data source using an outside editor and want to test the upgraded version while leaving the Python interpreter, this is helpful. The module object is the return value (the same as the module argument).

Reload(module) is called when:

  • To define a new set of objects that are tied to names in the module's dictionary, the code for Python modules is reverse engineered as well as the module-level code is again executed. Extension modules do not make a second call to the init function.
  • The old objects, like all other Python objects, are only reclaimed when their reference counts reach zero.
  • Any new or modified objects are referenced by updated identifiers in the module namespace.
  • To refer to a new object, further reference to the old artefacts (such as names outside of the module) must be modified for each namespace wherever they appear.

The dictionary of a module, which contains the module's global variables, is kept when the module is reloaded. This is typically not a problem because reinterpretations of names will take precedence over previous definitions. The previous definition is kept if the new edition of a module doesn't really define a name that's been defined by the previous version.






Latest Courses