Skip to content

Instantly share code, notes, and snippets.

class PriorityQueue:
INIT_VALUE = -1
MAX_SIZE = 2000001
def __init__(self):
self.heap = [PriorityQueue.INIT_VALUE]*(PriorityQueue.MAX_SIZE + 1)
self.size = 0
def insert(self, k):
self.size += 1
self.heap[self.size] = k
# Dynamic Programming - Longest Common Subsequence
q = int(input())
for _ in range(q):
X = [''] + list(input())
Y = [''] + list(input())
i,j = len(X),len(Y)
# i x j Matrix
LCS = [[-1 for a in range(j)] for b in range(i)]
for ii in range(i):
for jj in range(j):
# Graph I - Connected Components
# 幅優先探索すればよかった
import array
from collections import deque
def groupify(length, adj_list):
group = array.array('l', (-1 for _ in range(length)))
g_id = 0
dq = deque()
for idx in range(length):
if group[idx] == -1:
# Graph II - Single Source Shortest Path II
def dijkstra(length, adj_m):
INFTY = float('inf')
d = [INFTY for _ in range(length)]
d[0] = 0
Q = [i for i in range(length)]
while Q:
idx = INFTY
minv = INFTY
for q in Q:
# Heuristic Search - 8 Queens Problem
"""まだ途中"""
NUM = 8
Q = 1
Und = 0
F = 2
k = int(input())
B = [[Und for _ in range(NUM)] for _ in range(NUM)]
for _ in range(k):
r,c = map(int, input().split())
# よく使う
import os
import shutil
# フォルダの作成
os.mkdir('dir_path')
os.makedirs('dir_path') # 中間のディレクトリも作成するときはこっちが便利
# フォルダの移動 移動先までのフォルダは先に作成しておくべき
shutil.move('srcfile_path', 'dstfile_path')
@ikapper
ikapper / update_files.py
Last active March 12, 2017 04:53
1つのフォルダの全内容について、差分バックアップを行う
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import os, shutil
def copy_update(from_dir, to_dir, cnt=0):
"""
再帰的にコピーする。コピー元が新しい時は既存ファイルなら上書きする
"""
dst_exists = []
@ikapper
ikapper / show_fft.py
Created June 20, 2017 03:54
Using PyAudio, matplotlib, numpy and wxPython.
# -*- coding: utf-8 -*-
"""
Display fft data.
Environment:
matplotlib==2.0.2
numpy==1.13.0
PyAudio==0.2.11
wxPython==4.0.0a3
# -*- coding: utf-8 -*-
"""
Flaskを使って、mongodbに保存した画像ファイルを表示させる。
"""
# まずはmongoDBのセットアップ
from mongoengine import connect
#!/usr/bin/env python3
import timeit
import random
# ここでは確率的素数判定法を実装する
# 確率的素数判定法は合成数かそうではなさそうか判断する。複数回行って、信用性を確保するみたい
# まずは、原始的な考えで
def way1(num):