1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Atcoderほぼ緑コーダーのスニペット(Python)

Posted at

記事構成

1. Atcoder自己紹介
2. スニペット集
3. 最後に

1. Atcoder自己紹介

  • 茶色🟤コーダー
  • 主な使用言語:Python,C++(C++は最近使ってません)
  • 一時期休んでたけど最近再開
  • 現在のグラフ↓

abckuri.png

まあほぼ緑コーダーって言っても問題ないはず

2. スニペット集

一応競プロで使いやすいものを作ってます.

Yes,YES,No,NO

競プロのよくある出力用に

Yes出力
"Print Yes": {
	 	"prefix": "yes",
		"body": [
			"print('Yes')",
	 	],
	 	"description": "print Yes"
	}
No出力
"Print No": {
		"prefix": "no",
	    "body": [
		   "print('No')",
		],
		"description": "print No"
    }
YES出力
"Print YES": {
		"prefix": "Yes",
		"body": [
			"print('YES')",
		],
		"description": "print YES"
	}
NO出力
"Print NO": {
		"prefix": "No",
	    "body": [
		   "print('NO')",
		],
		"description": "print NO"
    }

標準入力

よく使う標準入力形式を一通り

単体出力(int)
"input a": {
		"prefix": "a",
	    "body": [
		   "a=int(input())",
		],
		"description": "input a"
    }
リスト入力
"input abc": {
		"prefix": "abc",
	    "body": [
		   "a = list(map(int, input().split()))",
		],
		"description": "input a[]"
    }
二重配列入力
"input aa": {
		"prefix": "aa",
	    "body": [
		   "a = [list(map(int, input().split())) for _ in range(n)]",
		],
		"description": "input a[][]"
    }

Forループ

for

ふぉーーー
"for roop": {
		"prefix": "fo",
	    "body": [
		   "for i in range(n):",
		],
		"description": "for roop"
    }

その他メソッド

一部必要ないものも混じってます

n以下の数字の素数の配列作成
"prime number": {
		"prefix": "prime",
	    "body": [
			"def prime_num(n):",
			"	if n < 2:",
			"		return []",
			"	sieve = [True] * (n + 1)",
			"	sieve[0], sieve[1] = False, False",			
			"	for i in range(2, int(n**0.5) + 1):",
			"		if sieve[i]:",
			"			for j in range(i * i, n + 1, i):",
			"				sieve[j] = False",
			"	return [i for i, v in enumerate(sieve) if v]",
		],
		"description": "n以下の数字の素数の配列"
    }
リストの要素が辞書式順序で何番目かを返す関数
"dict order": {
		"prefix": "dict",
	    "body": [
			"from math import factorial",
			"def dictCheck(L):",
			"	index = 0",
			"	while len(L)> 1:",
			"		a = len([l for l in L if l<L[0]])",			
			"		index = index + a * factorial(len(L) - 1)",
			"		L = L[1:]",
			"	return index",
		],
		"description": "リストの要素が辞書式順序で何番目か"
    }
1からnまでの和を返す関数
"sum_to_n": {
		"prefix": "sum_to_n",
	    "body": [
			"def sumn(n):",
			"	return n * (n + 1) // 2",
		],
		"description": "1からnまでの和"
    }
配列内に整数nが存在するかを2分探索で探して返す関数
"binary_search": {
		"prefix": "binary_search",
	    "body": [
			"def binary_search(arr, N):",
			"	low = 0",
			"	high = len(arr) - 1",
			"	while low <= high:",
			"		mid = (low + high) // 2",
			"		if arr[mid] == N:",
			"			return True",
			"		elif arr[mid] < N:",
			"			low = mid + 1",
			"		else:",
			"			high = mid - 1",
			"	return False",
		],
		"description": "配列内に整数nが存在するかを2分探索で"
    }
最大公約数を返す関数
"gcd": {
		"prefix": "gcd",
	    "body": [
			"def gcd(a, b):",
			"	while b:",
			"		a, b = b, a % b",
			"	return a",
		],
		"description": "最大公約数"
    }
最小公倍数を返す関数
"lcm": {
		"prefix": "lcm",
	    "body": [
			"def lcm(a, b):",
			"	return abs(a * b) // gcd(a, b)",
		],
		"description": "最小公倍数"
    }
aのb乗をmで割った余りを返す関数(これ何で作ったんだ?)
"pow_mod": {
		"prefix": "pow_mod",
	    "body": [
			"def pow_mod(a, b, m):",
			"	res = 1",
			"	a %= m",
			"	while b > 0:",
			"		if b & 1:",
			"			res = (res * a) % m",
			"		a = (a * a) % m",
			"		b >>= 1",
			"	return res",
		],
		"description": "aのb乗をmで割った余りを返す関数"
    }
Bit全探索用n桁の00...0〜11...1をリストに格納する関数
"generate01": {
		"prefix": "generate01",
	    "body": [
			"def generate01(n):",
			"	return [format(i, f'0{n}b') for i in range(2**n)]",
		],
		"description": "n桁の01をリストで返す"
    }

3. 最後に

私のおすすめスニペットをコメントで教えて

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?