2021-05-01から1ヶ月間の記事一覧

Project Euler Problem 24

Python import itertools a = list(itertools.permutations(range(10)))[1000000 - 1] print(''.join(list(map(str, a))))

PythonのpyocrでPDFの特定の位置から文字列を読み取って、そのファイル名を変更する

前提 PDFファイルが「pdf」ディレクトリに保存されている。 読み取る文字列は英数字である。 読み取る文字列はPDFの1ページ目に書かれている。 import pdf2image import pyocr import pyocr.builders import glob import os import pprint for file in glob.…

Pythonのpdf2imageでPDFを画像に変換する

PDFをページごとに画像ファイルに保存する import pdf2image # PDFのページごとの画像データのリストが得られる img_list = pdf2image.convert_from_path('test.pdf') for idx, img in enumerate(img_list): img.save(str(idx) + '.png')

LinuxでTesseractを使って画像ファイルに書かれたテキストデータをテキストファイルに出力する

Tesseractのインストール $ sudo apt install tesseract-ocr $ sudo apt install libtesseract-dev 日本語の訓練済みモデルのインストール $ sudo apt install tesseract-ocr-jpn tesseract-ocr-jpn-vert $ sudo apt install tesseract-ocr-script-jpan tess…

Project Euler Problem 23

Python def sum_of_divisors(n): s = 1 i = 2 while i * i <= n: if n % i == 0: if i * i == n: s += i else: s += i + n // i i += 1 return s def is_abundant(n): return sum_of_divisors(n) > n LIMIT = 28123 abundant_list = list(filter(is_abundant…

Project Euler Problem 22

