【问题标题】:Solving a Chain Link Python Puzzle:解决链式 Python 拼图:
【发布时间】:2023-04-07 00:17:01
【问题描述】:

我不确定从以下 python 谜题开始。

“你持有一个链的链接。实现一个方法longerSide来查找链的哪一侧有更多的链接,相对于你持有的链接。如果左侧有更多的链接返回Side.left。如果右侧有更多链接,则返回Side.right,如果两边的链接数量相等,或者如果链是闭环,则返回Side.none。 例如,下面的代码应该输出True

left = ChainLink() 
middle = ChainLink() 
right = ChainLink() 
left.append(middle) 
middle.append(right) 
print(left.longerSide() == Side.right)

我真的不知道如何处理这个问题。我想展示我迄今为止所做的一些事情,但我还没有创造出任何实质性的东西。到目前为止,我只定义了以下枚举

from enum import Enum
class Side(Enum):
    NONE = 0
    LEFT = 1
    RIGHT = 2

如果有人有任何可以帮助我的建议或资源,我将不胜感激。

谢谢

【问题讨论】:

  • 如果你能数出两边的链接数,那就很简单了。那你怎么能这样呢?至少,每个链接都需要记录其最左侧的链接(如果有)和最右侧的链接(如果有)——什么是合适的数据结构?
  • 谢谢,在这种情况下,双向链表是合适的数据结构吗?
  • 是的,这是一个不错的选择。现在,如何计算当前链接右侧的链接数量?
  • 我会这样做:如果heldLink.next != None counter += 1 这是在正确的轨道上吗?
  • 当然,这是您需要的循环的一部分。

标签: python algorithm puzzle chain


【解决方案1】:

所以这里有一个在代码中使用 cmets 来解决这个难题的方法。

from enum import Enum

class Side(Enum):
    none = 0
    left = 1
    right = 2

class ChainLink:

    def __init__(self):
        self._left = None
        self._right = None

    def append(self, link):
        if self._right is not None: raise Exception('Link already connected!')
        self._right = link
        link._left = self

    def longer_side(self):
        # Initialize right side and left side counters
        right = 0
        left = 0
    
        # Create an empty list that will be used to check if the link 
        # has been used already which indicates it's a loop
        seen_links = []

        # Set the link to be the first one on the right side
        link = self._right
    
        # Loop through all the links while the next one on the right is of type "ChainLink"
        while isinstance(link , ChainLink):
            # If the link on the right appears in the seen_links array, this means 
            # it's a loop.           
            if link in seen_links:
                print("Loop")
                return Side.none
            # Add +1 to the right side counter for each link 
            right += 1
        
            # Add the link to the seen_links array
            seen_links.append(link)
        
            # Set the link to the next one on the right.
            link = link._right
    
        # At this moment we know the count of links on the right side, and we know
        # if the chain is a loop or not. Now we just need to count the left side.
    
        # Set link to the first link on the left.
        link = self._left
    
        # Loop through all the links while the next one on the left is of type "ChainLink"
        while isinstance(link , ChainLink): 
            # Add +1 to left side counter
            left += 1 
            # set link to next on the left.
            link = link._left  
    
        # Return needed value depending on which side is longer
        if left > right:
            return Side.left
        elif left < right:
            return Side.right
    
        # Return Side.none if both sides are equal.
        return Side.none

if __name__ == "__main__":
    left = ChainLink()
    middle = ChainLink()`
    right = ChainLink()
    left.append(middle)
    middle.append(right)
    print(left.longer_side() == Side.right)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多