2

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

0

5 Answers 5

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Please add: xi = [i for i, c in enumerate(a) if c == 'X']
5

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)

4 Comments

solution is awesome please add some explanation
Considering a and b have same length, first we will iterate both of that with zip. Using zip you can iterate multiple iterators. While checking the strings, make a set of elements of b it the element of a with same index has 'x'. Now obviously the set will make every element unique, so if position 'x' has same character then the set will be only one unique char. The length is 1. If they have more than one number then the set length will be more than 1.
this is the best answer here
Now you just need to not assume that there are any X.
3

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)

1 Comment

@Heisenberg You're welcome!
1

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)

Comments

1

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)

1 Comment

@Heisenberg. Anytime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.