Pythonã§ç«¶ããããããï¼ãå ¥éè ãç¥ã£ã¦ããã¹ãTipsã
ããã«ã¡ã¯ãFinatextã°ã«ã¼ãã®ãã¦ãã£ã¹ãã§ãã¼ã¿ã¨ã³ã¸ãã¢ããã¦ãããã³ã( Twitter: @Kevinrobot34, AtCoder: Kevinrobot34 )ã§ããå æ¥ãPyCon JP 2020ã§ããPythonã§ç«¶ããããããï¼ ãå ¥éè ãç¥ã£ã¦ããã¹ãé«éåTipsããã¨ããé¡åã§çºè¡¨ãããã¦ããã ãã¾ãããçºè¡¨æã®ã¹ã©ã¤ãã¨åç»ã¯ãã¡ãã§ãã
ãã®å 容ã«ã¤ãã¦ãå°ãå çãã¦ã¾ã¨ãããã¨æãã¾ãã
ã¯ããã«
æè¿AtCoderãä¸å¿ã«ã競æããã°ã©ãã³ã°ã®äººæ°ãé«ã¾ã£ã¦ãã¾ãã C++ã§åå ãã¦ãã人ãæãå¤ãã§ãããPythonã§åå ãã¦ãã人ãããªãå¢ãã¦ãã¾ãã Pythonã¯æ¸ããããä¸æ¹ã§C++ã¨æ¯ã¹ã¦ãã¾ãã¨å®è¡é度ãé ããLogicã¯æ£ããã¦ãPythonã ã¨TLE(Time Limited Exceeded, æéè¶ é)ãã¦ãã¾ããã¨ãå°ãªãããã¾ããã ãããå®éã«ã¯Pythonã§ãé«éåãå¯è½ã§ãã»ã¼å ¨ã¦ã®åé¡ãPython(/PyPy)ã§AC(Accepted, æ£è§£)ãããã¨ãã§ããã¨ç¥ããã¦ãã¾ãã
ãã®ããã°ã§ã¯ã競ããã¨Pythonã®åºæ¬çãªé¨åã¯ç¥ã£ã¦ãã人ãã¡ã¤ã³ã¿ã¼ã²ããã«ãå®éã«Pythonã§ç«¶æããã°ã©ãã³ã°ãããéã«ææ¡ãã¦ããã¹ãæ§ã ãªTipsãç´¹ä»ãã¾ãã
ç´¹ä»ãããã¨ã¯
- Pythonã§ç«¶ãããããä¸ã§é«éåã®ããã«æ°ãã¤ããæ¹ãè¯ãæ¸ãæ¹
- PyPyãç¨ããé«éå
- ããããã¨ã©ã¼ã®å¯¾å¦
ã§ãç´¹ä»ããªããã¨ã¯
- numpyãnumbaãç¨ããé«éå
ã«ãªãã¾ããnumpyãnumbaãç¨ããé«éåãå¼·åãªææ³ãªã®ã§ãæ°ã«ãªã人ã¯æ¯é調ã¹ã¦ã¿ã¦ãã ããã
é«éåé¢é£
æ¨æºå ¥å
inputã¨sys.stdin.readline
çµã¿è¾¼ã¿é¢æ°ã®input()
ã使ã£ã¦æ¨æºå
¥åãèªã¿è¾¼ãã®ã¯é
ãã§ãã
ãã¼ã¿æ°n
ãã¯ããã«ä¸ããããããããn
è¡ã«ããã£ã¦æ´æ°ãå
¥åãããå ´åãèãã¾ãããã
次ã®ããã« sys.stdin.readline
ã使ã£ãæ¹ãæ©ãã§ãã
ããããn=10**6
ã®å ´åã§é度ãè¨æ¸¬ããã¨æ¬¡ã®ããã«ãªãã¾ã
åºæ¬çãªãã¼ã¿æ§é ã®è¨ç®é
Listl
ãé·ã n
ã®Listã¨ãã¾ãã
- ã©ã³ãã ã¢ã¯ã»ã¹ã¯O(1) ï¼ã¨ããã¤ã¤éãã®ã§ãé¿ãããããªãé¿ããæ¹ãè¯ãã§ãï¼å¾è¿°ï¼ï¼
- appendãæ«å°¾ã®popã¯æ©ãã§ãããæ«å°¾ä»¥å¤ã¸ã®insertãpopã¯é ãã§ã
- å é ã¸ã®è¦ç´ ã®è¿½å ã»åé¤ããããå ´åã¯å¾è¿°ã®dequeã使ãã¾ããã
- inãminãmaxã¨ãã£ãæä½ã¯ç´æçã«çãæ¸ããã¨ãã§ã便å©ã§ãããO(n)ãªã®ã§æ³¨æãå¿ è¦ã§ã
- inãå¤ç¨ãããå ´åãªã©ã¯æ¬¡ç¯ã®set/dictã使ãã¾ããã
Setã¨Dicts
ãsetãd
ãdictã¨ãã¾ãã
- Setã¨Dictã¯ããã·ã¥ãã¼ãã«ã¨ãããã¼ã¿æ§é ã§ã
- C++ã®å¹³è¡¡äºåæ¢ç´¢æ¨ã®Setã¨ã¯å ¨ãå¥ç©ã§ãï¼ï¼
- Listã¨éãinãæ©ãã§ã
å°ã話ã¯ããã¾ãããcollections.defaultdictã¨ãããã®ãããã¾ããããã©ã«ãå¤ãè¨å®ã§ããdictã¿ãããªãã¤ã§ãããã使ãã¨å°ãã³ã¼ãã綺éºã«ãªããã¨ãããã¾ãã
deque
double-ended queueã®ãã¨ã§ãStackã¨Queue両è
ã®å½¹å²ãããªãããã¼ã¿æ§é ã§ãã詳細ã¯collections.dequeãè¦ã¦ãã ãããé
åã®å
é ã¨æ«å°¾ã®popãappendã¯æ©ãã§ãããçãä¸ã®è¦ç´ ã¸ã¢ã¯ã»ã¹ãããã¨ããã¨é
ãã§ãã
å
é ã¸ã®pop/appendãå¿
è¦ã§ããã°dequeãã©ã³ãã ã¢ã¯ã»ã¹ãé »ç¹ã«å¿
è¦ã§ããã°Listã使ããã¨ãèãã¾ãããã
Listã®å¹ççãªä½¿ãæ¹
indexã«ããã¢ã¯ã»ã¹ã¯é
ã
次ã®ããã«é·ãn
ã®List a
ãç¨æãã¦ãforæã§ã«ã¼ããã¦ã¢ã¯ã»ã¹ãããã¨ãèãã¾ãã
foræã®æ¸ãæ¹ã¨ãã¦ã¯ä»¥ä¸ã®3ã¤ãã±ã£ã¨èãããã¾ãã
ã¯ããã®a[i]
ã®ãããªindexã使ã£ãã¢ã¯ã»ã¹ã¯é
ããã¨ãç¥ããã¦ãã¾ããå®éä¸è¨3ã¤ã®å®è¡é度ãæ¯è¼ãã¦ã¿ã¾ãããã
ãã®ããã«indexã使ããã«foræãæ¸ãã¨ããªãæ©ããªãã¾ãã
indexãå¿
è¦ãªå ´åã«ã¯`enumerate`ã使ã£ãæ¸ãæ¹ãã¢ãªã§ãã
ã¾ããIndexã§ã¢ã¯ã»ã¹ãã¦å¤ãæ¸ãè¾¼ãã®ã¯æ´ã«é ãã®ã§æ³¨æãå¿ è¦ã§ããABC142 E: Get Everything (500ç¹) ã®ä»¥ä¸ã®ã³ã¼ããä¾ã¨ãã¦è¦ã¦ã¿ã¾ããããããããbit DPã®åé¡ã§ãã
ã¢ã«ã´ãªãºã ã®è©³ç´°ã«ã¯ç«ã¡å
¥ããªãã§ãããdp
ã¨ããé·ãlistãæ´æ°ãã¦ããã ãã®ã³ã¼ãã§ãã15,16è¡ç®ã§å®éã«List dp
ãæ´æ°ãã¦ãã¾ãã
ãã®ã³ã¼ãã®15,16è¡ç®ã¨ã³ã¡ã³ãã¢ã¦ãããã¦ãã17è¡ç®ã¯æ¬è³ªçã«ã¯ç価ã§ããããã17è¡ç®ã®æ¹ã¯æ¯åListã«å¤ãæ¸ãè¾¼ããã¨ã«ãªãããé常ã«é
ãã§ãã
å®éã«ããããå©ç¨ããã³ã¼ããæåºãã¦å®è¡é度ãæ¯è¼ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
ãã®ããã«ããªãé度ãå¤ãããã¨ã確èªã§ãã¾ããListã¸ã®ä¸å¿ è¦ãªæ¸ãè¾¼ã¿ããªãã注æããªããæ¸ãã¦ã¿ã¾ãããã
Listã®Listã®ã½ã¼ãã«ã¯operator.itemgetter
Listã®Listï¼äºæ¬¡å
é
åï¼ã®ã½ã¼ãã¯é
ãã§ãããkeyã®æå®ã®ä»æ¹ã§é«éåã§ãã¾ãã次ã®ãããªListãèãã¦ã¿ã¾ãããã
ããã2çªç®ã®è¦ç´ (Listã®ã¤ã³ããã¯ã¹ã¨ãã¦ã¯1)ã§ã½ã¼ããããã¨ãèãã¾ããä¸çªã·ã³ãã«ãªkeyã®æå®æ¹æ³ã¯lamdbaå¼ã使ã£ã¦æ¸ããã¨ã ã¨æãã¾ãããoperator.itemgetterã使ãã¨è¦ããããã¤æ©ããªãã¾ãã
å®éé度ãæ¯è¼ããã¨ã以ä¸ã®éãã§ãã
Listå ã®è¦ç´ ãæååã§ãã£ããããã¨æ´ã«sortã¯éããªãã®ã§ãitemgetterã使ã£ã¦ããã¨ç¡é£ã§ãã
localå¤æ°ãå©ç¨ããé«éå
Pythonã§ã¯ã°ãã¼ãã«å¤æ°ã«ã¢ã¯ã»ã¹ããããããã¼ã«ã«å¤æ°ã«ã¢ã¯ã»ã¹ããæ¹ãæ©ããã¨ãç¥ããã¦ãã¾ãããã®æ§è³ªã«ããå
¨ã¦ã®å¦çãmain()
ãªã©ã®é¢æ°ã«å
¥ãã¦ããã ãã§ãé«éåãè¦è¾¼ãã¾ãã
å
ç¨ã®ABC142Eã®ã³ã¼ããä¾ã«ãã¦ã¿ã¾ãããã以ä¸ã®ããã«mainé¢æ°å
ã«å¦çãæ¸ãã¦ããã¾ãã
ãããæåºãã¦ã¿ãã¨å®è¡æéã¯ãããã以ä¸ã®ããã«ãªãã¾ãã
def main():
㨠if __name__ == '__main__':
ã®é¨åã¯ãã³ãã¬ã¼ãã«ãã¦ããã¦ãè¯ãããããã¾ããã
PyPyã«ããé«éå
PyPyã¨ã¯
PyPyã¨ã¯Pythonã®å®è£
ã®ä¸ã¤ã§ããå®å®ãªJIT(=Just In Time)ã³ã³ãã¤ã©ã§ãCPythonãããé«éã«åä½ãã¾ããNumpyãªã©ã®ã©ã¤ãã©ãªã¯ä½¿ãã¾ããããã»ã¼å
¨ã¦ã®Pythonã®çµã¿è¾¼ã¿ã¢ã¸ã¥ã¼ã«ãæä¾ããã¦ãã¾ãã
競æããã°ã©ãã³ã°ã§ã¯åºæ¬çãªå¶å¾¡æ§æã¨ãã¼ã¿æ§é ãçµã¿åããã¦ã³ã¼ããæ¸ããã¨ãå¤ãã¨æãã®ã§ãPythonã§æ¸ããã³ã¼ãããã®ã¾ã¾ä½ãããPyPyã¨ãã¦æåºããã ãã§é度ãããªãä¸ãããã¨ãå¤ãã§ãã
ç¾å¨AtCoderã§æä¾ããã¦ããPyPy3 (7.3.0)ã¯Python3.6.9ã¨äºææ§ã®ãããã®ã«ãªãã¾ãã
Installæ¹æ³
pyenvãããããã§ããpyenvã¯Pythonã®ãã¼ã¸ã§ã³ç®¡çãä»®æ³ç°å¢ã®ç®¡çããµãã¼ããã¦ããããã¼ã«ã§ãã
ãã®ããã«ç°¡åã«æå®ãããã¼ã¸ã§ã³ã®ã¤ã³ã¹ãã¼ã«ãå¯è½ã§ãã
ãã¼ã¸ã§ã³ã®åãæ¿ãã以ä¸ã®ããã«ç°¡åã«ã§ãã¾ãã
使ãæ¹ã®è©³ç´°ã«ã¤ãã¦ã¯pyenvã®Githubãã覧ãã ããï¼
Pythonã¨ã®æ¯è¼
AtCoderã®EDPC B: Frog 2ã¨ããåçè¨ç»æ³ã®åé¡ãä¾ã«Pythonã¨PyPyã®æ¯è¼ããã¦ã¿ã¾ãããã以ä¸ã®ãããªå®è³ªäºéã«ã¼ãã®ã³ã¼ããPythonã¨PyPyã®ä¸¡æ¹ã§æåºãã¦ã¿ã¾ãã
ãã®ããã«å
¨ãåãã³ã¼ãã§ããPyPyã¨ãã¦å®è¡ããã ãã§ããªãé«éåããããã¨ã確èªã§ãã¾ãã
ä»åã®åé¡ã§ã¯Python3ã§ãACãããã¨ãã§ãã¾ããããããå³ããå¶ç´ã®å ´åPyPy3ããACã§ããªããã¨ãèãããã¾ãã
PyPyã ã¨é
ãå¦ç
PyPyã ã¨é
ãå¦çãããã¤ãç¥ããã¦ãã¾ãã
- å帰é¢æ°ãç¨ããå¦ç
- æååã«é¢é£ããå¦ç
- tupleã®æ¯è¼ãå«ããããªå¦ç ï¼sort, heapqãªã©ï¼
ã¨ããããlocalç°å¢ãAtCoderã®ã³ã¼ããã¹ãã§å®è¡æéãPythonã¨PyPyã§æ¯è¼ããæ©ãæ¹ã®è¨èªã§æåºããã®ãè¯ãã¨æãã¾ãã
ããããã¨ã©ã¼
RecursionError
競ããããã¦ããã¨å帰é¢æ°ã§å¦çãæ¸ããã¨ãããããã¾ãããå帰ã§ããåæ°ã«ã¯ä¸éã決ãããã¦ãã¾ãã以ä¸ã§ç¾ç¶ã®ä¸éå¤ã¯ç¢ºèªã§ãã¾ãã
AtCoderã®Python3.8ã ã¨ä¸è¨ã¯1000ã«ãªã£ã¦ããããã£ã¨ããéã«ä¸éã«éãã¦ãã¾ããRE(=Runtime Error)ã¨ãªããã¨ãããã¾ãã
åèµ·é¢æ°ã使ã£ãå¦çãããå ´åã«ã¯ä»¥ä¸ã®ããã«ä¸éãæä½ããã¦ããã¾ãããã
æ´æ°ã¨æµ®åå°æ°ç¹
ãã®è©±èªä½ã¯Pythonã«éããªãã§ãããPythonã¯åçåä»ãã®è¨èªã§ããåã®ãã¨ãæ·±ãèããªãã¦ãã³ã¼ããæ¸ãã¦ãã¾ãã®ã§ãæ°å¤ã®æ±ãã«ã¯æ³¨æãå¿
è¦ã§ããä¾ãã°ããå¤æ°ãåãã¯æ´æ°ãæ±ã£ã¦ãã¦ããéä¸ã§é¤ç®(/
)ãããã¨æµ®åå°æ°ç¹ï¼å¤§æµCè¨èªã®doubleã使ã£ã¦å®è£
ããã¦ããï¼ã«å¤æããã¦ãã¾ãã¾ããå²ãåãããããªå ´åã§ã/
ã¯æµ®åå°æ°ç¹ãè¿ãã¾ãã
ãã®ããã«æµ®åå°æ°ç¹ã§ã®è¨ç®ã«ã¯èª¤å·®ãçºçããã®ã§ãåççã«å
¨ã¦æ´æ°ã§è¨ç®ã§ãåºåãæ´æ°ãªåé¡ã®å ´åãéä¸ã®è¨ç®å¦çã¯åãæ¨ã¦é¤ç®//
ã使ãæ´æ°ã§è¨ç®ãã¦ããæ¹ãè¯ãã§ãã
ãããã«
åã®æå±ããFinatextã°ã«ã¼ãã§ã¯æ§ã ãªäºæ¥ãå±éãã¦ããããããããªé åã®ã¨ã³ã¸ãã¢ãåéãã¦ããã¾ãï¼æ¯éèå³ãããæ¹ã¯æ¡ç¨ãã¼ã¸ã¸ã¢ã¯ã»ã¹ããã ããã@Kevinrobot34ã¾ã§ç´æ¥ãé£çµ¡ãã ããï¼
åèæç®
- https://github.com/Kevinrobot34/cp_python_tips : ããã°å·çã«ä½¿ã£ãã³ã¼ãç½®ãå ´ã§ãã
- https://wiki.python.org/moin/TimeComplexity
- ãã¤ããã©ã¼ãã³ã¹Python
- Effective Python