Python online written test trivial

Source: Internet
Author: User
Tags stdin

This blog records some of the things that you need to be aware of when writing algorithmic questions in Python with the online test paper.

Processing input

The online writing of some large companies seems to have contracted to a third party, and the online code function under that platform is no longer a residue. It is not given function declarations and function headers like Leetcode and other OJ, but requires you to handle the input yourself.

First of all, the input under Python is basically a string, and in single-line input mode, we can use the Raw_input () function to accept processing in the case of a mixed number or character.

# 输入:{1, 2, 3},23s = raw_input()
    • 1
    • 2
    • 3

At this point, the S-save is the entire input, then you need to manually get the data inside. The commonly used manipulation of strings (for example split , int , float ) is done with an array of slices.

For single-line input is a pure number, you can use the input () function to handle. The relationship between input and Raw_input is mentioned later.

# 输入:23.45s = input()type(s)  # float
    • 1
    • 2
    • 3
    • 4

What if it's multi-line input? For example, the topic requires multiple sets of simultaneous input, at this time we can not write a loop to use the Raw_input function bar. You can then use the Sys.stdin that handles the input. Like what:

# 输入:12\n43\n...import sysfor line in sys.stdin:  # 使用for处理多个输入    print(line)
    • 1
    • 2
    • 3
    • 4
    • 5

At this point, line is a row of input saved in the form of a string. String manipulation and slicing are still required for mixed situations. But for a lot of situations, we can use the Eval function to handle it. The meaning of eval (str) is to evaluate the string str as a valid expression and return the result of the calculation. Like what:

# 例1,输入:{1, 2, 3, 4}s = raw_input()  # s = ‘{1, 2, 3, 4}‘d = eval(s)  # d = {1, 2, 3, 4}type(d) # set类型# 例2,输入:0xAs = raw_input()  # s = ‘0xA‘n = eval(s)  # n = 10hn = hex(n)  # hn = ‘0xA‘
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

With eval, the input string can be easily converted to the corresponding Python type. In addition, since the input function is actually called raw_input after the eval function is processed, so, for the above two examples, can also be written as:

# 例1,输入:{1, 2, 3, 4}d = input()  # s = {1, 2, 3, 4}type(d) # set类型# 例2,输入:0xAn = input()  # n = 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
Input & Raw_input

The relationship between the two is as follows:

def input(prompt):    return eval(raw_input(prompt))
    • 1
    • 2
    • 3

That is, Raw_input accepts only the string that is returned, and input accepts the string value after the type conversion of Eval, and, of course, an error if the input cannot be processed with the Eval function.

Sys.stdin & input & Raw_input

Sys.stdin and the next two are all getting input, but the latter two are generally only used when processing single-line input. The following two are equivalent:

import sysline = sys.stdin.readline()[:-1]  # sys.stdin会接受最后的`\n`,而raw_input不会
    • 1
    • 2
    • 3

Equivalent to:

line = raw_input()
    • 1
    • 2

With the sys.stdin.readlines() ability to get multiple lines of input, you need CTRL + Z to stop the input under Windows.

Supplemental examples

The topic comes from the a+b problem on Hihocoder.
Describe
For two integers a+b and

Input
The input contains multiple sets of data.
Each set of data contains two integers a (1≤a≤100) and B (1≤b≤100).

Output
For each set of data output a+b and.

Code:

while True:    try:        (n, m) = (int(x) for x in raw_input().split())        print(m + n)    except EOFError:        break

Python online written test trivial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.