AtCoder cheat sheet in python (for myself)

Purpose

If you don't make a cheat sheet, it's getting tougher, so for organizing.

In c ++, it seems that the code is well known, but python seems to have an implicit wisdom-like writing style. Looking at the answers, there are many libraries and data structures that everyone uses for granted. I'm doing these implicit things while taking notes, but I'm confused, so I'll put them together for myself. When I started Atcoder with python, I wanted a summary of this kind of prerequisite knowledge. .. ..

There are many mistakes, but please try more.

Prerequisite cheat sheet

Python Competitive Programming Cheat Sheet for Yourself I always look here for how to input.


Cheat Sheet (python3) See here for divisors and powers. I don't know the meaning of about a few libraries, but it's very convenient to use.


Python standard library: order maintenance bisect bisect see here


Difference between sort and sorted to sort list in Python See here for sorting


[What is lambda used in python's sorted function] (https://qiita.com/n10432/items/e0315979286ea9121d57) The lamda function made this easy to understand.


Set operation with Python, set type (union, intersection and subset judgment, etc.) set,len,add,discard(), remove(), pop(), clear()


[Python] Summary of slicing operations See here for slices


[Calculate and get the greatest common divisor and least common multiple with Python] (https://note.nkmk.me/python-gcd-lcm/) See here for gcd (greatest common divisor) and lcm (least common multiple). You can put out 3 or 4 numbers.


[Get division quotient and remainder at the same time with Python divmod] (https://note.nkmk.me/python-divmod-quotient-remainder/) Look here for quotient


[Loop processing by Python for statement (range, enumerate, zip, etc.)] (https://note.nkmk.me/python-for-usage/) See here for range, enumerate, zip Intensional expressions such as [expression for variable name in iterable object] Multiple loops: itertools.product (), I don't use it myself, but I can use it when reading other people's code.)


Count the number of occurrences of each element in the list with Python Counter It's tough for me now, but it's unreasonably convenient to search for an array or some other element.

Tips

10^0.5 n**0.5


Count l.count('a')


Eliminate fog set([1,1,2,2,3,3]) ⇨[1,2,3]


if X not in p: X is not in the list of #p


Case conversion str.upper (): Convert all characters to uppercase str.lower (): Convert all characters to lowercase


Absolute value: abs () Sum: sum (list)


Larger: max (,) Smaller: min (,)


count.values()


Reverse order: reversed ()


Circulation in multiple columns with for: enumerate () Cycle in multiple columns with for: zip ()


mods are usually mod = 10 ** 9 + 7


Since the number of trials of 10 ^ 12 is quite strict, it tends to be a branch potash or a device. Since 10 ^ 6 is very close to the square, O (N ^ 2) tends to be close to the limit, so some ingenuity is required.


print ("". join (lis)): Now you can join the list


print(list(itertools.permutations([1, 2, 3]))) -> [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]


c = collections.Counter(l) ⇨Counter({'a': 4, 'c': 2, 'b': 1}) ⇨ c.keys () is ['a','b','c'] ⇨ c.values () is [4, 1, 2] ⇨ c.items () is [('a', 4), ('b', 1), ('c', 2)] ⇨ c.most_common () is ('a', 4), ('c', 2), ('b', 1) ⇨ c.most_common () [:: -1] is [('b', 1), ('c', 2), ('a', 4)] In other words, [:: -1] is arranged from the opposite. Meaning of (Quote: Count the number of occurrences of each element in the list with Python Counter)


a = lambda x : x*x print(a(4)) #16 (If you insert an anonymous function λ in a and 4 in x, x ^ 2)


If you don't understand the behavior in the code test, you can easily debug by checking what the variable looks like with print (variable).


Furthermore, the basic python grammar that is a prerequisite

for _ in range(N): This outputs 0 ~ N-1 Add ":" at the end The next line of the for statement is indented (4 spaces or tabs to make spaces) An error may occur if tab and space are mixed in the line following the for statement. You can also slice and substitute c for that amount, as in for c in l [2: 5]:. You can also start from 1 by setting for i in range (1: N):


if s == 1: = Is not one, but it is not a condition unless it is connected by two (= only = is an assignment) Add ":" at the end The next line of the if statement is indented (4 spaces or tabs to make spaces) An error may occur if tab and space are mixed in the line following the if statement.


while n!=1: The next line of the while statement is indented (4 spaces or tabs to make spaces) while means to do "while this conditional expression is satisfied".


What I personally plan to understand in the future

dfs, bfs, Union Find programs

[Guidelines for improving AtCoder, a competition pro taught by Red Coder [Intermediate Edition: Aim for Light Blue Coder! ]] (https://qiita.com/e869120/items/eb50fdaece12be418faa#2-3-%E5%88%86%E9%87%8E%E5%88%A5%E5%88%9D%E4%B8%AD%E7%B4%9A%E8%80%85%E3%81%8C%E8%A7%A3%E3%81%8F%E3%81%B9%E3%81%8D%E9%81%8E%E5%8E%BB%E5%95%8F%E7%B2%BE%E9%81%B8-100-%E5%95%8F) It seems that you can aim for light blue, or even blue, with 100 questions. Problems are categorized by method, so it is a good way to train a method that you are not good at. The article that solves this article with python [[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 4/22]] (https://qiita.com/rudorufu1981/items/71abd5320a636e9146cb)

Recommended Posts

AtCoder cheat sheet in python (for myself)
python [for myself]
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 15 in Python
Daily AtCoder # 47 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 45 in Python
Daily AtCoder # 30 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 28 in Python
Daily AtCoder # 39 in Python
Daily AtCoder # 20 in Python
PySpark Cheat Sheet [Python]
Daily AtCoder # 19 in Python
Daily AtCoder # 52 in Python
Daily AtCoder # 3 in Python
Daily AtCoder # 14 in Python
Daily AtCoder # 50 in Python
Daily AtCoder # 43 in Python
Daily AtCoder # 22 in Python
Daily AtCoder # 49 in Python
Daily AtCoder # 1 in Python
Daily AtCoder # 25 in Python
Daily AtCoder # 16 in Python
Daily AtCoder # 12 in Python
Daily AtCoder # 48 in Python
Daily AtCoder # 23 in Python
Daily AtCoder # 34 in Python
Daily AtCoder # 51 in Python
Daily AtCoder # 31 in Python
Daily AtCoder # 46 in Python
Daily AtCoder # 35 in Python
Daily AtCoder # 9 in Python
Daily AtCoder # 44 in Python
Daily AtCoder # 41 in Python
[Updating] Python Syntax cheat sheet for Java shop
Python memo (for myself): Array
Atcoder ABC164 A-C in Python
[Python3] Standard input [Cheat sheet]
Search for strings in Python
Python Tkinter notes (for myself)
Atcoder ABC167 A-D in Python
Techniques for sorting in Python
Atcoder ABC165 A-D in Python
Atcoder ABC166 A-E in Python