Injection, Surjection, Bijection☆(^~^)

読了目安:15分

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 Crieiteブログで 数学用語を使いたいんで、説明したら使っていい、ということにする☆」

Git hub: jection

KIFUWARABE_80x100x8_01_Futu.gif
「 ブルバキの真似か☆」

OKAZAKI_Yumemi_80x80x8_02_Syaberu.gif
「 じゃあ インジェクション、サージェクション、バイジェクション で」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 Python3 の練習も兼ねたいんで、Python3 を使うことにする☆」

# There are 5 peoples. Pitcher have six boxes.
pitcher = [[2], [3], [5], [7], [11], [13]]
first = [[], [], [], [], [], []]
second = [[], [], [], [], [], []]
third = [[], [], [], [], [], []]
catcher = [[], [], [], [], [], []]


def show(person):
    for box in person:
        print("[", end="")
        first = True
        for ball in box:
            if not first:
                print(", ", end="")
            else:
                first = False
            print("{:>2}".format(ball), end="")
        print("]", end="")
    print("")


# Result.
print("Pitcher: ", end="")
show(pitcher)

print("First  : ", end="")
show(first)

print("Second : ", end="")
show(second)

print("Third  : ", end="")
show(third)

print("Catcher: ", end="")
show(catcher)
Pitcher: [ 2][ 3][ 5][ 7][11][13]
First  : [][][][][][]
Second : [][][][][][]
Third  : [][][][][][]
Catcher: [][][][][][]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 9人集まらなかった野球部が 校庭に5人いる☆」

KIFUWARABE_80x100x8_01_Futu.gif
「 悲しい……☆」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 5人は 6個ずつ バケツを持っている☆」

OKAZAKI_Yumemi_80x80x8_02_Syaberu.gif
「 グラブを持てばいいのに……」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 校庭には 6個のボールがあり、ピッチャーは バケツに1つずつボールを入れた☆」

KIFUWARABE_80x100x8_01_Futu.gif
「 何かの儀式か☆」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 ボールには 素数 2、3、5、7、11、13 の番号が 黒のマジックペン太 で キュッキュッっと 濃く書かれていたとする☆」

OKAZAKI_Yumemi_80x80x8_02_Syaberu.gif
「 本当に野球部員なのかしら……?」

Injection

import random

def inject(person1, person2):
    if len(person2) < len(person1):
        print("Fail    : Person1 {}, Person2 {}.".format(
            len(person1), len(person2)))
        return

    destinations = list(range(len(person2)))
    random.shuffle(destinations)

    for i in range(0, len(person1)):
        person2[destinations[i]] = person1[i]


# Go!
inject(pitcher, first)
inject(first, second)
inject(second, third)
inject(third, catcher)
Pitcher: [ 2][ 3][ 5][ 7][11][13]
First  : [13][ 3][ 5][ 7][ 2][11]
Second : [ 5][11][ 2][ 3][13][ 7]
Third  : [13][ 5][11][ 2][ 3][ 7]
Catcher: [11][ 7][ 3][ 2][13][ 5]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 漏れず被らず、バケツの中身を、移し替えるのが インジェクションだぜ☆
関数はこんな感じでいいのか☆?」

KIFUWARABE_80x100x8_01_Futu.gif
「 何が嬉しくて こんなこと してるのか分からん☆」

# There are 5 peoples. Pitcher have six boxes.
pitcher = [[2], [3], [5], [7], [11], [13]]
first = [[], [], [], [], [], [], []]
second = [[], [], [], [], [], [], [], []]
third = [[], [], [], [], [], [], [], [], []]
catcher = [[], [], [], [], [], [], [], [], [], []]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 ピッチャー以外の4人は、ピッチャーより多くのバケツを持っていることにしよう☆」

Pitcher: [ 2][ 3][ 5][ 7][11][13]
First  : [ 5][ 3][13][ 2][][ 7][11]
Second : [ 5][ 2][ 3][][][ 7][13][11]
Third  : [ 5][][ 3][][ 2][13][][11][ 7]
Catcher: [ 2][ 5][ 3][][][13][][][ 7][11]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 ちゃんと インジェクションできているな☆」

OKAZAKI_Yumemi_80x80x8_02_Syaberu.gif
「 インジェクションじゃなくて、キャッチボールしましょうよ」

def show(label, person):
    print("{:<8}: ".format(label), end="")

    for box in person:
        print("[", end="")
        first = True
        for ball in box:
            if not first:
                print(", ", end="")
            else:
                first = False
            print("{:>2}".format(ball), end="")
        print("]", end="")
    print("")

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 表示を ワンライナー(1行)で書けるように 改造☆」

def inject(person1, person2):
    # Delete an empty box.
    person1 = [x for x in person1 if x]

    if len(person2) < len(person1):
        print("Fail    : Person1 {}, Person2 {}.".format(
            len(person1), len(person2)))
        return

    destinations = list(range(len(person2)))
    random.shuffle(destinations)

    for i in range(0, len(person1)):
        person2[destinations[i]] = person1[i]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 インジェクションを キャッチボールに対応☆」

# Go!
print("radio> Starting line up.")
show("Pitcher", pitcher)
show("First", first)
show("Second", second)
show("Third", third)
show("Catcher", catcher)

print("radio> The pitcher threw the ball towards first.")
inject(pitcher, first)
show("First", first)

print("radio> First returned the ball to the pitcher.")
inject(first, pitcher)
show("Pitcher", pitcher)

print("radio> The pitcher threw the ball towards second.")
inject(pitcher, second)
show("Second", second)

print("radio> Second returned the ball to the pitcher.")
inject(second, pitcher)
show("Pitcher", pitcher)

print("radio> The pitcher threw the ball towards third.")
inject(pitcher, third)
show("Third", third)

print("radio> Third returned the ball to the pitcher.")
inject(third, pitcher)
show("Pitcher", pitcher)

print("radio> The pitcher threw the ball towards catcher.")
inject(pitcher, catcher)
show("Catcher", catcher)

print("radio> Catcher returned the ball to the pitcher.")
inject(catcher, pitcher)
show("Pitcher", pitcher)
radio> Starting line up.
Pitcher : [ 2][ 3][ 5][ 7][11][13]
First   : [][][][][][][]
Second  : [][][][][][][][]
Third   : [][][][][][][][][]
Catcher : [][][][][][][][][][]
radio> The pitcher threw the ball towards first.
First   : [][ 3][ 2][13][ 5][ 7][11]
radio> First returned the ball to the pitcher.
Pitcher : [13][ 2][ 7][ 5][11][ 3]
radio> The pitcher threw the ball towards second.
Second  : [][11][ 3][13][][ 2][ 7][ 5]
radio> Second returned the ball to the pitcher.
Pitcher : [11][13][ 3][ 2][ 5][ 7]
radio> The pitcher threw the ball towards third.
Third   : [][13][ 7][11][][][ 2][ 3][ 5]
radio> Third returned the ball to the pitcher.
Pitcher : [ 5][ 3][13][11][ 2][ 7]
radio> The pitcher threw the ball towards catcher.
Catcher : [13][ 3][][][ 2][ 7][][ 5][11][]
radio> Catcher returned the ball to the pitcher.
Pitcher : [13][ 2][ 3][11][ 5][ 7]
Info    : Finished

KIFUWARABE_80x100x8_01_Futu.gif
「 おっ、野球っぽくなったな☆」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 本当か☆」

Suejection

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 次はサージェクションだな☆」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 あっ、めんどくせっ☆!」

class Person(object):
    def __init__(self, name, boxes):
        self._name = name
        self._boxes = boxes
        return

    @property
    def name(self):
        return self._name

    @property
    def boxes(self):
        return self._boxes

    def clear_boxes(self):
        for box in self._boxes:
            box.clear()

    def show(self):
        print("{:<8}: ".format(self._name), end="")

        for box in self._boxes:
            print("[", end="")
            first = True
            for ball in box:
                if not first:
                    print(", ", end="")
                else:
                    first = False
                print("{:>2}".format(ball), end="")
            print("]", end="")
        print("")

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 野球部員はクラスにして show は メソッドにするぜ☆」

# There are 5 peoples. Pitcher have six boxes.
pitcher = Person("Pitcher", [[2], [3], [5], [7], [11], [13]])
first = Person("First", [[], [], [], [], []])
second = Person("Second", [[], [], [], []])
third = Person("Third", [[], [], []])
catcher = Person("Catcher", [[], []])

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 名前も箱も持てだぜ☆」

KIFUWARABE_80x100x8_01_Futu.gif
「 箱が減ってしまって大丈夫か☆? ボールが こぼれ落ちるんじゃないか☆?」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 そこは野球部員を信じろだぜ☆」

def surject(person1, person2):
    """
    len(person1.boxes) >= len(person2.boxes)
    """

    if len(person1.boxes) < len(person2.boxes):
        print("Fail    : I can not be satisfied. Src {}, Dst {}.".format(
            len(person1.boxes), len(person2.boxes)))
        return

    destinations = []
    for i in range(0, len(person1.boxes)):
        destinations.append(i % len(person2.boxes))

    random.shuffle(destinations)

    for i in range(0, len(person1.boxes)):
        person2.boxes[destinations[i]].extend(person1.boxes[i])

    return

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 だらっと書いたが、サージェクションは こんな感じだろうか……☆?」

# Go!
print("radio> Starting line up.")
pitcher.show()
first.show()
second.show()
third.show()
catcher.show()

print("radio> The pitcher threw the ball towards first.")
first.clear_boxes()
surject(pitcher, first)
first.show()

print("radio> The first threw the ball towards second.")
second.clear_boxes()
surject(first, second)
second.show()

print("radio> The second threw the ball towards third.")
third.clear_boxes()
surject(second, third)
third.show()

print("radio> The third threw the ball towards catcher.")
catcher.clear_boxes()
surject(third, catcher)
catcher.show()

print("Info    : Finished")

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 キャッチボールはできないんで、一方通行にするぜ☆」

radio> Starting line up.
Pitcher : [ 2][ 3][ 5][ 7][11][13]
First   : [][][][][]
Second  : [][][][]
Third   : [][][]
Catcher : [][]
radio> The pitcher threw the ball towards first.
First   : [ 3, 13][ 2][ 7][11][ 5]
radio> The first threw the ball towards second.
Second  : [ 2,  7][11][ 5][ 3, 13]
radio> The second threw the ball towards third.
Third   : [ 5,  3, 13][ 2,  7][11]
radio> The third threw the ball towards catcher.
Catcher : [ 2,  7, 11][ 5,  3, 13]
Info    : Finished

OKAZAKI_Yumemi_80x80x8_02_Syaberu.gif
「 箱が2個でも うまくキャッチしたものねぇ」

KIFUWARABE_80x100x8_01_Futu.gif
「 これは 鳩ノ巣原理☆?!」

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 まぐれ☆」

radio> Starting line up.
Pitcher : [ 2][ 3][ 5][ 7][11][13]
First   : [][][][][]
Second  : [][][][]
Third   : [][][]
Catcher : [][]
radio> The pitcher threw the ball towards first.
First   : [ 2,  7][13][11][ 5][ 3]
radio> The first threw the ball towards second.
Second  : [13, 11][ 5][ 2,  7][ 3]
radio> The second threw the ball towards third.
Third   : [ 2,  7,  3][13, 11][ 5]
radio> The third threw the ball towards catcher.
Catcher : [ 2,  7,  3, 13, 11][ 5]

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 偏ることは よくある☆
『よくある』がどれぐらいかの説明は 確率論に任せる☆ ここでは触れない☆」

Bijection

KITASHIRAKAWA_Chiyuri_80x100x8_01_Futu.gif
「 バイジェクションは最初にやったな☆」

from my_lib.person import Person
from my_lib.jection import biject

# There are 5 peoples. Pitcher have six boxes.
pitcher = Person("Pitcher", [[2], [3], [5], [7], [11], [13]])
first = Person("First", [[], [], [], [], [], []])
second = Person("Second", [[], [], [], [], [], []])
third = Person("Third", [[], [], [], [], [], []])
catcher = Person("Catcher", [[], [], [], [], [], []])

# Go!
print("radio> Starting line up.")
pitcher.show()
first.show()
second.show()
third.show()
catcher.show()

print("radio> The pitcher threw the ball towards first.")
first.clear_boxes()
biject(pitcher, first)
first.show()

print("radio> First returned the ball to the pitcher.")
pitcher.clear_boxes()
biject(first, pitcher)
pitcher.show()

print("radio> The pitcher threw the ball towards second.")
second.clear_boxes()
biject(pitcher, second)
second.show()

print("radio> Second returned the ball to the pitcher.")
pitcher.clear_boxes()
biject(second, pitcher)
pitcher.show()

print("radio> The pitcher threw the ball towards third.")
third.clear_boxes()
biject(pitcher, third)
third.show()

print("radio> Third returned the ball to the pitcher.")
pitcher.clear_boxes()
biject(third, pitcher)
pitcher.show()

print("radio> The pitcher threw the ball towards catcher.")
catcher.clear_boxes()
biject(pitcher, catcher)
catcher.show()

print("radio> Catcher returned the ball to the pitcher.")
pitcher.clear_boxes()
biject(catcher, pitcher)
pitcher.show()

print("Info    : Finished")
radio> Starting line up.
Pitcher : [ 2][ 3][ 5][ 7][11][13]
First   : [][][][][][]
Second  : [][][][][][]
Third   : [][][][][][]
Catcher : [][][][][][]
radio> The pitcher threw the ball towards first.
First   : [ 7][ 5][ 3][11][13][ 2]
radio> First returned the ball to the pitcher.
Pitcher : [ 3][ 5][ 2][11][ 7][13]
radio> The pitcher threw the ball towards second.
Second  : [ 7][13][ 3][11][ 5][ 2]
radio> Second returned the ball to the pitcher.
Pitcher : [ 7][ 3][13][ 5][ 2][11]
radio> The pitcher threw the ball towards third.
Third   : [ 5][13][11][ 7][ 3][ 2]
radio> Third returned the ball to the pitcher.
Pitcher : [ 3][ 2][11][13][ 7][ 5]
radio> The pitcher threw the ball towards catcher.
Catcher : [ 7][ 2][ 5][ 3][11][13]
radio> Catcher returned the ball to the pitcher.
Pitcher : [ 2][ 5][ 7][11][13][ 3]
Info    : Finished

KIFUWARABE_80x100x8_01_Futu.gif
「 で、インジェクション、サージェクション、バイジェクション とは何だったのか……☆?」

ツイッターでシェア
みんなに共有、忘れないようにメモ

むずでょ@きふわらべ第29回世界コンピューター将棋選手権一次予選36位

光速のアカウント凍結されちゃったんで……。ゲームプログラムを独習中なんだぜ☆電王戦IIに出た棋士もコンピューターもみんな好きだぜ☆▲(パソコン将棋)WCSC29一次予選36位、SDT5予選42位▲(パソコン囲碁)AI竜星戦予選16位

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント