I currently have 6 separate for loops which iterate over a list of numbers looking to match specific sequences of numbers and replace them like this:
0,1,0 => 0,0,0
0,1,1,0 => 0,0,0,0
0,1,1,1,0 => 0,0,0,0,0
And their inverse:
1,0,1 => 1,1,1
1,0,0,1 => 1,1,1,1
1,0,0,0,1 => 1,1,1,1,1
The code is clearly legible and easy to understand, however I believe its hugely inefficient and I'm looking to improve the code to. I'm thinking of converting the list into a string and using substring to identify and replace the string sequences and then convert it back into a list once done.
Would this be a more appropriate design pattern to use in this case? The number of sequences to match could potentially increase in the future. Also, by using substring to match I should be able to eliminate the need to consider indexes out of bounds of the list (since I'm iterating over i, i+1, i+2 etc).
Also, is there anyway to approximate the time complexity for the individual iterators vs the substring method?
Aucun commentaire:
Enregistrer un commentaire