human
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers.
To store the permutation.
For sequence 1 2 3. . ..
For sequence N , N - 1 , N - 2. . ..
Flag is used to alternate between the above if else statements.
If last element added was r + 1.
If last element added was l - 1.
Print the permutation.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers and the other steps required.
 def printPermutation ( N , K ) :
	res = list ( ) ;
	l , r , flag = 1 , N , 0
	for i in range ( K ) :
		if flag == False :
			res . append ( l )
			l += 1
		else :
			res . append ( r ) ;
			r -= 1 ;
		flag = flag ^ 1 ;
	if flag == False :
		for i in range ( r , 2 , - 1 ) :
			res . append ( i )
	else :
		for i in range ( l , r ) :
			res . append ( i )
	for i in res :
		print ( i , end = " ▁ " )
N , K = 10 , 4
printPermutation ( N , K )