In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about the content of the Python3 code framework used to solve algorithm problems. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Python code
So I used the code snippet feature of VS Code to write a code framework to handle these inputs and outputs, and added the test function (the right thing to write a test before writing a function). The code is as follows:
"Simple Console Program With Data Input And Output." Import sys import io def 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 res def 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." Pass if _ _ name__ = ='_ main__': sys.exit (int (main () or 0))
VS Code code snippet
The default code snippet added to VS Code is roughly as follows:
File-> * item-> 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\ 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 (map (str) Seq)\ n if num is not None:\ nres = str (seq [0])\ n for element in range (1 Len (seq):\ n res + = sep + str if element% num! = 0 else'\ n'+ str)\ 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"}} Thank you for reading! This is the end of this article on "what are the Python3 code frameworks for solving algorithm problems?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.