自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用于解答算法題目的Python3代碼框架

開發(fā) 架構 算法
最近在實習,任務并不是很重,就利用閑暇時間使用Python3在PAT網(wǎng)站上刷題,并致力于使用Python3的特性和函數(shù)式編程的理念,編寫了一個用于處理這些輸入輸出的代碼框架,并加入了測試功能(寫函數(shù)前先寫測試時正確的事情)。

前言

最近在實習,任務并不是很重,就利用閑暇時間使用Python3在PAT網(wǎng)站上刷題,并致力于使用Python3的特性和函數(shù)式編程的理念,其中大部分題目都有著類似的輸入輸出格式,例如一行讀入若干個數(shù)字,字符串,每行輸出多少個字符串等等,所以產(chǎn)生了很多重復的代碼。

Python代碼

于是我就利用VS Code的代碼片段功能編寫了一個用于處理這些輸入輸出的代碼框架,并加入了測試功能(寫函數(shù)前先寫測試時正確的事情)。代碼如下:

  1. """Simple Console Program With Data Input And Output.""" 
  2. import sys 
  3. import io 
  4.  
  5.  
  6. def read_int(): 
  7.     """Read a seris of numbers.""" 
  8.     return list(map(int, sys.stdin.readline().split())) 
  9.  
  10.  
  11. def test_read_int(): 
  12.     """Test the read_int function""" 
  13.     test_file = io.StringIO("1 2 3\n"
  14.     sys.stdin = test_file 
  15.     assert read_int() == [1, 2, 3], "read_int error" 
  16.  
  17.  
  18. def read_float(): 
  19.     """Read a seris of float numbers.""" 
  20.     return list(map(float, sys.stdin.readline().split())) 
  21.  
  22.  
  23. def test_read_float(): 
  24.     """Test the read_float function""" 
  25.     test_file = io.StringIO("1 2 3\n"
  26.     sys.stdin = test_file 
  27.     assert read_float() == [1.0, 2.0, 3.0], "read_float error" 
  28.  
  29.  
  30. def read_word(): 
  31.     """Read a seris of string.""" 
  32.     return list(map(str, sys.stdin.readline().split())) 
  33.  
  34.  
  35. def test_read_word(): 
  36.     """Test the read_word function""" 
  37.     test_file = io.StringIO("1 2 3\n"
  38.     sys.stdin = test_file 
  39.     assert read_word() == ["1""2""3"], "read_word error" 
  40.  
  41.  
  42. def combine_with(seq, sep=' ', num=None): 
  43.     """Combine list enum with a character and return the string object""" 
  44.     res = sep.join(list(map(str, seq))) 
  45.     if num is not None: 
  46.         res = str(seq[0]) 
  47.         for element in range(1, len(seq)): 
  48.             res += sep + \ 
  49.                 str(seq[element]) if element % num != 0 else '\n' + \ 
  50.                 str(seq[element]) 
  51.     return res 
  52.  
  53.  
  54. def test_combile_with(): 
  55.     """Test the combile_with function.""" 
  56.     assert combine_with([1, 2, 3, 4, 5], '*', 2) == """1*2 3*4 5""""combine_with error." 
  57.  
  58.  
  59. def main(): 
  60.     """The main function.""" 
  61.     pass 
  62.  
  63.  
  64. if __name__ == '__main__'
  65.     sys.exit(int(main() or 0)) 

VS Code代碼片段

添加到VS Code的默認代碼片段的操作大致如下:

文件->***項->用戶代碼片段,選擇Python

 

編輯"python.json"文件如以下內(nèi)容:

  1. /* 
  2.    // Place your snippets for Python here. Each snippet is defined under a snippet name and has a prefix, body and  
  3.    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
  4.    // $1, $2 for tab stops, ${id} and ${id:label} and ${1:label} for variables. Variables with the same id are connected. 
  5.    // Example: 
  6.    "Print to console": { 
  7.       "prefix""log"
  8.       "body": [ 
  9.           "console.log('$1');"
  10.           "$2" 
  11.       ], 
  12.       "description""Log output to console" 
  13.   } 
  14. */ 
  15. "Simple Console Program With Data Input And Output": { 
  16.       "prefix""simple"
  17.       "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" 
  18.       ], 
  19.       "description""Simple Console Program With Data Input And Output" 
  20.   } 

總結 

雖然Python不是特別適合解答算法題目這種性能要求很高的場景,但是在一些模擬題目如各種排隊型和字符串處理的條件下,使用Python可以極大地提高解體效率,另外還可以使用cimport使用C語言的數(shù)據(jù)結構和Python的語法特性,效率不弱于原生C代碼。

責任編輯:武曉燕 來源: 博客園
相關推薦

2015-07-21 15:35:47

代碼總結源碼

2012-09-28 09:37:10

CSSJavaScriptJS

2011-06-09 09:18:11

iPadiOS蘋果

2015-09-10 08:45:39

CSS3生成器

2009-03-04 09:52:35

代碼契約組件接口

2009-12-18 10:24:28

VS 2010代碼

2009-12-14 16:04:23

MyEclipse 6

2021-12-30 11:30:13

人工智能機器學習技術

2009-12-15 13:39:43

2009-12-15 17:29:59

VS 2008代碼

2009-12-14 10:42:11

VS 2008代碼

2011-05-19 16:30:38

軟件測試

2018-09-13 10:20:49

編程語言PythonPython庫

2018-09-13 21:38:15

Python語言

2018-11-22 14:51:09

Python 開發(fā)編程語言

2020-06-05 14:48:11

零代碼低代碼開發(fā)

2022-06-30 07:48:06

Dooring低代碼零代碼

2015-09-10 08:48:39

CSS3v代碼生成器

2021-11-29 17:29:14

帆軟智數(shù)大會

2021-11-04 08:06:47

代碼編排平臺
點贊
收藏

51CTO技術棧公眾號