Friday, August 27, 2021

Program to expand given string with spaces and swaps (Assessment Test)

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

import numpy as np

#input_str = ['CARDBOARD']
input_str = ['BATIA']
new_array = []

for letter in input_str[0]:
    new_array.append(letter)
print(new_array)

if len(input_str) % 2 != 0:
   print("Odd input string, need not to add space in middle")
quot = len(input_str) / 2
indices = quot + 1

#print("Mid character is ", quot, input_str[indices]);
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

right_index = middle_index + 1
left_index = middle_index - 1

# Swap the characters from middle
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

# Add spaces before and after the chars from middle
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

Output

['B', 'A', 'T', 'I', 'A']
Odd input string, need not to add space in middle
2
After adding space ['B', 'A', 'T', 'I', 'A']
chars from new array I A
Swapping character  ['B', 'I', 'T', 'A', 'A']
Final output after swapping ['B', 'I', 'T', 'A', 'A']
After adding space  ['B', 'I', ' ', 'T', ' ', 'A', 'A']
After adding space  ['B', ' ', 'I', ' ', 'T', ' ', 'A', ' ', 'A']

Program to rearrange words in a sentence (Assessment Test)

 

Rearrange words

Requirement

1) Arrange the words in alphabetical order based on only the "first" character (not the second character and further characters). 

2) If two words are starting with the same character then the same order of the words should be maintained from the original sentence. 

Constraints for this problem

    Use char[] or Character[] array for the input and output. (e.g., avoid in-built java.lang.String or similar class to represent 'string', instead use char[] to solve the problem). 

    e.g.,  Java      :    char input[] = "The domestic dog is a domesticated form of wolf".toCharArray()
             Python      :    input = list('The domestic dog is a domesticated form of wolf')
             Javascript  :   input = Array.from('hello')

Example 1:

Input    :  this is an impossible task

Output :  an is impossible this task

Explanation:

       'a' is the lowest, followed by 'i' and 't' character.
       an - first word because of 'a'
       'is impossible' - 'is' comes before 'impossible' since 'is' appears in the orginial sentence before 'impossible'
       'this task' - 'this' comes before 'task' since 'this' appears in the orginial sentence before 'task'.

Program to rearrange words in a sentence

input_str = "this is an impossible task"

#output = "an is impossible this task"

def sort_strings(list):
    flag = 0
    for index in range(len(arbitrary_list) - 1):  
        if arbitrary_list[index][0] > arbitrary_list[index + 1][0]:
            tmp_word = arbitrary_list[index]
            arbitrary_list[index] = arbitrary_list[index + 1]
            arbitrary_list[index + 1] = tmp_word
            flag = 1
    return flag

arbitrary_list = []

new_word = ""

for letter in input_str:   #the domestic
    if letter.isspace():   # 't', 'h', 'e', ' ', 'd' ..... etc
       print("Space word ... Add new_word to list and comma sep")
       arbitrary_list.append(new_word)  #the
       new_word = ""
    else:
       new_word = new_word + letter   # new_word = 't' + 'h' + 'e'

if new_word:
   arbitrary_list.append(new_word)    

return_value = sort_strings(arbitrary_list)

while sort_strings(arbitrary_list):
   print("Continue sorting ... ")
else:
   print("Sorted list is", arbitrary_list)

Output

Sorted list is ['an', 'is', 'impossible', 'this', 'task']

Tuesday, August 24, 2021

Technical Support for Assessment Tests

 

Tech Support for Assessment Tests


IT companies conduct assessment exams (e.g CoCubes) or similar online tests to filter out the students

It would be difficult for most of the students to clear the "technical part" of the exam

We do provide "Technical Support" to students to clear CoCubes or similar online tests