Expand a given string with "spaces" and "swaps"
Step 1: Expand with "spaces" from the center towards the start/end of the string in each step.
i) string with no middle character adds a middle single space
e.g. ""BATA" " becomes "BA TA" and becomes “B A T A”
ii) string with middle character adds left and right space. A space is introduced before and after a 'character' or a 'substring'.
e.g. ""BATIA" " becomes "BA T IA" and becomes "B A T I A"
Step 2: Once space is added in each step then it is followed by character swap (swap between character after left space and character before right space). Swap happens and continues between all the opposite characters till the center (space or middle character) of the string is reached.
e.g.
"BA TA" becomes "BA TA" (swap for middle space will be same)
"B A T A" becomes "B T A A" (swap between A and T, followed by continuous swap till the middle empty space)
e.g.
"BA T IA" becomes "BA T IA" (swap middle character T will be in the same position)
"B A T I A" becomes “B I T A A” (swap between A and I, followed by continuous swap till the middle character ‘T’)
Step 3: Print the output
Step 4: Iterate from Step 1
All the intermediate outputs should be printed as below.
Example 1:
Input:
BATA
Output:
BA TA # addition of space in middle, followed by swap in middle space
B T A A # addition of space (before A and after T) followed by swap of 'T' and 'A' followed by a swap for middle space.
Example 2:
Input:
BATIA
Output:
BA T IA
B I T A A
Example 3:
Input:
CAPITOL
Outputs:
CAP I TOL # addition of space(before and after I) swaps 'I' which has no change since it is at the center
CA T I P OL # addition of space(before T and after P) swaps 'P' and 'T'. Followed by a swap of 'I.
C O P I T A L # addition of space(before A and after O) swaps 'A' and 'O'. Followed by a swap of 'T' and 'P'. Followed by a swap of 'I'. (swap till the middle is reached)
Example 4:
Input:
CARDBOARD
Output:
CARD B OARD
CAR O B D ARD
CA A D B O R RD
C R R O B D A A D
Example 5:
Input:
BATAMA
Output:
BAT AMA
BA A T MA
B M T A A A
Program
import math
input_str = ['BATIA']
new_array = []
new_array.append(letter)
print(new_array)
print("Odd input string, need not to add space in middle")
quot = len(input_str) / 2
indices = quot + 1
middle_index = math.floor(len(input_str[0]) / 2)
print(middle_index)
print("After adding space", new_array)
# CARDBOARD = CARD B OARD after exchange
# CAR O B D ARD
# CA A O B D R RD
# C R A O B D R A D
left_index = middle_index - 1
for index in range(middle_index - 1):
print("chars from new array", new_array[right_index], new_array[left_index])
tmp_letter = new_array[right_index]
new_array[right_index] = new_array[left_index]
new_array[left_index] = tmp_letter
print("Swapping character ", new_array)
right_index += 1
left_index -= 1
print("Final output after swapping", new_array)
middle_index = math.floor(len(input_str[0]) / 2)
right_index = middle_index + 1
left_index = middle_index - 1
for index in range(middle_index):
new_array.insert(right_index, ' ')
new_array.insert(left_index + 1, ' ')
print("After adding space ", new_array)
new_middle_index = math.floor(len(new_array) / 2)
right_index += 3
left_index -= 1