M.Hiroi's Home Page
http://www.geocities.jp/m_hiroi/

Lightweight Language

お気楽 Python3 プログラミング超入門

[ Home | Light | Python3 ]

●Hello, Python3!

●基本的なデータ型

●基本的な演算子

●基本的な制御構造

●関数

●イテレータとジェネレータ

●簡単なプログラム (1)

#
# sample01.py : 簡単なプログラム (1)
#
#               Copyright (C) 2018 Makoto Hiroi
#

#
# FizzBuzz
#
def check(n):
    if n % 15 == 0: return 'FizzBuzz'
    if n % 3 == 0: return 'Fizz'
    if n % 5 == 0: return 'Buzz'
    return n

def fizzbuzz():
    for x in range(1, 101):
        print(check(x), end=' ')

def fizzbuzz1():
    return map(check, range(1, 101))

#
# 数値積分 (中点則で円周率を求める)
#
def midpoint(n):
    w = 1.0 / n
    s = 0.0
    for i in range(1, n + 1):
        x = (i - 0.5) * w
        s += 4.0 / (1.0 + x * x)
    return s * w

#
# 平均値と標準偏差
#
import math

# 乱数で生成した身長のデータ
height = [
    148.7, 149.5, 133.7, 157.9, 154.2, 147.8, 154.6, 159.1, 148.2, 153.1,
    138.2, 138.7, 143.5, 153.2, 150.2, 157.3, 145.1, 157.2, 152.3, 148.3,
    152.0, 146.0, 151.5, 139.4, 158.8, 147.6, 144.0, 145.8, 155.4, 155.5,
    153.6, 138.5, 147.1, 149.6, 160.9, 148.9, 157.5, 155.1, 138.9, 153.0,
    153.9, 150.9, 144.4, 160.3, 153.4, 163.0, 150.9, 153.3, 146.6, 153.3,
    152.3, 153.3, 142.8, 149.0, 149.4, 156.5, 141.7, 146.2, 151.0, 156.5,
    150.8, 141.0, 149.0, 163.2, 144.1, 147.1, 167.9, 155.3, 142.9, 148.7,
    164.8, 154.1, 150.4, 154.2, 161.4, 155.0, 146.8, 154.2, 152.7, 149.7,
    151.5, 154.5, 156.8, 150.3, 143.2, 149.5, 145.6, 140.4, 136.5, 146.9,
    158.9, 144.4, 148.1, 155.5, 152.4, 153.3, 142.3, 155.3, 153.1, 152.3,
]

# 平均値と標準偏差
def meansd(xs):
    m = sum(xs) / len(xs)
    s = 0.0
    for x in xs:
        s += (x - m) * (x - m)
    return m, math.sqrt(s / len(xs))

# 1 回の読み込みで求める
def meansd2(xs):
    m, s = 0.0, 0.0
    k = len(xs)
    for i in range(1, k + 1):
        x = xs[i - 1] - m
        m += x / i
        s += (i - 1) * x * x / i
    return m, math.sqrt(s / k)

#
# パスカルの三角形
#

# 2 次元配列版
def pascal(n):
    table = [[] for _ in range(n + 1)]
    table[0].append(1)
    table[1].append(1)
    table[1].append(1)
    for i in range(2, n + 1):
        table[i].append(1)
        for j in range(1, i):
            table[i].append(table[i - 1][j - 1] + table[i - 1][j])
        table[i].append(1)
    for i in range(n + 1):
        print(table[i])

# 一次元配列版
def pascal1(n):
    table = [1] * (n + 1)
    print([1])
    for i in range(1, n + 1):
        for j in range(i - 1, 0, -1):
            table[j] += table[j - 1]
        print(table[:i+1])

#
# 素数
#

# 単純版
def is_prime(n, ps):
    for p in ps:
        if p * p > n: break
        if n % p == 0: return False
    return True

def prime(n):
    ps = [2]
    for x in range(3, n + 1, 2):
        if is_prime(x, ps):
            ps.append(x)
    return ps

# エラトステネスの篩
def sieve(n):
    ps = [2]
    xs = list(range(3, n + 1, 2))
    while True:
        p = xs[0]
        if p * p > n: break
        ps.append(p)
        xs = list(filter(lambda x: x % p != 0, xs))
    return ps + xs

