Appending a List and Recursion (Hamming Distance) - Python 3

你。 提交于 2020-01-06 08:14:02

问题


I'm supposed to write a program that takes a string of binary code and a number, and outputs all the strings within that hamming distance of the original string. I have a function that does everything, but in the output there are lists within lists. I understand why this is - the function is recursive, and sometimes it will return a list of possible values.

The problem is, I don't know how to change it so it outputs complete strings. For example, for a string of "0000" and hamming distance "2", I get: ["1['100', '010', '001']", "01['10', '01']", "001['1']"]

But what I want is:

1100
1010
1001
0110
0101
0011

Code is below:

def hamming(num, dist):
    if dist == 0:
        return num
    else:
        outputlist = list()
        for item in range(len(num)):
            if len(num[item:]) > dist - 1:
                if num[item] == "0":
                    restoflist = hamming(num[item + 1:], dist - 1)
                    outputlist.append(num[:item] + "1" + str(restoflist))
                else:
                    restoflist = hamming(num[item + 1:], dist - 1)
                    outputlist.append(num[:item] + "0" + str(restoflist))                
        return outputlist

回答1:


If you loop through the list that is returned by hamming you should be able to append the strings together the right way - like this:

def hamming(num, dist):
    if dist == 0:
        return num
    else:
        outputlist = list()
        for item in range(len(num)):
            if len(num[item:]) > dist - 1:
                if num[item] == "0":
                    restoflist = hamming(num[item + 1:], dist - 1)
                    if type(restoflist) is not str
                        for rest in restoflist:
                            outputlist.append(num[:item] + "1" + str(rest))
                    else
                        outputlist.append(num[:item] + "1" + str(restoflist))
                else:
                    restoflist = hamming(num[item + 1:], dist - 1)
                    if type(restoflist) is not str
                        for rest in restoflist:
                            outputlist.append(num[:item] + "0" + str(rest))
                    else
                        outputlist.append(num[:item] + "0" + str(restoflist))                
    return outputlist



回答2:


You can fix it by returning [num] rather that num in the base case, and in a general case, combine results from recursive calls using map and extend rather than append:

def hamming(num, dist):
    if dist == 0:
        return [num]
    else:
        outputlist = list()
        for item in range(len(num)):
            if len(num[item:]) > dist - 1:
                if num[item] == "0":
                    restoflist = hamming(num[item + 1:], dist - 1)
                    outputlist.extend(map(lambda x: num[:item] + "1" + x, restoflist))
                else:
                    restoflist = hamming(num[item + 1:], dist - 1)
                    outputlist.extend(map(lambda x: num[:item] + "0" + x, restoflist))                
        return outputlist

>>> hamming('0000', 2)
['1100', '1010', '1001', '0110', '0101', '0011']
>>> 


来源:https://stackoverflow.com/questions/22072112/appending-a-list-and-recursion-hamming-distance-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!