human
What is a code to smallest number greater than X which is K and the other steps required given the following:
Smallest number greater than X which is K | Function to find the smallest K periodic integer greater than X.
Stores the number in a temporary string.
Set X [ i ] = X [ i - k ] for i > k.
Start from the current index.
Loop upto N change X [ j ] to X [ i ].
Return X if current Value is greater than original value.
Find the first digit not equal to 9.
Increment X [ i ].
Set POS to current index.
Change X [ i ] to 0 for all indices from POS + 1 to K.
Set X [ i ] = X [ i - k ] for i > k.
Loop upto N change X [ j ] to X [ i ].
Return the final string.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to smallest number greater than X which is K and the other steps required.
 def Kperiodicinteger ( X , N , K ) :
	X = list ( X )
	temp = X . copy ( )
	for i in range ( K ) :
		j = i
		while ( j < N ) :
			X [ j ] = X [ i ]
			j += K
	if ( X >= temp ) :
		return X
	POS = 0
	for i in range ( K - 1 , - 1 , - 1 ) :
		if ( X [ i ] != '9' ) :
			X [ i ] = str ( int ( X [ i ] ) + 1 )
			POS = i
			break
	for i in range ( POS + 1 , K ) :
		X [ i ] = '0'
	for i in range ( K ) :
		j = i
		while ( j < N ) :
			X [ j ] = X [ i ]
			j += K
	return X
N = 4
K = 2
X = "1215"
print ( * Kperiodicinteger ( X , N , K ) , sep = ' ' )