Python3 code framework used to answer algorithm questions, algorithm python3
Preface
Recently, I am working as an intern, and I am not very busy with this task. I just used Python3 in my spare time to refresh my questions on PAT. I am committed to using Python3's features and functional programming concepts, most of the questions have similar input and output formats. For example, a row reads several numbers, strings, and how many strings are output in each row, so a lot of repeated code is generated.
Python code
So I used the Code snippet function of VS Code to write a Code framework for processing these input and output, and added the test function (the correct thing to write before writing the function ). The Code is as follows:
"""Simple Console Program With Data Input And Output."""import sysimport iodef read_int(): """Read a seris of numbers.""" return list(map(int, sys.stdin.readline().split()))def test_read_int(): """Test the read_int function""" test_file = io.StringIO("1 2 3\n") sys.stdin = test_file assert read_int() == [1, 2, 3], "read_int error"def read_float(): """Read a seris of float numbers.""" return list(map(float, sys.stdin.readline().split()))def test_read_float(): """Test the read_float function""" test_file = io.StringIO("1 2 3\n") sys.stdin = test_file assert read_float() == [1.0, 2.0, 3.0], "read_float error"def read_word(): """Read a seris of string.""" return list(map(str, sys.stdin.readline().split()))def test_read_word(): """Test the read_word function""" test_file = io.StringIO("1 2 3\n") sys.stdin = test_file assert read_word() == ["1", "2", "3"], "read_word error"def combine_with(seq, sep=' ', num=None): """Combine list enum with a character and return the string object""" res = sep.join(list(map(str, seq))) if num is not None: res = str(seq[0]) for element in range(1, len(seq)): res += sep + \ str(seq[element]) if element % num != 0 else '\n' + \ str(seq[element]) return resdef test_combile_with(): """Test the combile_with function.""" assert combine_with([1, 2, 3, 4, 5], '*', 2) == """1*2 3*4 5""", "combine_with error."def main(): """The main function.""" passif __name__ == '__main__': sys.exit(int(main() or 0))
VS Code snippet
The default Code snippet added to VS Code is roughly as follows:
- File-> preferences-> User code snippet, select Python
- Edit the "python. json" file as follows:
{/* // Place your snippets for Python here. Each snippet is defined under a snippet name and has a prefix, body and // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: // $1, $2 for tab stops, ${id} and ${id:label} and ${1:label} for variables. Variables with the same id are connected. // Example: "Print to console": { "prefix": "log", "body": [ "console.log('$1');", "$2" ], "description": "Log output to console" }*/"Simple Console Program With Data Input And Output": { "prefix": "simple", "body": ["\"\"\"Simple Console Program With Data Input And Output.\"\"\"\nimport sys\n\ndef read_int():\n \"\"\"Read a seris of numbers.\"\"\"\n return list(map(int, sys.stdin.readline().split()))\n\n\ndef read_float():\n \"\"\"Read a seris of float numbers.\"\"\"\n return list(map(float, sys.stdin.readline().split()))\n\n\ndef read_word():\n \"\"\"Read a seris of string.\"\"\"\n return list(map(str, sys.stdin.readline().split()))\n\n\ndef combine_with(seq, sep=' ', num=None):\n \"\"\"Combine list enum with a character and return the string object\"\"\"\n res = sep.join(list(map(str, seq)))\n if num is not None:\n res = str(seq[0])\n for element in range(1, len(seq)):\n res += sep + str(seq[element]) if element % num != 0 else '\\n' + str(seq[element])\n return res\n\n\ndef main():\n \"\"\"The main function.\"\"\"\n pass\n\n\nif __name__ == '__main__':\n sys.exit(int(main() or 0))\n" ], "description": "Simple Console Program With Data Input And Output" }}
Then, when writing the Python code, type "simple" to automatically enter the above template.
Summary
Although Python is not particularly suitable for scenarios with high performance requirements for solving algorithm questions, it can greatly improve the efficiency of dismounting by simulating questions such as queuing and string processing, in addition, cimport can be used to use the data structure of C language and Python syntax features, which is less efficient than native C code.