There is a two input first input represent the index of the second input.I need to find the 'X' position of second input are same or not?
input:
123X56XX
Ahjutruu
Output:
True
Example: X in input one at 4,7,8
position of 4,7,8 in second input is 'u' all are same so i need to print "true"
I tried using enumerate function but not came:
a="123X56XX"
b="Ahjutruu"
xi=[]
for i,val in enumerate(a):
if val=='X':
xi.append(i)
print(xi)
#next step only i dont know how to doo please help
Next i dont know how to check
CodePudding user response:
Put all values of given indexes of xi
in set
, the length of this set must equal to 1:
xi = [i for i, c in enumerate(a) if c == 'X']
len(set([b[i] for i in xi])) == 1
CodePudding user response:
you can do like this
a = "123X56XX"
b = "Ahjutruu"
status = len({j for i,j in zip(a,b) if i=='X'}) == 1
print(status)
CodePudding user response:
An optional solution:
a="123X56XX"
b="Ahjutruu"
c = list([b[i] for i in range(len(a)) if a[i] == "X"]) # a sublist of the characters from b that correspond to "X" in a
s = set(c) # a set with the elements of c; a set has no duplicated elements, so if all of c's items are the same, s will only have one element
print(len(s) == 1)
CodePudding user response:
the logic is added for matching the X places in a
a = "123X56XX"
b = "Ahjutruu"
xi = []
allSame = True
for i, val in enumerate(a):
if val == 'X':
xi.append(i)#saving the indices
allSame = len(a) == len(b) # length must be same
prev = None
for v in xi:
if not allSame:
break
if prev is None:
prev = b[v] # if previous value is not set setting it
else:
allSame = prev == b[v] # matching with previous value in b's list
print(allSame)
CodePudding user response:
Minor addition to your code.
a="123X56XX"
b="Ahjutruu"
xi=[]
result = False
for i,val in enumerate(a):
if val=='X':
xi.append(i)
print(xi[0])
for index, value in enumerate(xi):
if index < len(xi) - 1:
if b[value] == b[xi[index 1]]:
result = True
else:
result = False
print(result)