# 高速版
def sieve1(n):
    ps = [True] * ((n + 1) // 2 - 1)
    x = 3
    while x * x <= n:
        y = (x - 3) // 2
        if ps[y]:
            y += x
            while y < len(ps):
                ps[y] = False
                y += x
        x += 2
    return [2] + [i * 2 + 3 for i, x in enumerate(ps) if x]

#
# 素因数分解
#
def factorization(n):
    def factor_sub(n, m):
        c = 0
        while n % m == 0:
            c += 1
            n //= m
        return c, n
    #
    buff = []
    c, m = factor_sub(n, 2)
    if c > 0: buff.append((2, c))
    c, m = factor_sub(m, 3)
    if c > 0: buff.append((3, c))
    x = 5
    while m >= x * x:
        c, m = factor_sub(m, x)
        if c > 0: buff.append((x, c))
        if x % 6 == 5:
            x += 2
        else:
            x += 4
    if m > 1: buff.append((m, 1))
    return buff

#
# 順列
#
def permutations(n, xs, func):
    def perm(ps):
        if len(ps) == n:
            func(tuple(ps))
        else:
            for x in xs:
                if x in ps: continue
                ps.append(x)
                perm(ps)
                ps.pop()
    perm([])

# ジェネレータ版
def make_perm(n, xs):
    if n == 0:
        yield []
    else:
        for ys in make_perm(n - 1, xs):
            for x in xs:
                if x in ys: continue
                yield ys + [x]

# 重複順列
def repeat_perm(n, xs, func):
    def perm(ps):
        if len(ps) == n:
            func(tuple(ps))
        else:
            for x in xs:
                ps.append(x)
                perm(ps)
                ps.pop()
    perm([])

#
# 組み合わせ
#

# 二重再帰
def combination_number(n, r):
    if r == 0 or n == r:
        return 1
    else:
        return combination_number(n - 1, r) + combination_number(n - 1, r - 1)

# 末尾再帰
def combination_number1(n, r):
    if n == 0 or r == 0: return 1
    return combination_number1(n, r - 1) * (n - r + 1) // r

# 組み合わせ
def combinations(n, xs, func):
    def comb(i, a):
        if len(a) == n:
            func(tuple(a))
        elif i < len(xs):
            a.append(xs[i])
            comb(i + 1, a)
            a.pop()
            comb(i + 1, a)
    comb(0, [])

# ジェネレータ版
def make_comb(n, xs, i = 0):
    if n == 0:
        yield []
    elif i < len(xs):
        for ys in make_comb(n - 1, xs, i + 1):
            yield [xs[i]] + ys 
        for ys in make_comb(n, xs, i + 1):
            yield ys

# 重複組み合わせ
def repeat_comb(n, xs, func):
    def comb(i, a):
        if len(a) == n:
            func(tuple(a))
        elif i < len(xs):
            a.append(xs[i])
            comb(i, a)
            a.pop()
            comb(i + 1, a)
    comb(0, [])

if __name__ == '__main__':
    print("----- FizzBuzz -----")
    fizzbuzz()
    print("")
    print(list(fizzbuzz1()))
    
    print("----- 数値積分 -----")
    print(midpoint(10000))

    print("---- 平均値と標準偏差 -----")
    print(meansd(height))
    print(meansd2(height))

    print("----- パスカルの三角形 -----")
    pascal(15)
    pascal1(15)

    print("----- 素数 -----")
    print(prime(100))
    print(sieve(200))
    print(sieve1(300))

    print("----- 素因数分解 -----")
    print(factorization(24))
    print(factorization(12345678))
    print(factorization(1234567890))
    print(factorization(11111111111))

    print("----- 順列 -----")
    permutations(4, [1,2,3,4], print)
    print(list(make_perm(4, [1,2,3,4])))

    print("----- 重複順列 -----")
    repeat_perm(3, [1,2,3], print)

    print("----- 組み合わせの数 -----")
    print(combination_number(22, 11))
    print(combination_number1(26, 13))
    
    print("----- 組み合わせ -----")
    combinations(3, [1,2,3,4,5], print)
    print(list(make_comb(3, [1,2,3,4,5])))
    
    print("----- 重複組み合わせ -----")
    repeat_comb(3, [1,2,3,4,5], print)
----- FizzBuzz -----
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz
Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz
76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz',
 19, 'Buzz', 'Fizz', 22, 23, 'Fizz', 'Buzz', 26, 'Fizz', 28, 29, 'FizzBuzz', 31, 32, 'Fizz', 34, 'Buzz',
 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'FizzBuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52,
 53, 'Fizz', 'Buzz', 56, 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz',
 'Buzz', 71, 'Fizz', 73, 74, 'FizzBuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86,
 'Fizz', 88, 89, 'FizzBuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz', 'Buzz']
----- 数値積分 -----
3.141592654423134
---- 平均値と標準偏差 -----
(150.62699999999998, 6.433472701426501)
(150.62699999999998, 6.433472701426506)
----- パスカルの三角形 -----
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
[1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1]
[1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1]
[1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1]
[1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]
[1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1]
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
[1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1]
[1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1]
[1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1]
[1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]
[1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1]
----- 素数 -----
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 
 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
----- 素因数分解 -----
[(2, 3), (3, 1)]
[(2, 1), (3, 2), (47, 1), (14593, 1)]
[(2, 1), (3, 2), (5, 1), (3607, 1), (3803, 1)]
[(21649, 1), (513239, 1)]
----- 順列 -----
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)
[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], 
 [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2],
 [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3],
 [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
----- 重複順列 -----
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 2, 1)
(1, 2, 2)
(1, 2, 3)
(1, 3, 1)
(1, 3, 2)
(1, 3, 3)
(2, 1, 1)
(2, 1, 2)
(2, 1, 3)
(2, 2, 1)
(2, 2, 2)
(2, 2, 3)
(2, 3, 1)
(2, 3, 2)
(2, 3, 3)
(3, 1, 1)
(3, 1, 2)
(3, 1, 3)
(3, 2, 1)
(3, 2, 2)
(3, 2, 3)
(3, 3, 1)
(3, 3, 2)
(3, 3, 3)
----- 組み合わせの数 -----
705432
10400600
----- 組み合わせ -----
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 3, 4)
(1, 3, 5)
(1, 4, 5)
(2, 3, 4)
(2, 3, 5)
(2, 4, 5)
(3, 4, 5)
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
----- 重複組み合わせ -----
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 1, 4)
(1, 1, 5)
(1, 2, 2)
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 3, 3)
(1, 3, 4)
(1, 3, 5)
(1, 4, 4)
(1, 4, 5)
(1, 5, 5)
(2, 2, 2)
(2, 2, 3)
(2, 2, 4)
(2, 2, 5)
(2, 3, 3)
(2, 3, 4)
(2, 3, 5)
(2, 4, 4)
(2, 4, 5)
(2, 5, 5)
(3, 3, 3)
(3, 3, 4)
(3, 3, 5)
(3, 4, 4)
(3, 4, 5)
(3, 5, 5)
(4, 4, 4)
(4, 4, 5)
(4, 5, 5)
(5, 5, 5)

Copyright (C) 2018 Makoto Hiroi
All rights reserved.

[ Home | Light | Python3 ]