Reputation: 383
I am trying to find out if given a string if 2 target characters follow one another. So essentially, I am trying to find if a character and its neighbor are target characters. How should I go about this? The following is what I have tried so far.
target_list=["t","a","r","g","e","t"]
for char in some_string:
if (char and some_string[some_string.index(char)+1]) in target_list:
print ("correct")
else:
print ("incorrect")
Expected output:
Upvotes: 2
Views: 1777
Reputation: 9
def judge(some_string):
for index in range(0,len(some_string)-1):
if some_string[index] in target_list and some_string[index+1] in target_list:
print("correct")
break
else:
print("incorrect")
judge("heytr") --correct
judge("hyt") -- incorrect
judge("heyt") --incorrect
For loop iterates on all character of the string checks if i and i+1 is present in target.if yes prints out correct. after all iterations of loop is over if no two character are found in target it comes to the else part of for loop and prints out incorrect.
Upvotes: -1
Reputation: 26335
You can also use any()
and zip()
for this:
target_list=["t","a","r","g","e","t"]
target_list = set(target_list)
some_strings = ["heytr", "hyt", "heyt"]
def match_strings(string, target_list):
return any(x in target_list and y in target_list for x, y in zip(string, string[1:]))
for some_string in some_strings:
if match_strings(some_string, target_list):
print("correct")
else:
print("incorrect")
Which Outputs:
correct
incorrect
incorrect
Logic of above code:
target_list
to a set, since set lookup is constant time. If you keep it as a list, then lookup time is linear. zip()
to create pairs of the current element and the next element, and checks if both of these elements exist in target_list
. Then checks if any()
of the pairs exist, which returns True
if any of the pairs exist, and False
if none exist. some_strings
, and check the above for each one. Upvotes: 0
Reputation: 8597
A regular expression solution.
import re
target_chars='target'
p = re.compile('[%s]{2}' % re.escape(target_chars))
m = p.search(some_string)
if m:
print('correct')
else:
print('incorrect')
Upvotes: 1
Reputation: 363616
from itertools import tee
def rolling_window(iterable, n=2):
iterators = tee(iterable, n)
for i, iterator in enumerate(iterators):
for skip in range(i):
next(iterator, None)
return zip(*iterators)
def match(some_string, target, n=2):
target = set(target)
return any(target.issuperset(s) for s in rolling_window(some_string, n=n))
Upvotes: 1
Reputation: 183
Just use map:
target_list=['t','a','r','g','e']
def toDict(list):
mp = {}
for c in list:
mp[c] = True
return mp
d = toDict(target_list)
print("dict:" , d)
def check(string, mp):
count = 0
for c in string:
if(mp.get(c,False)):
count = count+1
if(count > 1):
return True
else:
count = 0
return False
print("check:" , check("heytr", d))
print("check:" , check("hyt", d))
print("check:" , check("heyt", d))
Upvotes: 1
Reputation: 37327
Just go through the indices and process every pair of characters:
for i in range(len(some_string) - 1):
if some_string[i] in target_list and some_string[i+1] in target_list:
print ("correct")
break
if i == len(some_string) - 1:
print ("incorrect")
You can alternatively create a mapping and look for adjacent true positives:
m = [(char in target_list) for char in some_string]
for i in range(len(m) - 1):
if m[i] and m[i+1]:
print ("correct")
break
if i == len(m) - 1:
print ("incorrect")
Upvotes: 1