I want to use Mathematica to solve the following problem:
For example I have a sequence 101. I want to compare it with $1101$ and $0101$. The comparison has the following procedure:
- Check the first term of $101$ is $1$ or $0$. If it is $1$, compare $101$ to $1101$, term by term; If it is $0$, compare $101$ to $0101$. Stop the process before the first term they are different, or all the terms of $101$ have been compared without stopping the process, and report all the terms that have been compared.
In our case, the first term of $101$ is $1$, so we compare it with $1101$. Then, $101$ and $1101$ only has one term in common, the first term $1$. So the program should report $1$, and go to next step.
- Recording the remaining sequence of $101$.
In our case, as only $1$ is reported, the remaining sequence is $01$.
- Restart the process. Check the first term of $01$ is $0$ or $1$. If it $0$, compare $01$ with $0101$, if it is $1$, compare $01$ with $1101$. Stop the process before the first term they are different, or the sequence of $01$ has been run out. Report all the timers that have been compared.
*In our case, the first term of $01$ is $0$, so we compare $01$ with $0101$. Then the first two terms agree, and then $01$ ran out. The program should report $01$, and then stop.
- Repeating the process again and again until there is no remaining sequence.
I tried to use the commend "If" to write this but it did not work, since I did not know how to let Mathematica to "remember" what has been compared.
Then, I tried to use commend "Order" and "Sort", but it seems that I need to program a comparison function.
Is there anyway for me to achieve this using Mathematica? Thank you!