-
Notifications
You must be signed in to change notification settings - Fork 0
/
11th.js
67 lines (51 loc) · 2.06 KB
/
11th.js
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
/* DESCRIPTION:
You are given a string of n lines, each substring being n characters long. For example:
s = "abcd\nefgh\nijkl\nmnop"
We will study the "horizontal" and the "vertical" scaling of this square of strings.
A k-horizontal scaling of a string consists of replicating k times each character of the string (except '\n').
Example: 2-horizontal scaling of s: => "aabbccdd\neeffgghh\niijjkkll\nmmnnoopp"
A v-vertical scaling of a string consists of replicating v times each part of the squared string.
Example: 2-vertical scaling of s: => "abcd\nabcd\nefgh\nefgh\nijkl\nijkl\nmnop\nmnop"
Function scale(strng, k, v) will perform a k-horizontal scaling and a v-vertical scaling.
Example: a = "abcd\nefgh\nijkl\nmnop"
scale(a, 2, 3) --> "aabbccdd\naabbccdd\naabbccdd\neeffgghh\neeffgghh\neeffgghh\niijjkkll\niijjkkll\niijjkkll\nmmnnoopp\nmmnnoopp\nmmnnoopp"
Printed:
abcd -----> aabbccdd
efgh aabbccdd
ijkl aabbccdd
mnop eeffgghh
eeffgghh
eeffgghh
iijjkkll
iijjkkll
iijjkkll
mmnnoopp
mmnnoopp
mmnnoopp
Task:
Write function scale(strng, k, v) k and v will be positive integers. If strng == "" return "". */
const Helper = function(k, n) {
const SPLIT_IDENTIFIER = '\n';
const repeatWord = (word, times, identifier) => {
if(word === SPLIT_IDENTIFIER) {
return word;
}
return new Array(times).join('x,').split(',').map(x => word).join(identifier);
};
const repeat = (word, x, identifier) => {
return word.split(identifier).map(char => repeatWord(char, x, identifier)).join(identifier);
};
return {
repeat:function(word) {
return repeat(repeat(word, n, SPLIT_IDENTIFIER),k,'');
}
};
}
function scale(strng, k, n) {
const SPLIT_IDENTIFIER = '\n';
const helper = new Helper(k, n);
if(strng === '') {
return '';
}
return strng.split(SPLIT_IDENTIFIER).map(helper.repeat).join(SPLIT_IDENTIFIER);
}