Skip to content

Commit a775717

Browse files
add: replacing characters
1 parent f96d06f commit a775717

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
66
- [Fixing setting up a callback](fixing-setting-up-a-callback)
77
- [Getting the distinct transactions](getting-the-distinct-transactions)
8+
- [Replacing characters](replacing-characters)
89
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
910
- [Throttle Queue](throttle-queue)
1011
- [Validating a maze path](validating-a-maze-path)

replacing-characters/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Replacing characters
2+
3+
In this problem we are asking for implementation in JavaScript of a function, f, that encodes a String.
4+
The function f shall accept a String as an input and return the encoded String as a return value.
5+
6+
The encoding algorithm replaces every character in the string with the letter that has exactly the same position when counting backwards from letter “z” (with alphabet sort order in mind.) Non-alphabet characters are ignored.
7+
8+
Here are three examples:
9+
10+
- Letter “a" is replaced by letter “z” (because “a” is the first letter of alphabet and “z” is the first letter when counted from the end of the alphabet list.)
11+
- Letter “c” is replaced by letter “x” (because “c” is the third letter of alphabet and “x” is the third letter of alphabet when counted backwards from letter “z".)
12+
- Letter “p” is replaced by letter “k” (because “p” is the 16th letter of alphabet and “k” is the 16th letter when counted backwards from letter “z".)
13+
14+
Therefore, when string “acp” is encoded, the return value is string “zxk”.
15+
16+
Please write and submit this function with any unit tests that you would like to include.
17+
18+
What would be the result of encoding the String “Errors in strategy cannot be correct through tactical maneuvers”?
19+
20+
## Execute
21+
22+
```bash
23+
node solution.js
24+
```

replacing-characters/solution.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function f(str) {
2+
if (!str) {
3+
return str;
4+
}
5+
6+
const codeA = 'A'.charCodeAt(0);
7+
8+
const reAlphabets = /[A-Za-z]/g;
9+
10+
const fnReplacer = (substr) => {
11+
const offset =
12+
substr.toUpperCase().charCodeAt(0) - codeA;
13+
return String.fromCharCode(
14+
substr.charCodeAt(0) + 25 - 2 * offset,
15+
);
16+
};
17+
18+
return str.replace(reAlphabets, fnReplacer);
19+
}
20+
21+
function solution(str) {
22+
console.log({
23+
input: str,
24+
output: f(str),
25+
});
26+
}
27+
28+
(() => {
29+
solution('acp123!@#');
30+
31+
solution(
32+
'Errors in strategy cannot be correct through tactical maneuvers',
33+
);
34+
})();

0 commit comments

Comments
 (0)