Mixed Matrix - Integers & Characters

 Mixed Matrix - Integers & Characters 

 Problem Statement: 
The program must accept a matrix of size R*C containing integers and characters as the input. The program must print the sum of all the integers in the given matrix. Then the program must print a string representing the characters in the given matrix. The string must be formed by traversing the columns of the matrix from left to right, where each column must be traversed from top to bottom.
Note: At least one character is always present in the given matrix.

Boundary Condition(s):
2 <= R, C <=50
1 <= Values of each integer in the matrix <=1000

Input Format:
The first line contains R and C separated by a space.
The next R lines, each contains C values (integers and characters) separated by a space.

Output Format:
The first line contains an integer representing the sum of all the integers in the matrix.
The second line contains a string representing all the characters in the matrix based on the given conditions

 Example Input/Output 1: 
 Input: 
4 5
50 j 2 41 7
p 47 o 30 e
k 41 3 42 r
75 c d 79 s

 Output: 
417
pkjcoders

Explanation:
The sum of the integers in the given matrix is 417(50 + 2 + 41 + 7 + 47 + 30 + 41 + 3 + 42 + 75 + 79).
The string formed using the characters in the given matrix is pkjcoders.

Example Input/Output 2:
Input:
7 6
31 59 76 66 88 37
1 31 28 83 e 12
27 m u 84 72 8
56 86 63 t 76 55
C 62 25 40 83 r
45 p 1 26 10 70
o 50 31 1 45 88

Output:
1616
Computer

Example Input/Output 3:
Input:
3 3
a b c
d e f
# G H

Output:
0
ad#beGcfH

 Note: Max Execution Time Limit: 50 millisecs 

 Solution: 

 Programming Language: Python 3 Language 

a,c=list(map(int,input().split()))
b=[]
for i in range(0,a):
    b.append(input().split())
d=0
e=""
for i in range(0,c):
    for j in range(0,a):
        if b[j][i].isdigit():
            d+=int(b[j][i])
        else:
            e+=b[j][i]
print(d,"\n",e)

Alter:

r,c=map(int,input().split())
matrix=[list(input().split()) for _ in range(r)]
tot,reqStr=0,''
for i in matrix:
    for j in i:
        if j.isdigit():tot+=int(j)
for col in range(c):
    for row in range(r):
        if matrix[row][col].isdigit()==False:
            reqStr+=matrix[row][col]
print(tot,reqStr,sep="\n")

 Programming Language: C Language 

#include<stdio.h>
#include<stdlib.h>
int main() {
    int R, C, i = 0, j, ans = 0, c = 0; scanf("%d%d\n", &R, &C);
    char mat[R][C][4], s[R * C];
    while(~scanf("%s", mat[i / C][i % C]))
        if(isdigit(*mat[i / C][i++ % C]))
            ans += atoi(mat[--i / C][i++ % C]);
    for(j = 0; j < C; j++)
        for(i = 0; i < R; i++)
            if(!isdigit(*mat[i][j]))
                s[c++] = *mat[i][j];
    s[c] = '\0';
    printf("%d\n%s", ans, s);
}

Alter:

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

 Programming Language: C++ Language 

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv){
int r,c;
cin>>r>>c;
int sum =0;
vector<string> sAns(c,"");
for(int i=0;i<r;i++){
    for(int j=0;j<c;j++){
        string temp;
        cin>>temp;
        try{
            int ans = stoi(temp);
            sum+=ans;
        }catch(...){
            sAns[j]+=temp;
        }
    }
}
cout<<sum<<'\n';
for(string i:sAns){
    cout<<i;
}
}

 Programming Language: JAVA Language 

import java.util.*;
public class Hello {
    public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int r=s.nextInt();
		int c=s.nextInt();
		String[][] mat=new String[r][c];
		for(int i=0;i<r;i++) {
		    for(int j=0;j<c;j++) {
		        mat[i][j]=s.next();
		    }
		}
		String str="";
		int a=0;
		for(int j=0;j<c;j++){
		    for(int i=0;i<r;i++){
		        if(Character.isDigit(mat[i][j].charAt(0)))
	                a+=Integer.parseInt(mat[i][j]);
                else
                    str+=mat[i][j];
		    }
		}
		System.out.println(a);
		System.out.print(str);
	}
}

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 


Comments

Popular Posts

Array Elements - Divisible by D

Move Hundreds to End

Unique Characters

Function printArrayElementsInReverse

Sort Number By Unit and Tenth Digits

Most Vowels in Row