2024-08-17

anond:20240817001425

・・・できたけど木の組み立てがしんどすぎるー

class TreeNode:
def __init__(self, name, attributes=None):
self.name = name
self.attributes = attributes or {}
self.children = []

def add_child(self, child_node):
self.children.append(child_node)

def display(self, level=0):
indent = " " * level
print(f"{indent}{self.name} {self.attributes}")
for child in self.children:
child.display(level + 1)

def has_dependency(self):
# ルートノード属性を持たないノード依存関係を判定しない
if not self.children:
return False

for child in self.children:
# 子ノードがBusinessHourかScheduleかをチェック
if "start_at" in child.attributes and "end_at" in child.attributes:
child_start = child.attributes["start_at"]
child_end = child.attributes["end_at"]

# 現在ノードがBusinessHourで、子がScheduleの場合
if "start_at" in self.attributes and "end_at" in self.attributes:
self_start = self.attributes["start_at"]
self_end = self.attributes["end_at"]

if self_start <= child_start and self_end >= child_end:
print(f"{child.name} (start_at: {child_start}, end_at: {child_end}, room_id: {child.attributes['room_id']}) is dependent on {self.name} (start_at: {self_start}, end_at: {self_end})")
else:
print(f"{child.name} (start_at: {child_start}, end_at: {child_end}, room_id: {child.attributes['room_id']}) is NOT dependent on {self.name} (start_at: {self_start}, end_at: {self_end})")

# 現在ノードがRoomで、子がScheduleの場合
elif self.name.startswith("Room"):
print(f"{child.name} (start_at: {child_start}, end_at: {child_end}, room_id: {child.attributes['room_id']}) is dependent on Room {self.name[-1]}")
else:
child.has_dependency()

# 子ノード属性を持たない場合再帰的に依存関係をチェック
else:
child.has_dependency()

# ノード作成
root = TreeNode("Root")
office_node = TreeNode("Office")

# Roomノード作成
room1_node = TreeNode("Room1")
room2_node = TreeNode("Room2")

# BusinessHourノード作成
business_hour1_node = TreeNode("BusinessHour1", {"start_at": 9, "end_at": 12})
business_hour2_node = TreeNode("BusinessHour2", {"start_at": 13, "end_at": 17})

# Scheduleノード作成
schedule1_node = TreeNode("Schedule1", {"start_at": 10, "end_at": 11, "room_id": 1})
schedule2_node = TreeNode("Schedule2", {"start_at": 14, "end_at": 15, "room_id": 1})
schedule3_node = TreeNode("Schedule3", {"start_at": 10, "end_at": 11, "room_id": 2})
schedule4_node = TreeNode("Schedule4", {"start_at": 14, "end_at": 15, "room_id": 2})

# 木構造の構築
root.add_child(office_node)
office_node.add_child(room1_node)
office_node.add_child(room2_node)
office_node.add_child(business_hour1_node)
office_node.add_child(business_hour2_node)

# Room1にSchedule1, Schedule2を追加
room1_node.add_child(schedule1_node)
room1_node.add_child(schedule2_node)

# Room2にSchedule3, Schedule4を追加
room2_node.add_child(schedule3_node)
room2_node.add_child(schedule4_node)

# BusinessHour1にSchedule1, Schedule3を追加
business_hour1_node.add_child(schedule1_node)
business_hour1_node.add_child(schedule3_node)

# BusinessHour2にSchedule2, Schedule4を追加
business_hour2_node.add_child(schedule2_node)
business_hour2_node.add_child(schedule4_node)

# 木構造の表示
root.display()

# 依存関係のチェック
office_node.has_dependency()
room1_node.has_dependency()
room2_node.has_dependency()
business_hour1_node.has_dependency()
business_hour2_node.has_dependency()

