「 Crieiteブログで 数学用語を使いたいんで、説明したら使っていい、ということにする☆」
Git hub: jection
「 じゃあ インジェクション、サージェクション、バイジェクション で」
「 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: [][][][][][]
「 校庭には 6個のボールがあり、ピッチャーは バケツに1つずつボールを入れた☆」
「 ボールには 素数 2、3、5、7、11、13 の番号が 黒のマジックペン太 で キュッキュッっと 濃く書かれていたとする☆」
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]
「 漏れず被らず、バケツの中身を、移し替えるのが インジェクションだぜ☆
関数はこんな感じでいいのか☆?」
# There are 5 peoples. Pitcher have six boxes.
pitcher = [[2], [3], [5], [7], [11], [13]]
first = [[], [], [], [], [], [], []]
second = [[], [], [], [], [], [], [], []]
third = [[], [], [], [], [], [], [], [], []]
catcher = [[], [], [], [], [], [], [], [], [], []]
「 ピッチャー以外の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]
「 インジェクションじゃなくて、キャッチボールしましょうよ」
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("")
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]
# 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
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("")
「 野球部員はクラスにして 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", [[], []])
「 箱が減ってしまって大丈夫か☆? ボールが こぼれ落ちるんじゃないか☆?」
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
「 だらっと書いたが、サージェクションは こんな感じだろうか……☆?」
# 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")
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
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]
「 偏ることは よくある☆
『よくある』がどれぐらいかの説明は 確率論に任せる☆ ここでは触れない☆」
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
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント