Python3 code framework for solving algorithmic problems

Source: Internet
Author: User
Tags assert

Objective

Recently in the internship, the task is not very heavy, the use of leisure time using Python3 on the PAT website, and dedicated to the use of PYTHON3 features and functional programming concept, most of the topics have similar input and output format, such as a line to read several numbers, strings, How many strings are output per line, etc., so there is a lot of duplicated code.

Python code

I then used the code snippet feature of VS code to write a code framework for working with these input and output, and added the test function (the right thing to write before writing the function). The code is as follows

"" "Simple Console program with Data Input and Output." "" "Import SysImport IODefRead_int():"" Read a Seris of numbers. "" "Return list (map (int, Sys.stdin.readline (). Split ()))DefTest_read_int():"" "Test the Read_int Function" "" Test_file = io. Stringio ("1 2 3\n") Sys.stdin = Test_fileAssert read_int () = = [1,2,3],"Read_int Error"DefRead_float():"" Read a seris of float numbers. "" "Return list (map (float, Sys.stdin.readline (). Split ()))DefTest_read_float():"" "Test the Read_float Function" "" Test_file = io. Stringio ("1 2 3\n") Sys.stdin = Test_fileAssert read_float () = = [1.0,2.0,3.0],"Read_float Error"DefRead_word():"" Read a Seris of string. "" "Return list (map (str, Sys.stdin.readline (). Split ()))DefTest_read_word():"" "Test the Read_word Function" "" Test_file = io. Stringio ("1 2 3\n") Sys.stdin = Test_fileAssert Read_word () = = ["1","2","3"],"Read_word Error"DefCombine_with(Seq, sep=", Num=none):"" "Combine list enum with a character and return the string Object" "" res = Sep.join (list (map (str, seq))If numIsNotNone:res = str (seq[0])for elementIn range (1, Len (seq)): Res + = Sep + str (seq[element])If element% num! =0Else' \ n ' + str (seq[element])return resDefTest_combile_with(): "" " Test the Combile_with function." " assert Combine_with ([1, 2, 3, 4, 5], ' * ', 2) = = "" " 1*2 3*4 5" "", "Combine_with E Rror. " 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 as follows:

    • User code Snippets, preferences, file, select Python

  • Edit the "Python.json" file as follows
    {/*   //Place your snippetsForPython here.Each snippetis defined under a snippet nameand has a prefix, bodyand//description.The prefixIS-whatis used to trigger the snippetAnd the body would be expandedand inserted.Possible variables is://$1, $2For tab stops, ${id}and ${id:label}and ${1:label}For variables.VariablesWith the same ID is connected. //Example:"Print to Console": {"prefix":"Log","Body": ["Console.log (' $ ');","$"],"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 num Bers.\ "\" \ "\ 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 you write the Python code, type "simple" to automatically enter the above template.

Summarize

While Python is not particularly well suited for solving algorithmic problems such as high-performance scenarios, using Python can greatly improve the efficiency of disassembly in the context of a number of simulation topics such as queueing and string processing. You can also use Cimport to use C-language data structures and Python syntax features, which are not as efficient as native C code.

Python3 code framework for solving algorithmic problems

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.