您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關用于解答算法題目的Python3代碼框架有哪些的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
Python代碼
于是我就利用VS Code的代碼片段功能編寫了一個用于處理這些輸入輸出的代碼框架,并加入了測試功能(寫函數前先寫測試時正確的事情)。代碼如下:
"""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代碼片段
添加到VS Code的默認代碼片段的操作大致如下:
文件->***項->用戶代碼片段,選擇Python
編輯"python.json"文件如以下內容:
{ /* // 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" } }
感謝各位的閱讀!關于“用于解答算法題目的Python3代碼框架有哪些”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。