-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix_script.dart
88 lines (74 loc) · 1.65 KB
/
matrix_script.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//https://www.hackerrank.com/challenges/matrix-script/problem
import 'dart:core';
void main() {
// List<String> matrix = [
// //5 9
// '#%\$r%r\$n ',
// 'O%Mi\$iTi\$',
// 'yiaxsprt ',
// 'est%ctiy#',
// ' t c i %',
// ];
//Output: #Oye is Mattrix sccript Triinity $ #%
List<String> matrix = [
//7 3
'Tsi',
'h%x',
'i #',
'sM ',
'\$a ',
'#t%',
'ir!',
];
//Output: This is Matrix# %!
// List<String> matrix = [
// //4,6
// 'T%Mic&',
// 'h%axr%',
// 'iit#p!',
// 'ssrst&'
// ];
//output: This isMatrix scrpt&%!&
// List<String> matrix =[
// ///4 6
// '#%\$r%r',
// 'I%Mi\$i',
// 'tiaxsp',
// '#st%ct',
// ];
// //Output: #It is Matrix script
//Input
//4 8
// List<String> matrix = [
// '#%\$r%r\$n',
// 'I%Mi\$iTi',
// 'tiaxsprt',
// '#st%ctiy',
// ];
// //Output: #It is Matrix script Trinity
int row = 7, column = 3;
print(matrixScript(row, column, matrix));
}
String matrixScript(int rows, int column, List<String> matrix) {
// Rotate the matrix
List<List<String>> rotatedMatrix =
List.generate(column, (i) => List.filled(rows, ''));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
rotatedMatrix[j][i] = matrix[i][j];
}
}
// Flatten the rotated matrix into a single string
StringBuffer sample = StringBuffer();
for (List<String> subset in rotatedMatrix) {
for (String letter in subset) {
sample.write(letter);
}
}
print(sample.toString());
// Replace invalid characters with a space
String result = sample
.toString()
.replaceAllMapped(RegExp(r'(?<=\w)([^\d\w]+)(?=\w)'), (match) => ' ');
return result;
}