AtCoder Python チートシート

## 入力
### 一行だけ取る
|Input  |script  |結果  |
|---|---|---|
|abcde|s=input()  |s='abcde'  |
|abcde  |s=list(input())  |s=['a', 'b', 'c', 'd', 'e']  |
|5(1つだけ)  |a=int(input())  |a=5  |
|1 2  |x,y = map(int,input().split())  |x=1,y=2  |
|1 2 3 4 5 ... n  |li = input().split()  |li=['1','2','3',...,'n']  |
|1 2 3 4 5 ... n  |li = list(map(int,input().split()))  |li=[1,2,3,4,5,...,n]  |
|FFFTFTTFF  |li = input().split('T')  |li=['FFF', 'F', '', 'FF']  |
### 複数行取る
#### patern 1
```py:input.txt
3
a
b
c
```
```py:input.py
n=int(input())
string_list=[input() for i in range(n)]
#string_list<-['a','b','c']
```
#### patern 2
```py:input.txt
3
1 2 2 3 1 5
4 5 3 4 1 2
2 3 5 6 0 2
```
```py:input.py
n = int(input())
for i in range(n):
    listA=list(map(int,input().split()))
    #ここに逐次処理を書けばいいと思う。
```
```py:input.py
n = int(input())
for i in range(n):
    listA.append(list(map(int,input().split())))
print(listA)
#[[1, 2, 2, 3, 1, 5], [4, 5, 3, 4, 1, 2], [2, 3, 5, 6, 0, 2]]
```
## 出力
### 一行だけ
|Input  |script  |OutPut  |
|---|---|---|
|s = "abcde"|print(s)|abcde\n|
|s = "abcde"|print(s,end="")|abcde|
|a = 1, b = 2|print(a/b)|0.5\n|
|a = 1, b = 2|print("%lf" % (a/b))|0.500000\n|
|a = 1, b = 2|print("%.1lf" % (a/b))	|0.5\n|
|a = 1, b = 2|print(str(a+b)+"ですね"|0.5くらいかな\n|
### 複数
#### patern 1
```py:output.txt
a
b
c
```
```py:input.py
n=["a","b","c"]
for i in n:
    print(i)
```
#### patern 2
```py:output.txt
1 2 2 3 1 5
4 5 3 4 1 2
2 3 5 6 0 2
```
```py:input.py
n = [[1, 2, 2, 3, 1, 5], [4, 5, 3, 4, 1, 2], [2, 3, 5, 6, 0, 2]]
s = 3
t = 6
for i in range(s):
    for j in range(t-1):
        print(n[i][j],end = " ")
    print(n[i][t-1])
```
```py:input.py
n = [[1, 2, 2, 3, 1, 5], [4, 5, 3, 4, 1, 2], [2, 3, 5, 6, 0, 2]]
s = 3
t = 6
for i in range(s):
    for j in range(t-1):
        print(*n[i])
```
## 小数点
### 一行だけ
```py
a = 0.364364

print(a) #0.364364
print("{:.6f}".format(a)) #0.364364
print("{:.4f}".format(a)) #0.3644 小数点第4位に丸めることもできる。
```
## 進数変更
```py
b = 810

print("{:b}".format(b)) #1100101010  2進数表記 
print("{:o}".format(b)) #1452        8進数表記
print("{:x}".format(b)) #32a         16進数小文字表記
print("{:X}".format(b)) #32A         16進数大文字表記
```
## 二分探索
```py
li = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
s = 0
t = len(li)
def ch(x):
    y = 13
    if x > y:
        return 0
    else:
        return 1
while True:
    p = int((s+t)/2)
    pp = ch(p)
    if pp == 0:
        t = p
    elif pp == 1:
        s = p
    print(s,t)
    if s + 1 == t:
        break
ans = s
```
## bit探索
```py
li = [1,2,3,4,5,6,7,8,9]
for i in range(2**len(li)):
    s = "{:b}".format(i)
    s = list(s)
    s = [int(i) for i in s]
    s = [0]*(len(li) - len(s)) + s
    o = 0
    for j in range(len(s)):
        o = o + s[j] * li[j]
    #処理を
```
## sort
```py
l = [3, 1, 4, 5, 2]

l.sort()
print(l)
# [1, 2, 3, 4, 5]

l.sort(reverse=True)
print(l)
# [5, 4, 3, 2, 1]
```
```py
l = [3, 1, 4, 5, 2]

l_sorted = sorted(l)
print(l_sorted) # [1, 2, 3, 4, 5]
print(l)        # [3, 1, 4, 5, 2]

l_reverse_sorted = sorted(l, reverse=True)
print(l_reverse_sorted) # [5, 4, 3, 2, 1]
```
```py
l = [-3, 1, 4, -5, 2]
print(sorted(l)) # [-5, -3, 1, 2, 4]
print(sorted(l, key=abs)) # [1, 2, -3, 4, -5]

l = ['b', 'cc', 'aaa']
print(sorted(l)) # ['aaa', 'b', 'cc']
print(sorted(l, key=len)) # ['b', 'cc', 'aaa']

l = ['banana', 'cherry', 'apple']
print(sorted(l)) # ['apple', 'banana', 'cherry']

l.sort()
print(l) # ['apple', 'banana', 'cherry']
```