String Matrix to Character Matrix

 String Matrix to Character Matrix 

PROBLEM STATEMENT :

The program must accept a matrix of size RxC containing only words as the input. All words in the matrix have the same length as 9. The program must form a character matrix of size (R*3)x(C*3) by expanding the each word to a 3*3 character matrix based on the following conditions. - The 1st row of the 3*3 matrix must be formed using the first 3 characters of the word. - The 2nd row of the 3*3 matrix must be formed using the middle 3 characters of the word. - The 3rd row of the 3*3 matrix must be formed using the last 3 characters of the word. Finally, the program must print the character matrix as the output. 

Boundary Condition(s): 

1 <= R, C <= 50 

Input Format: 

The first line contains R and C separated by a space. 

The next R lines, each contains C words separated by a space. 

Output Format: 

The first R*3 lines, each contains C*3 characters separated by a space. 

Example Input/Output 1: 
Input: () 
3 3 
skillrack chocolate IMPORTANT 
Knowledge Seventeen vegetable 
Crocodile nutrition Fireboard 
Output: s k i c h o I M P l l r c o l O R T a c k a t e A N T K n o S e v v e g w l e e n t e t a d g e e e n b l e C r o n u t F i r c o d r i t e b o i l e i o n a r d

Explanation: 

Here R = 3 and C = 3. 

The word skillrack is expanded to a 3x3 character matrix as given below. 

s k i 
l l r 
a c k 

The word chocolate is expanded to a 3x3 character matrix as given below. 

c h o 
c o l 
a t e 

Similarly the remaining words are expanded to 3x3 character matrices. 

Hence the output is 

s k i c h o I M P 
l l r c o l O R T 
a c k a t e A N T 
K n o S e v v e g 
w l e e n t e t a 
d g e e e n b l e 
C r o n u t F i r 
c o d r i t e b o 
i l e i o n a r d 

Example Input/Output 2: 
Input: () 
3 4 
Challenge irregular PINEAPPLE halloween 
affection NUTRITION Crocodile beginning 
CONFUSION celebrity Delicious LIGHTNING 
Output: C h a i r r P I N h a l l l e e g u E A P l o w n g e l a r P L E e e n a f f N U T C r o b e g e c t R I T c o d i n n i o n I O N i l e i n g C O N c e l D e l L I G F U S e b r i c i H T N I O N i t y o u s I N G




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int i,j; scanf("%d %d\n",&i,&j); char a[i][j][9]; for(int p=0;p<i;p++,scanf("\n")) { for(int r=0;r<j;r++) { scanf("%s ",a[p][r]); } } for(int p=0;p<i;p++) { for(int s=0;s<9;s+=3) { for(int r=0;r<j;r++) { for(int c=s;c<s+3;c++) { printf("%c ",a[p][r][c]); } } printf("\n"); } } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int i,j; cin>>i>>j; char a[i][j][9]; for(int p=0;p<i;p++,scanf("\n")) { for(int r=0;r<j;r++) { cin>>a[p][r]; } } for(int p=0;p<i;p++) { for(int s=0;s<9;s+=3) { for(int r=0;r<j;r++) { for(int c=s;c<s+3;c++) { cout<<a[p][r][c]<<" "; } } cout<<"\n"; } } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(),C=sc.nextInt(); String[][] matrix=new String[R][C]; for(int row=0;row<R;row++){ for(int col=0;col<C;col++){ matrix[row][col]=sc.next(); } } for(int row=0;row<R;row++){ for(int ind=0;ind<9;ind+=3){ for(int col=0;col<C;col++){ for(int currInd=ind;currInd<ind+3;currInd++){ System.out.print(matrix[row][col].charAt(currInd)+" "); } } System.out.print("\n"); } } } }



PYTHON

       

r,c=map(int,input().split()) m=[input().split() for _ in range(r)] for i in range(r): for k in range(0,9,3): for j in range(c): print(*m[i][j][k:k+3],end=' ') print()



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