我不知道这是动态编程

时间:2019-12-01 11:25:59

标签: python dynamic-programming

我是在韩国大学学习算法的初学者。 这是通过仅使用python而不使用递归编程的动态编程来解决问题的任务。 问题是当您输入仅由“ a”,“ b”,“ c”组成的字符串的5个字母时,是否通过某些操作将结果打印出“ a”

我做到了,但是我不确定它是动态编程 如果您给我建议,我真的很感谢您的支持

问题。

       a b c
     a b b a
     b c b a
     c a c c

有一个由三个元素{a,b和c}组成的算术表,并且行,列和该表的相交值为您提供一个值,例如{aa = b,ab = b,ac = a}。

基于这些值,打印出是否可以针对任何字符串(“可能”或“不可能”)得出结果,  并在可能的情况下使用动态编程技术编写计算顺序,以通过括号指出顺序。

示例 输入:bbbba。 结果:可能,(b(bb)(bb)a)) 在上面的示例中,表格是通过引用第一个圆括号和Analysis来计算的,  (bb)是(b)。 然后(b(b))变成(b),而(b)a变成c,表明  满足bc = a的最终要求。 -语言必须使用Python。 -动态编程算法是允许的技术(不要使用递归x)。 -输出很多情况下获得的第一个值 -将字符串的长度(即输入值)设置为5。

def rule(x,y):
    if((x=='a' and y=='c') or (x=='b' and y=='c') or (x=='c' and y=='a')):
        return 'a'

    if((x=='a' and y=='a') or (x=='a' and y=='b') or (x=='b' and y=='b')):
        return 'b'

    if((x=='b' and y=='a') or (x=='c' and y=='b') or (x=='c' and y=='c')):
        return 'c'
def rule2(x,y,z):
    return rule(rule(x,y),z)
def rule3(x,y,z):
    return rule(x,rule(y,z))
def rule4(w,x,y,z):
    return rule(rule(w,x),rule(y,z))
def rule5(w,x,y,z):
    return rule(rule2(w,x,y),z)
def rule6(w,x,y,z):
    return rule(rule3(w,x,y),z)
def rule7(w,x,y,z):
    return rule(w,rule2(x,y,z))
def rule8(w,x,y,z):
    return rule(w,rule3(x,y,z))
def rule9(v,w,x,y,z):
    global k
    k='((('+v+w+')'+x+')'+y+z+')'
    return rule(rule2(v,w,x),rule(y,z))
def rule10(v,w,x,y,z):
    global k
    k='(('+v+'('+w+x+'))('+y+z+'))'
    return rule(rule3(v,w,x),rule(y,z))
def rule11(v,w,x,y,z):
    global k
    k='(('+v+w+')(('+x+y+')'+z+'))'
    return rule(rule(v,w),rule2(x,y,z))
def rule12(v,w,x,y,z):
    global k
    k='(('+v+w+')('+x+'('+y+z+')))'
    return rule(rule(v,w),rule3(x,y,z))
def rule13(v,w,x,y,z):
    global k
    k='((('+v+w+')('+x+y+'))'+z+')'
    return rule(rule4(v,w,x,y),z)
def rule14(v,w,x,y,z):
    global k
    k='(((('+v+w+')'+x+')'+y+')'+z+')'
    return rule(rule5(v,w,x,y),z)
def rule15(v,w,x,y,z):
    global k
    k='((('+v+'('+w+x+')'+y+')'+z+'))'
    return rule(rule6(v,w,x,y),z)
def rule16(v,w,x,y,z):
    global k
    k='('+v+'('+w+'(('+x+y+')'+z+')))'
    return rule(rule7(v,w,x,y),z)
def rule17(v,w,x,y,z):
    global k
    k='('+v+'('+w+'('+x+'('+y+z+'))))'
    return rule(rule8(v,w,x,y),z)
def rule18(v,w,x,y,z):
    global k
    k='('+v+'(('+w+x+')('+y+z+')))'
    return rule(v,rule4(w,x,y,z))
def rule19(v,w,x,y,z):
    global k
    k='(('+v+'(('+w+x+')'+y+')'+z+'))'
    return rule(v,rule5(w,x,y,z))
def rule20(v,w,x,y,z):
    global k
    k='('+v+'(('+w+'('+x+y+'))'+z+'))'
    return rule(v,rule6(w,x,y,z))
def rule21(v,w,x,y,z):
    k='('+v+'('+w+'(('+x+y+')'+'))'+z+')'
    return rule(v,rule7(w,x,y,z))
def rule22(v,w,x,y,z):
    global k
    k='('+v+'('+w+'('+x+'('+y+z+'))))'
    return rule(v,rule8(w,x,y,z))
def rule23(v,w,x,y,z):
    global k
    k='((('+v+w+')'+x+')'+'('+y+z+'))'
    return rule2(rule(v,w),x,rule(y,z))
def rule24(v,w,x,y,z):
    global k
    k='(('+v+w+')('+x+'('+y+z+')))'
    return rule3(rule(v,w),x,rule(y,z))

print("     input:",end='')
str=input()
str=list(str)
x=[rule9,rule10,rule11,rule12,rule13,rule14,rule15,rule16,rule17,rule18,rule19,
   rule20,rule21,rule22,rule23,rule24]
for i in range(0,16):
    y=x[i](str[0],str[1],str[2],str[3],str[4])
    if(y=='a'):
        print("possible,",end=' ')
        print(k)
        break
    if(y!='a' and i==15):
        print("impossible")