Python def name_score(name): return sum(map((lambda c: ord(c) - 64), name)) f = open('p022_names.txt', 'r') name_list = list(map((lambda s: s.strip('"')), f.read().split(','))) f.close() name_list.sort() print(sum([(idx + 1) * name_score(n…

Project Euler Problem 21

Python def sum_of_divisors(n): if n == 1: return 0 s = 1 i = 2 while i * i <= n: if n % i == 0: if i * i == n: s += i else: s += i + n // i i += 1 return s def is_amicable(n): m = sum_of_divisors(n) if n != m: if sum_of_divisors(m) == n: r…

Project Euler Problem 20

Python import math print(sum(map(int, str(math.factorial(100)))))

Project Euler Problem 19

Python import datetime answer = 0 for y in range(1901, 2001): for m in range(1, 13): if datetime.date(y, m, 1).weekday() == 6: answer += 1 print(answer)

Pythonで日付から曜日を取得する

>>> import datetime >>> d = datetime.date(2021, 5, 25) >>> type(d) <class 'datetime.date'> >>> d.strftime('%A') 'Tuesday' >>> d.strftime('%a') 'Tue' >>> d.weekday() 1</class>

Project Euler Problem 18

Python 方法1 下の行から処理する。 隣り合う2つの数の内、大きいものを選んで、 その数を上の数に足して、上の数を上書きする。3 7 4 2 4 6 8 5 9 3 ↓ 3 7 4 10 13 15 ↓ 3 20 19 ↓ 23 f = open('data', 'r') data_list = [] for line in f: data_list.appe…

Project Euler Problem 17

Python # add 1 to 9 answer = len('onetwothreefourfivesixseveneightnine') len_1to9 = answer # add 10 to 19 answer += len('teneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen') # add 20 to 99 answer += len('twenty') * 10 +…

Project Euler Problem 16

Python 方法1 def sum_of_digits(n): s = 0 while n != 0: n, r = divmod(n, 10) s += r return s print(sum_of_digits(2 ** 1000)) 方法2 >>> sum(map(int, str(2 ** 1000)))

Project Euler Problem 15

>>> from scipy.special import comb >>> comb(40, 20, exact=True)

Pythonで階乗、順列、組み合わせを求める

階乗 >>> import math >>> math.factorial(3) 6 >>> math.factorial(4) 24 >>> math.factorial(5) 120 順列の場合の数 >>> from scipy.special import perm >>> perm(4, 2, exact=True) 12 >>> perm(5, 2, exact=True) 20 順列の生成 >>> import itertools >…

Project Euler Problem 14

def next_number(n): if n % 2 == 0: return n // 2 else: return n * 3 + 1 def collatz_len(n): if n in collatz_dic: return collatz_dic[n] elif n == 1: return 1 else: ret = 1 + collatz_len(next_number(n)) collatz_dic[n] = ret return ret collat…

Project Euler Problem 13

f = open('data', 'r') s = 0 for line in f: s += int(line) print(str(s)[:10]) f.close()

密度の高さがコロナウイルスで起きている困難の共通の原因

密度の高さがコロナウイルスで起きている困難の共通の原因であると思った。 大都市→人口密度の高さ 病院→コロナウイルス患者の密度の高さ 介護施設→基礎疾患がある人の密度の高さ 密度を高めるのは効率がよくなるからだろう。 ごみをごみ捨て場に集めるのと…

LinuxでノートPCのタッチパッドを無効化する方法

下記のページを参考にしました。 qiita.com xinputをインストールする $ sudo apt install xinput タッチパッドの名前を探す 「Virtual core pointer」の中の「PS/2」が含まれるものがそれである場合が多いそうです。 $ xinput list ⎡ Virtual core pointer …

宗教の作り方

CPU、OS、言語処理系、プロトコルスタックなど、 いろいろなものが自作できるようなので、 宗教も自作できるかなと思いました。 宗教の作り方 とりあえずの今の私の結論としては、 世界と人生はどのようなものかについて自分が考えていることと、 自分がいい…

LibreOfficeでExcelファイルをPDFに変換する(Linux・コマンドライン)

LinuxでコマンドラインでLibreOfficeを使ってExcelファイルをPDFに変換する 下記の例ではカレントディレクトリに「test.pdf」が作られる $ libreoffice --convert-to pdf test.xlsx「--outdir」オプションで出力先ディレクトリを指定できる 下記の例では「ou…

Pythonによるデータ分析入門

Pandas CSVファイルを読む 「data.csv」を読んで先頭の5行を表示する import pandas as pd csv_data = pd.read_csv('data.csv') print(csv_data.head()) データを縦に結合する(行方向に増やす) import pandas as pd data1 = pd.read_csv('data1.csv') data2 …

Python入門(文字列の置換など)

文字列の両端から指定した文字を削除する >>> '..abc..'.strip('.') 'abc' >>> 'ab\n'.strip('\n') 'ab' 大文字、小文字の変換 # 大文字に変換する >>> 'ab'.upper() 'AB' # 小文字に変換する >>> 'AB'.lower() 'ab' # 大文字、小文字を入れ替える >>> 'Ab'.…

Python入門(文字列)

文字列 文字列はシングルクォートまたはダブルクォートで囲って作る >>> 'abc' 'abc' >>> type('abc') <class 'str'> >>> "abc" 'abc' >>> type("abc") <class 'str'> シングルクォート3個で囲うこともできる こうすると複数行の文字列を作ることができる \nは改行を表す >>> '''aaa ..</class></class>…

Python入門(2進数・16進数・型の変換)

2進数と16進数 先頭に0bをつけると2進数になる >>> 0b10 2 >>> 0b11 3 >>> 0b100 4 先頭に0xをつけると16進数になる >>> 0x9 9 >>> 0xa 10 >>> 0xb 11 >>> 0xf 15 >>> 0x10 16 型の変換 数字の文字列を数値に変換する >>> int('12') 12 >>> int('-34') -34 t…

Python入門(計算)

計算 $ python3 足し算 >>> 1 + 2 3 引き算 >>> 3 - 2 1 掛け算 >>> 2 * 3 6 >>> 2 * (-3) -6 割り算 >>> 5 / 2 2.5 整数の割り算(小数点以下は切り捨て) >>> 5 // 2 2 0で割ろうとするとエラーになる >>> 5 / 0 Traceback (most recent call last): File "<stdin>"</stdin>…

Pythonのcryptographyモジュールのインストールエラー

MX Linux(Debian系Linux)で $ pip3 install cryptographyを実行したらエラーになった。 build/temp.linux-x86_64-3.7/_openssl.c:575:10: fatal error: openssl/opensslv.h: そのようなファイルやディレクトリはありません #include <openssl/opensslv.h> ^~~~~~~~~~~~~~~~~~~~ c</openssl/opensslv.h>…

PythonのPyAutoGUIで画面キャプチャを取得する

コマンドで画面キャプチャを取得するツールのscrotをインストールしておく必要がある $ sudo apt install scrot>>> import pyautogui 画面キャプチャを取得する >>> im = pyautogui.screenshot() >>> im <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1366x768 at 0x7F310D68EFD0> >>> type(im) <class 'PIL.PngImagePlugin.PngImageFile'> >>> im.size (1366, 768) …</class></pil.pngimageplugin.pngimagefile>

PythonのPyAutoGUIでマウスの座標を取得する

>>> import pyautogui >>> pos = pyautogui.position() >>> pos Point(x=229, y=342) >>> type(pos) <class 'pyautogui.Point'> >>> pos[0] 229 >>> pos[1] 342 >>> pos.x 229 >>> pos.y 342</class>

PythonのPyAutoGUIでマウスを動かす

画面の座標(200, 100)にマウスカーソルを移動するプログラム 座標は左上が(0, 0) durationを小さくすると速く動く >>> import pyautogui >>> pyautogui.moveTo(200, 100, duration=0.5) 移動量を指定して動かすプログラム 今の位置から右にどれだけ下にどれ…