Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My quick attempt:</p> <pre><code>def count_subseqs(string, subseq): string = [c for c in string if c in subseq] count = i = 0 for c in string: if c == subseq[0]: pos = 1 for c2 in string[i+1:]: if c2 == subseq[pos]: pos += 1 if pos == len(subseq): count += 1 break i += 1 return count print count_subseqs(string='3141592653', subseq='123') </code></pre> <p><strong>Edit:</strong> This one should be correct also if <code>1223 == 2</code> and more complicated cases:</p> <pre><code>def count_subseqs(string, subseq): string = [c for c in string if c in subseq] i = 0 seqs = [] for c in string: if c == subseq[0]: pos = 1 seq = [1] for c2 in string[i + 1:]: if pos &gt; len(subseq): break if pos &lt; len(subseq) and c2 == subseq[pos]: try: seq[pos] += 1 except IndexError: seq.append(1) pos += 1 elif pos &gt; 1 and c2 == subseq[pos - 1]: seq[pos - 1] += 1 if len(seq) == len(subseq): seqs.append(seq) i += 1 return sum(reduce(lambda x, y: x * y, seq) for seq in seqs) assert count_subseqs(string='12', subseq='123') == 0 assert count_subseqs(string='1002', subseq='123') == 0 assert count_subseqs(string='0123', subseq='123') == 1 assert count_subseqs(string='0123', subseq='1230') == 0 assert count_subseqs(string='1223', subseq='123') == 2 assert count_subseqs(string='12223', subseq='123') == 3 assert count_subseqs(string='121323', subseq='123') == 3 assert count_subseqs(string='12233', subseq='123') == 4 assert count_subseqs(string='0123134', subseq='1234') == 2 assert count_subseqs(string='1221323', subseq='123') == 5 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload