I have a function, which checks given two node chains and determines whether those two node chains contain same data values in same order. Function returns True if they do and returns False otherwise. I need to write this function using recursion.
Here's my attempt:
def check_chains(chain1, chain2):
if chain1 is None or chain2 is None:
return False
else:
if chain1.get_data() == chain2.get_data():
check_chains(chain1.get_next(), chain2.get_next())
return True
else:
return False
This one returns True even if node chains have different data values in them.
Below are some test cases:
1. Test case 1 (test passed)
chain1 = N.node(5 ,N.node(10,N.node(-15, N.node(1))))
chain2 = N.node(5 ,N.node(10,N.node(-15, N.node(1))))
expected = True
result = a7q8.check_chains(chain1, chain2)
if result!=expected:
print('Test failed')
2. test 2 (test failed, expected false but returned True)
chain1 = N.node(5 ,N.node(10,N.node(-15, N.node(1))))
chain2 = N.node(5 ,N.node(10,N.node(7, N.node(1))))
expected = True
result = a7q8.check_chains(chain1, chain2)
if result!=expected:
print('Test failed')