Python分配找不到正确的答案

时间:2016-11-04 01:52:21

标签: python python-3.x recursion

我正在为一个类进行python赋值,我们需要使用随机生成器来完成赋值(而不是递归地遍历选项)。

这是一个简单答案的经典问题,但随机选择必须偶然找到它。我无法弄清楚为什么输出重复相同的模式,因为函数看起来很稳固。是否存在导致实体相同移动的问题?

编辑:它会无限制地输出重复这些块,但是当所有元素都在“西方”列表中时它应该停止。

分配: 农民,狼,山羊,甘蓝,农民必须将所有实体移到河的西边。 狼,山羊和卷心菜不能单独留在一边。 狼会吃山羊,如果山羊一边(东边或西边),山羊会吃掉它们。 禁止[]列表反映了这些限制,每次元素改变河流边时都必须检查。

代码:

import random

# The following comments and some changes in the code were provided by Mr. Alexey Pogodin.
# Code has been changed to follow these guidelines.
# All style recommendations below are based on PEP 8 (Style Guide for Python Code)
# http://www.python.org/dev/peps/pep-0008/
# Python module names should be all-lowercase.
# Global variable names should be lowercase, with words separated by underscores as necessary to improve readability.
# Always surround these binary operators with a single space on either side: assignment, augmented assignment,
# comparisons, Booleans
# whitespace after comma is required

east = ['C', 'F', 'G', 'W']
west = []
forbidden = [['C', 'G', 'W'],['C', 'G'],['G', 'W']]

#Complete the following function so it Prints the objects at East and then the objects at West==========================
def print_contains(east, west):
    print(east)
    print(west)

    return

#Go west: Complete this function according to the instructions on HW4
def go_west(east, west):
    west.append('F')
    east.remove('F')

    isForbidden = 1

    while isForbidden == 1:
        curr = random.choice(east)
        west.append(curr)
        east.remove(curr)

        if (all(x in east for x in forbidden[0]) or
                all(x in east for x in forbidden[1]) or
                all(x in east for x in forbidden[2])):
            isForbidden = 1
            east.append(curr)
            west.remove(curr)
        else:
            isForbidden = 0
            break


    print_contains(east, west)
    print('-------------------------------------\n')
    return east, west




#Go East: Complete this function according to the instructions on HW4   
def go_east(east, west):
    east.append('F')
    west.remove('F')

    isForbidden = 1

    while isForbidden == 1:
        curr = random.choice(west)
        east.append(curr)
        west.remove(curr)

        if (all(x in west for x in forbidden[0]) or
                all(x in west for x in forbidden[1]) or
                all(x in west for x in forbidden[2])):
            isForbidden = 1
            west.append(curr)
            east.remove(curr)
        else:
            isForbidden = 0
            break


    print_contains(east, west)
    print('-------------------------------------\n')    
    return east, west


# Solution: This function returns True if all objects are on the West side otherwise returns False (One line of code)    
def solution():
    if not east:
        return True
    else:
        return False



#DO not change anything in the following lines. Your job is to complete the functions above.
# Main

print_contains(east, west)
print('-------------------------------------')

condition = True
while condition:
    east, west = go_west(east,west)
    if not solution():
        east, west = go_east(east,west)
    else:
        condition = False

输出:

['C', 'W', 'F', 'G']
[]
-------------------------------------

['W', 'C']
['F', 'G']
-------------------------------------

['W', 'C', 'F', 'G']
[]
-------------------------------------

['W', 'C']
['F', 'G']
-------------------------------------

['W', 'C', 'F', 'G']
[]
-------------------------------------

['W', 'C']
['F', 'G']
-------------------------------------

['W', 'C', 'F', 'G']
[]
-------------------------------------
etc...

1 个答案:

答案 0 :(得分:2)

当你往东走时,你总是与农民一起运送物品。 在第一次旅行(向西)之后,西岸只有一个项目。因此,选择陪伴农民往东走的唯一的选择就是你刚刚向西运的东西。 因此,您最终会在最初的东岸找到相同的物品:您只需在河流两侧之间来回运送随机物品。

一种解决方案是插入空的物品。这是随机挑选的,农民将自己排队。在某些情况下,您希望这个拼图可以解决。