I’m implementing the longest common subsequence for n dimensions. Current problem: how do I traverse the n strings? Simply nesting for loops won’t work anymore, because I need n of them. What’s the nice solution for that problem? Loops + recursion, I suppose, but how exactly? I’m not asking for the complete algorithm, but only how to generate all combinations for the dynamic programming algorithm.
Example in 2D:
for position, char in word0:
for position1, char1 in word1:
# code here
This is similar to counting, say, from 0000 up to 9999, which would correspond to four words ten letters each: 0000->0001->0002->…->0009->0010->… At each stage you increase the last digit, and when it rolls over you increase the preceding one, etc, until when the first digit would roll over you’re done.
Here’s one way you could encapsulate the counting in an iterator: