@mtf/

needle-haystack found string in string

Python

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.13547399421154327","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"CmRlZiBjb250YWlucyhoYXlzdGFjaywgbmVlZGxlKToKICAgIG4gPSBsZW4obmVlZGxlKQogICAgZiA9IG5lZWRsZVswXQogICAgZm9yIGksIHggaW4gZW51bWVyYXRlKGhheXN0YWNrKToKICAgICAgICBpZiB4ID09IGY6CiAgICAgICAgICAgIGlmIGhheXN0YWNrW2k6biArIGldID09IG5lZWRsZToKICAgICAgICAgICAgICAgIHJldHVybiBUcnVlCiAgICByZXR1cm4gRmFsc2UKCnByaW50IChjb250YWlucygnc3VwZXJpbXBvc2VkJywgJ2ltcG9zZScpKSAgICAjIFRydWUKCmRlZiBpbmRpY2VzKGhheXN0YWNrLCBuZWVkbGUpOgogICAgbiA9IGxlbihuZWVkbGUpCiAgICBmID0gbmVlZGxlWzBdCiAgICBzID0gW10KICAgIGZvciBpLCB4IGluIGVudW1lcmF0ZShoYXlzdGFjayk6CiAgICAgICAgaWYgeCA9PSBmOgogICAgICAgICAgICBpZiBoYXlzdGFja1tpOm4gKyBpXSA9PSBuZWVkbGU6CiAgICAgICAgICAgICAgICBzLmFwcGVuZChpKQogICAgcmV0dXJuIHMKCnByaW50IChpbmRpY2VzKCdzdXBlcmltcG9zZWQgc2VsZi1pbXBvc2VkJywgJ2ltcG9zZScpKQoKZGVmIGhhcyhoYXlzdGFjaywgbmVlZGxlKToKICAgIG4gPSBsZW4obmVlZGxlKQogICAgZiA9IG5lZWRsZVswXQogICAgcyA9IFtdCiAgICBmb3IgaSwgeCBpbiBlbnVtZXJhdGUoaGF5c3RhY2spOgogICAgICAgIGlmIHggPT0gZjoKICAgICAgICAgICAgaWYgaGF5c3RhY2tbaTpuICsgaV0gPT0gbmVlZGxlOgogICAgICAgICAgICAgICAgcy5hcHBlbmQoaSkKICAgIHJldH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sZVswXQogIHIgPSBGYWxzZQogIHMgPSBbXQogIGZvciBpIGluIGg6CiAgICBpZiBoYXlzdGFja1tpXSA9PSBmOgogICAgICBmb3IgaiBpbiBrOgogICAgICAgIGlmIGhheXN0YWNrW2kgKyBqXSA9PSBuZWVkbGVbal06CiAgICAgICAgICByID0gVHJ1ZQogICAgICAgICAgY29udGludWUKICAgICAgICBlbHNlOgogICAgICAgICAgciA9IEZhbHNlCiAgICAgICAgICBicmVhawogICAgICBpZiByOgogICAgICAgIHMuYXBwZW5kKGkpCiAgICAgICAgCiAgcmV0dXJuIHMgb3IgcgoKcHJpbnQgKGhhc19hdCgnc3VwZXJpbXBvc2VkIHNlbGYtaW1wb3NlZCBpbXBvc2UnLCAnaW1wb3NlJykpCnByaW50IChoYXNfYXQoJ3N1cGVyaW1wb3NlZCBzZWxmLWltcG9zZWQgaW1wb3NlJywgJ2ltcG9zdGVyJykpCnByaW50IChoYXNfYXQoJ3N1cGVyaW1wb3NlZCBzZWxmLWltcG9zZWQgaW1wb3NlJywgJ3N1cGVyJykpCgo="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def contains(haystack, needle):
    n = len(needle)
    f = needle[0]
    for i, x in enumerate(haystack):
        if x == f:
            if haystack[i:n + i] == needle:
                return True
    return False

print (contains('superimposed', 'impose'))    # True

def indices(haystack, needle):
    n = len(needle)
    f = needle[0]
    s = []
    for i, x in enumerate(haystack):
        if x == f:
            if haystack[i:n + i] == needle:
                s.append(i)
    return s

print (indices('superimposed self-imposed', 'impose'))

def has(haystack, needle):
    n = len(needle)
    f = needle[0]
    s = []
    for i, x in enumerate(haystack):
        if x == f:
            if haystack[i:n + i] == needle:
                s.append(i)
    return s or False

print (has('superimposed self-imposed', 'impose'))
print (has('superimposed self-imposed', 'imposter'))
s = has('superimposed self-imposed', 'impose')
print (s and True)
s = has('superimposed self-imposed', 'imposter')
print (s and True)

# without slicing or enumerate
def has_in(haystack, needle):
  k = range(len(needle))
  h = range(len(haystack))
  f = needle[0]
  r = False
  for i in h:
    if haystack[i] == f:
      for j in k:
        if haystack[i + j] == needle[j]:
          r = True
          continue
        else:
          r = False
          break
      if r: return r
  return r

print (has_in('superimposed self-imposed', 'impose'))
print (has_in('superimposed self-imposed', 'imposter'))

def has_at(haystack, needle):
  k = range(len(needle))
  h = range(len(haystack) - k[-1])
  f = needle[0]
  r = False
  s = []
  for i in h:
    if haystack[i] == f:
      for j in k:
        if haystack[i + j] == needle[j]:
          r = True
          continue
        else:
          r = False
          break
      if r:
        s.append(i)
        
  return s or r

print (has_at('superimposed self-imposed impose', 'impose'))
print (has_at('superimposed self-imposed impose', 'imposter'))
print (has_at('superimposed self-imposed impose', 'super'))