1

Topic: [Python] algorithm Implementation is_subsequence

Algorithm of check, whether is x a subsequence y, on java looks so: public static boolean isSubsequence (String x, String y) {int j = 0; for (int i = 0; i <y.length () && j <x.length (); i ++) if (x.charAt (j) == y.charAt (i)) j ++; return j == x.length ();} As laconic  on a python offer the such: def is_subsequence (x, y): it = iter (y) return all (c in it for c (x) I do not understand how it works. In my opinion, for the first c from x the iterator it should be rewound in the end (if c is not present in y),  c in it will be False for all subsequent c characters from x, all returns False for (almost all inputs. Experiments: it = iter (' abc ') list (it) # [' a ', ' b ', ' c '] it = iter (' abc ') ' a ' in it #True list (it) # [' b ', ' c '] it = iter (' abc ') ' b ' in it #True list (it) # [' c '] it = iter (' abc ') ' c ' in it #True list (it) # [] it = iter (' abc ') ' e ' in it #False list (it) # [] That I understand not so?

2

Re: [Python] algorithm Implementation is_subsequence

Hello, PrelaunchCalibration, you wrote: PC> Algorithm of check, whether is x substring y, on java looks so: Only not substring, and a subsequence. PC> I do not understand, how it works. In my opinion, for the first c from x the iterator it should be rewound in the end (if c is not present in y),  c in it will be False for all subsequent c characters from x, all returns False for (almost all inputs. The behavior at you all is truly described. all will return False for all inputs at which property "x - a subsequence y" is broken. Well and to return True for pairs x, y which possess this property. PC> that I understand not so? And you precisely do not confuse substring and a subsequence? smile

3

Re: [Python] algorithm Implementation is_subsequence

Hello, watchmaker, you wrote: W> And you precisely do not confuse substring and a subsequence? I was sealed up. It is a question of subsequences. It seems, I understood. Function all is used for that determination, what iterator empty (i.e. produced all that value, what it iterates)?

4

Re: [Python] algorithm Implementation is_subsequence

Hello, PrelaunchCalibration, you wrote: PC> It seems, I understood. PC> function all is used for that determination, what iterator empty (i.e. produced all that value, what it iterates)? Here something strange is written smile Expression return all (c in it for c in x) is equivalent to such imperative code: for c in x: if not (c in it): return False return True That's it it also is checked.

5

Re: [Python] algorithm Implementation is_subsequence

Hello, Kodt, you wrote: Hello, watchmaker, you wrote: W>> Here something strange is written smile Anything strange! This comment not to the code, and to the quoted fragment of the answer. There it is written . And with the code everything is all right.