human
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Python program to solve the above approach.
storing length.
variables for temporary strings of s and t.
final output variable.
if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k.
incrementing first by 2 , as said in problem statement.
if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k.
incrementing second by two as said in problem statement.
if k is smaller than y , then the character will be at position k % m ..
returning character at k index.
if k is smaller than x , then the character is at position k % n.
returning character at k index.
Driver code"
How can the above be solved in Python?
 ... Can you provide a code to find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times and the other steps required?
assistant
#Here is code to find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times and the other steps required.
 def KthCharacter ( s , t , k ) :
	n = len ( s )
	m = len ( t )
	first = 1
	second = 2
	output = ' ? '
	while ( k > 0 ) :
		if ( k > first * n ) :
			x = first * n
			k = k - x
			first = first + 2
			if ( k > second * m ) :
				y = second * m
				k = k - y
				second = second + 2
			else :
				ind = k % m
				ind -= 1
				if ( ind < 0 ) :
					ind = m - 1
				output = t [ ind ]
				break
		else :
			ind = k % n
			ind -= 1
			if ( ind < 0 ) :
				ind = n - 1
			output = s [ ind ]
			break
	return output
S1 = " a "
S2 = " bc "
k = 4
ans = KthCharacter ( S1 , S2 , k )
print ( ans )