Root {}
Office {}
Room1 {}
Schedule1 {'start_at': 10, 'end_at': 11, 'room_id': 1}
Schedule2 {'start_at': 14, 'end_at': 15, 'room_id': 1}
Room2 {}
Schedule3 {'start_at': 10, 'end_at': 11, 'room_id': 2}
Schedule4 {'start_at': 14, 'end_at': 15, 'room_id': 2}
BusinessHour1 {'start_at': 9, 'end_at': 12}
Schedule1 {'start_at': 10, 'end_at': 11, 'room_id': 1}
Schedule3 {'start_at': 10, 'end_at': 11, 'room_id': 2}
BusinessHour2 {'start_at': 13, 'end_at': 17}
Schedule2 {'start_at': 14, 'end_at': 15, 'room_id': 1}
Schedule4 {'start_at': 14, 'end_at': 15, 'room_id': 2}
Schedule1 (start_at: 10, end_at: 11, room_id: 1) is dependent on Room 1
Schedule2 (start_at: 14, end_at: 15, room_id: 1) is dependent on Room 1
Schedule3 (start_at: 10, end_at: 11, room_id: 2) is dependent on Room 2
Schedule4 (start_at: 14, end_at: 15, room_id: 2) is dependent on Room 2
Schedule1 (start_at: 10, end_at: 11, room_id: 1) is dependent on BusinessHour1 (start_at: 9, end_at: 12)
Schedule3 (start_at: 10, end_at: 11, room_id: 2) is dependent on BusinessHour1 (start_at: 9, end_at: 12)
Schedule2 (start_at: 14, end_at: 15, room_id: 1) is dependent on BusinessHour2 (start_at: 13, end_at: 17)
Schedule4 (start_at: 14, end_at: 15, room_id: 2) is dependent on BusinessHour2 (start_at: 13, end_at: 17)
Schedule1 (start_at: 10, end_at: 11, room_id: 1) is dependent on Room 1
Schedule2 (start_at: 14, end_at: 15, room_id: 1) is dependent on Room 1
Schedule3 (start_at: 10, end_at: 11, room_id: 2) is dependent on Room 2
Schedule4 (start_at: 14, end_at: 15, room_id: 2) is dependent on Room 2
Schedule1 (start_at: 10, end_at: 11, room_id: 1) is dependent on BusinessHour1 (start_at: 9, end_at: 12)
Schedule3 (start_at: 10, end_at: 11, room_id: 2) is dependent on BusinessHour1 (start_at: 9, end_at: 12)
Schedule2 (start_at: 14, end_at: 15, room_id: 1) is dependent on BusinessHour2 (start_at: 13, end_at: 17)
Schedule4 (start_at: 14, end_at: 15, room_id: 2) is dependent on BusinessHour2 (start_at: 13, end_at: 17)
  • A ー>BみたいなDAGは分かるけど Aがこういう属性持って number start number end Bがこういう属性持ってるとき number position Bのpositionが、Aのstartとendの範囲内にある場合は依存関係あり...

    • この分野は素人だけど、有向非巡回グラフのことなのであれば高さ3の有向木で根からAという節点が生えAからBという葉が生えるようにするという話?それであれば何が問題なのかがよく...

      • 例えば貸しオフィスの例で表現すると ER図がこんな感じ https://mermaid.live/edit#pako:eNqNUsFuwyAM_RXkc_sDHNemaqVtkRrlhoRYMC1agYmC1irJv5c0UbJqqTb7AH7P2A9MDZWTCBSY7Rz9WouDF4ZZkizfbHarjDTNctnUZJ_nb4SSywNX91FnNpoP9MQppSvkWvZM2y_3w...

        • 高さ3の有向木で根からAという節点が生えAからBという葉が生える class TreeNode: def __init__(self, name, attributes=None): self.name = name self.attributes = attributes or {} self.children = [] d...

          • うーむ?やっぱ木じゃOFFICE、ROOM、SCHEDULE、BUSINESS_HOURの4つは表現できないかー

            • で・・・できたけど木の組み立てがしんどすぎるー class TreeNode: def __init__(self, name, attributes=None): self.name = name self.attributes = attributes or {} self.children = [] def add_child(self, chi...

              • あー依存関係は木で表現して、条件はattributesとして持たせればいいのか あしたやるぞー!

                • 依存関係は木で表現 ノードにロック持たせる ロックに条件持たせる やりたいことはできてるように見えるが、うーんしんどい # Entity Relation Diagram# ```mermaid# ---# title: Rental Office example# -...

                  • そんでこのロックのデータをこんなかんじでRedisにもてばネットワーク越しに依存関係のあるロックできる? Type キー名 値 String "Office1" true String "Office2" false Stri...

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん