1~9の4つの組合せの四則計算

0を除いた組合せ

import itertools

seq = range(1, 10)

# 組合せ
data = list(itertools.combinations(seq, 4))

print(len(data))
# 126
import itertools

def test_eval(s):

    try:
        r = eval(s)
    except ZeroDivisionError:
        r = None

    if r == 10:
        print(s)
        return True
    else:
        return False

seq = range(1, 10)

data = [i for i in itertools.permutations(seq, 4)]

print(len(data))

# print(data)

calc = [j for j in itertools.product("+-*/", repeat=3)]

result = []

for d in data:

    for c in calc:
        t1 = f"{d[0]} {c[0]} {d[1]} {c[1]} {d[2]} {c[2]} {d[3]}"

        if test_eval(t1):
            result.append(tuple(sorted(list(d))))

        t2 = f"( {d[0]} {c[0]}  {d[1]} {c[1]} {d[2]} ) {c[2]} {d[3]} "

        if test_eval(t2):
            result.append(tuple(sorted(list(d))))

        t3 = f"{d[0]} {c[0]} ( {d[1]} {c[1]} {d[2]} {c[2]} {d[3]} )"

        if test_eval(t3):
            result.append(tuple(sorted(list(d))))

        t4 = f"( {d[0]} {c[0]} {d[1]} ) {c[1]} ( {d[2]} {c[2]} {d[3]} )"

        if test_eval(t4):
            result.append(tuple(sorted(list(d))))

        t5 = f"{d[0]} {c[0]} ( {d[1]} {c[1]} {d[2]} ) {c[2]} {d[3]}"

        if test_eval(t5):
            result.append(tuple(sorted(list(d))))

        t6 = f"( {d[0]} {c[0]} {d[1]} ) {c[1]} {d[2]} {c[2]} {d[3]}"

        if test_eval(t6):
            result.append(tuple(sorted(list(d))))

        t7 = f"{d[0]} {c[0]} {d[1]} {c[1]} ( {d[2]} {c[2]} {d[3]} )"

        if test_eval(t7):
            result.append(tuple(sorted(list(d))))


print(len(result))
print(len(set(result)))

愛媛県内の状況のPDFから検査陽性者の状況をスクレイピング3

shellで作ってみた

wget https://www-eu.apache.org/dist/pdfbox/2.0.21/pdfbox-app-2.0.21.jar -O pdfbox-app.jar
wget https://www.pref.ehime.jp/h25500/kansen/documents/kennai_link.pdf -O kennnai_link.pdf

java -jar pdfbox-app.jar ExtractText -sort -startPage 1 -endPage 1 -console kennnai_link.pdf | grep '合 計' | awk '{print $2, $3, $4}' | grep -oP '[0-9,]+' > data.tsv
java -jar pdfbox-app.jar ExtractText -sort -startPage 1 -endPage 2 -console kennnai_link.pdf | grep -oP "([0-9,]+)人" | sed -r 's/([0-9,]+)人/\1/g' >> data.tsv

0~9の4つの組合せ

import itertools

seq = range(10)

result = ["".join(map(str, i)) for i in itertools.combinations(seq, 4)]

print(len(result))
# 210

print(result)

愛媛県内の状況のPDFから検査陽性者の状況をスクレイピング2

PDFをテキスト化して抽出する方が簡単だったorz

imabari.hateblo.jp

import pathlib
import re

import pdfbox
import requests

url = "https://www.pref.ehime.jp/h25500/kansen/documents/kennai_link.pdf"

r = requests.get(url)
r.raise_for_status()

p = pathlib.Path("data.pdf")

with p.open(mode="wb") as fw:
    fw.write(r.content)

pdf = pdfbox.PDFBox()

for i in range(1, 3):
    pdf.extract_text("data.pdf", f"data{i}.txt", sort=True, start_page=i, end_page=i)

with open("data1.txt") as fr:
    txt1 = fr.read()

with open("data2.txt") as fr:
    txt2 = fr.read()

print(txt1)
print(txt2)

m1 = re.search(r"^合 計 ([0-9,]+) ([0-9,]+) ([0-9,]+)$", txt1, re.M)
検査 = [int(i.replace(",", "")) for i in m1.groups()]

m2 = re.search("治療中:([0-9]+)人", txt1)
治療中 = int(m2.group(1).replace(",", ""))

m3 = re.search("退院等:([0-9]+)人", txt1)
退院等 = int(m3.group(1).replace(",", ""))

状況 = [int(i.replace(",", "")) for i in re.findall("([0-9,]+)人", txt2)]

print(検査)
print(治療中)
print(退院等)

print(状況)

最近買った本