使用测距选项生成零件号的最佳方法

时间:2014-05-21 14:56:29

标签: javascript algorithm recursion combinatorics

完成后我有一个如下所示的部件号:TCW-K1-A4-B21AA

唯一的常量是TCW-部分,其他所有内容都指定了部件号的选项。我的最终目标是能够为csv文件中的每个配置提供所有可能的部件号。我的编程技巧有限,所以我尝试使用以下代码解决我的javascript问题:

var tcwArray = [['TCW-'], 
['K', 'J', 'T', 'R', 'S', 'E'], 
['1-', '2-', '3-', '4-', '5-', '6-', '7-', '8-'], 
['A', 'B'], 
['1-', '2-', '3-', '4-', '5-', '6-', '7-', '8-', '9-'], 
['A', 'B', 'C', 'D'], 
['0', '1', '2', '3'], 
['0', '1', '2'], 
['1', 'A', 'B', 'C', 'D', 'E'], 
['A', 'B']];

function allPossibleCases(arr) {
    if (arr.length === 0) {
        return [];
    }
    else if (arr.length === 1){
        return arr[0];
    }
    else {
        var result = [];
        var allCasesOfRest = allPossibleCases(arr.slice(1)); //recur with the rest of array
        for (var c in allCasesOfRest) {
            for (var i = 0; i < arr[0].length; i++) {
                result.push(arr[0][i] + allCasesOfRest[c]);
            }
        }
        return result;
    }

}
var r=allPossibleCases(tcwArray);

这对我不起作用,很可能是由于内存限制。我的问题是,我可以用更好的语言来做到这一点,因为我可以快速学习吗?

2 个答案:

答案 0 :(得分:0)

只有在数字完整时才附加到结果中(或者像贾斯汀所提到的那样存储在文件中。)

var results = [];

function allPossibleCases(arr, number) {
    if (arr.length === 0) {
        return result.push(number);
    }
    else {
        var first = arr[0];
        var rest = arr.slice(1);
        for (var i = 0; i < first.length; i++) {
            allPossibleCases(rest, number + first[i]);
        }
    }
}

答案 1 :(得分:0)

其他语言而言,Python相对容易进入。以下是Python中的实现:

all_descriptors = [
    ['TCW-'],
['K', 'J', 'T', 'R', 'S', 'E'],
['1-', '2-', '3-', '4-', '5-', '6-', '7-', '8-'],
['A', 'B'],
['1-', '2-', '3-', '4-', '5-', '6-', '7-', '8-', '9-'],
['A', 'B', 'C', 'D'],
['0', '1', '2', '3'],
['0', '1', '2'],
['1', 'A', 'B', 'C', 'D', 'E'],
['A', 'B']
]
all_part_numbers = []

# Recursive part number generator, all possible permutations for given list of descriptor lists
#   descriptors - the list of lists of descriptors
#   part_number - the part number string which has descriptors added to it with each recursion
def generate_permutations(descriptors, part_number):
    if len(descriptors) == 1:  # this is the farthest right descriptor
        for char_descriptor in descriptors[0]:
            # finish up, add last character descriptor to part number
            all_part_numbers.append(part_number + char_descriptor)
    else:
        for char_descriptor in descriptors[0]:
            # call this function recursively with the descriptor fields to the right of current field
            #     and append current descriptor to end of part_number
            generate_permutations(descriptors[1:], part_number + char_descriptor)

generate_permutations(all_descriptors, "")

outfile = open("part_numbers.csv", 'w')
for part_number in all_part_numbers[0:-1]:
    print("{},".format(part_number), file=outfile, end="")
# separate printing of last element so there is no comma at the end
print("{}".format(all_part_numbers[-1]), file=outfile, end="")

要节省内存,您可以跳过将部件号附加到all_part_numbers并直接打印到文件。但是,您必须找到一种方法来避免在最后一个部分编号上使用该逗号。不过,也许这无关紧要。