1 个答案:

答案 0 :(得分:1)

当然,有更好的方法可以解决您遇到的问题,并且您可能会了解到,一旦看到其他人的分配解决方案,但是关于递归/动态问题,内存中的代码执行就不是递归的。

您在那里创建的绝对是线性动态编程。

可以肯定的是,您还可以通过以下递归测试的实现来对其进行测试:https://stackoverflow.com/a/36663046/3564632

因此,总的来说,您可以尝试使用此示例进行测试:

from bdb import Bdb
import sys

class RecursionDetected(Exception):
    pass

class RecursionDetector(Bdb):
    def do_clear(self, arg):
        pass

    def __init__(self, *args):
        Bdb.__init__(self, *args)
        self.stack = set()

    def user_call(self, frame, argument_list):
        code = frame.f_code
        if code in self.stack:
            raise RecursionDetected
        self.stack.add(code)

    def user_return(self, frame, return_value):
        self.stack.remove(frame.f_code)

def test_recursion(func):
    detector = RecursionDetector()
    detector.set_trace()
    try:
        func()
    except RecursionDetected:
        return True
    else:
        return False
    finally:
        sys.settrace(None)

def rule(x,y):
    if((x=='a' and y=='c') or (x=='b' and y=='c') or (x=='c' and y=='a')):
        return 'a'

    if((x=='a' and y=='a') or (x=='a' and y=='b') or (x=='b' and y=='b')):
        return 'b'

    if((x=='b' and y=='a') or (x=='c' and y=='b') or (x=='c' and y=='c')):
        return 'c'
def rule2(x,y,z):
    return rule(rule(x,y),z)
def rule3(x,y,z):
    return rule(x,rule(y,z))
def rule4(w,x,y,z):
    return rule(rule(w,x),rule(y,z))
def rule5(w,x,y,z):
    return rule(rule2(w,x,y),z)
def rule6(w,x,y,z):
    return rule(rule3(w,x,y),z)
def rule7(w,x,y,z):
    return rule(w,rule2(x,y,z))
def rule8(w,x,y,z):
    return rule(w,rule3(x,y,z))
def rule9(v,w,x,y,z):
    global k
    k='((('+v+w+')'+x+')'+y+z+')'
    return rule(rule2(v,w,x),rule(y,z))
def rule10(v,w,x,y,z):
    global k
    k='(('+v+'('+w+x+'))('+y+z+'))'
    return rule(rule3(v,w,x),rule(y,z))
def rule11(v,w,x,y,z):
    global k
    k='(('+v+w+')(('+x+y+')'+z+'))'
    return rule(rule(v,w),rule2(x,y,z))
def rule12(v,w,x,y,z):
    global k
    k='(('+v+w+')('+x+'('+y+z+')))'
    return rule(rule(v,w),rule3(x,y,z))
def rule13(v,w,x,y,z):
    global k
    k='((('+v+w+')('+x+y+'))'+z+')'
    return rule(rule4(v,w,x,y),z)
def rule14(v,w,x,y,z):
    global k
    k='(((('+v+w+')'+x+')'+y+')'+z+')'
    return rule(rule5(v,w,x,y),z)
def rule15(v,w,x,y,z):
    global k
    k='((('+v+'('+w+x+')'+y+')'+z+'))'
    return rule(rule6(v,w,x,y),z)
def rule16(v,w,x,y,z):
    global k
    k='('+v+'('+w+'(('+x+y+')'+z+')))'
    return rule(rule7(v,w,x,y),z)
def rule17(v,w,x,y,z):
    global k
    k='('+v+'('+w+'('+x+'('+y+z+'))))'
    return rule(rule8(v,w,x,y),z)
def rule18(v,w,x,y,z):
    global k
    k='('+v+'(('+w+x+')('+y+z+')))'
    return rule(v,rule4(w,x,y,z))
def rule19(v,w,x,y,z):
    global k
    k='(('+v+'(('+w+x+')'+y+')'+z+'))'
    return rule(v,rule5(w,x,y,z))
def rule20(v,w,x,y,z):
    global k
    k='('+v+'(('+w+'('+x+y+'))'+z+'))'
    return rule(v,rule6(w,x,y,z))
def rule21(v,w,x,y,z):
    k='('+v+'('+w+'(('+x+y+')'+'))'+z+')'
    return rule(v,rule7(w,x,y,z))
def rule22(v,w,x,y,z):
    global k
    k='('+v+'('+w+'('+x+'('+y+z+'))))'
    return rule(v,rule8(w,x,y,z))
def rule23(v,w,x,y,z):
    global k
    k='((('+v+w+')'+x+')'+'('+y+z+'))'
    return rule2(rule(v,w),x,rule(y,z))
def rule24(v,w,x,y,z):
    global k
    k='(('+v+w+')('+x+'('+y+z+')))'
    return rule3(rule(v,w),x,rule(y,z))

print("     input:",end='')
str=input()
str=list(str)
x=[rule9,rule10,rule11,rule12,rule13,rule14,rule15,rule16,rule17,rule18,rule19,
   rule20,rule21,rule22,rule23,rule24]
for i in range(0,16):
    assert not test_recursion(lambda: x[i](str[0],str[1],str[2],str[3],str[4]))
    y = x[i](str[0],str[1],str[2],str[3],str[4])
    if(y=='a'):
        print("possible,",end=' ')
        print(k)
        break
    if(y!='a' and i==15):
        print("impossible")