Four Substrings to Matrix

 Four Substrings to Matrix 

PROBLEM STATEMENT :

The program must accept N string values of equal length L as the input. The program must divide each string into four substrings of equal length. Then the program must form a character matrix of size R*C, where R = (N*3) and C = (L/4)*3. Then the program must fill the character matrix based on the following conditions. 

- The top-left, top-right, bottom-left, bottom-right and middle submatrices of size (R/3)*(C/3) must be filled with the asterisks. 

- The top-middle submatrix of size (R/3)*(C/3) must be filled with the 1st substrings of the N string values. 

- The middle-right submatrix of size (R/3)*(C/3) must be filled with the 2nd substrings of the N string values. 

- The bottom-middle submatrix of size (R/3)*(C/3) must be filled with the 3rd substrings of the N string values. 

- The middle-left submatrix of size (R/3)*(C/3) must be filled with the 4th substrings of the N string values. 

Finally, the program must print the R*C character matrix as the output. 

Note: The value of L is always divisible by 4. 

Boundary Condition(s): 

1 <= N <= 50 

4 <= L <= 100 

Input Format: 

The first line contains N. 

The next N lines, each contains a string. 

Output Format: 

The first R lines, each contains C characters representing the R*C character matrix. 

Example Input/Output 1: 
Input: () 
4 
abcdefghijklmnop 
ABCDEFGHIJKLMNOP 
1234567891011121 
zyxwvutsrqponmlk 
Output: ****abcd**** ****ABCD**** ****1234**** ****zyxw**** mnop****efgh MNOP****EFGH 1121****5678 nmlk****vuts ****ijkl**** ****IJKL**** ****9101**** ****rqpo****

Explanation: 

Here N = 4 and L = 16

The size of the character matrix to be formed is 12*12 (where R = 4*3 and C = (16/4)*3). 

Then the matrix is filled with the characters based on the given conditions. 

Example Input/Output 2: 
Input: () 
7 
interest 
absolute 
complete 
accurate 
delivery 
achieved 
dominant 
Output: **in** **ab** **co** **ac** **de** **ac** **do** st**te te**so te**mp te**cu ry**li ed**hi nt**mi **re** **lu** **le** **ra** **ve** **ev** **na**



SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int n,i,j,m; scanf("%d\n",&n); char s[n][101]; for(i=0;i<n;i++) scanf("%s\n",s[i]); m=strlen(s[0])/4; for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("*"); for(j=0;j<m;j++) printf("%c",s[i][j]); for(j=0;j<m;j++) printf("*"); printf("\n"); } for(i=0;i<n;i++) { for(j=m*3;j<m*4;j++) printf("%c",s[i][j]); for(j=0;j<m;j++) printf("*"); for(j=m;j<m*2;j++) printf("%c",s[i][j]); printf("\n"); } for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("*"); for(j=m*2;j<m*3;j++) printf("%c",s[i][j]); for(j=0;j<m;j++) printf("*"); printf("\n"); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int n ; cin >> n; string arr[n]; for(int i = 0 ; i<n ; i++) cin >> arr[i]; int len = arr[0].size() , r = n*3 , c = (len/4)*3 , t = len/4; string star(t , '*'); for(int j = 0 ; j<n ; j++) { cout << star; cout << arr[j].substr(0,t); arr[j] = arr[j].substr(t); cout << star << endl; } for(int i = 0 ; i<n ; i++) { cout << arr[i].substr(arr[i].size()-t); arr[i] = arr[i].substr(0,arr[i].size()-t); cout << star; cout << arr[i].substr(0,t) << endl; arr[i] = arr[i].substr(t); } for(int i = 0 ; i<n ; i++) { cout << star; cout << arr[i].substr(0,t); cout << star << endl; } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt();sc.nextLine(); String s=sc.nextLine(); int len=s.length()/4; String[][] str=new String[N][4]; str[0][0]=s.substring(0,len); str[0][1]=s.substring(len,len*2); str[0][2]=s.substring(len*2,len*3); str[0][3]=s.substring(len*3,len*4); for(int i=1;i<N;i++) { String t=sc.nextLine(); str[i][0]=t.substring(0,len); str[i][1]=t.substring(len,len*2); str[i][2]=t.substring(len*2,len*3); str[i][3]=t.substring(len*3,len*4); } for(int i=0;i<N;i++) { for(int j=0;j<len;j++) System.out.print("*"); System.out.print(str[i][0]); for(int j=0;j<len;j++) System.out.print("*"); System.out.println(); } for(int i=0;i<N;i++) { System.out.print(str[i][3]); for(int j=0;j<len;j++) System.out.print("*"); System.out.print(str[i][1]); System.out.println(); } for(int i=0;i<N;i++) { for(int j=0;j<len;j++) System.out.print("*"); System.out.print(str[i][2]); for(int j=0;j<len;j++) System.out.print("*"); System.out.println(); } } }



PYTHON

       

n=int(input()) l=[input().strip() for i in range(n)] k=len(l[0])//4 for i in range(n): l[i]=[l[i][:k],l[i][k:2*k],l[i][2*k:3*k],l[i][3*k:4*k]] res=[] for i in range(n): res.append(('*'*k)+l[i][0]+('*'*k)) for i in range(n): res.append(l[i][3]+('*'*k)+l[i][1]) for i in range(n): res.append(('*'*k)+l[i][2]+('*'*k)) for i in res: print(i)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right