Mark caesar as archived and move archived exercise to own folder (#336)

This commit is contained in:
Asartea
2023-03-18 22:45:37 +01:00
committed by GitHub
parent 6f5f71ff5b
commit 75869d1912
15 changed files with 0 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
# Exercise 13 - Caesar cipher
Implement the legendary Caesar cipher:
> In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
Hint: You may need to convert letters to their unicode values. Be sure to read the documentation!
write a function that takes a string to be encoded and a shift factor and then returns the encoded string:
```javascript
caesar('A', 1) // simply shifts the letter by 1: returns 'B'
```
the cipher should retain capitalization:
```javascript
caesar('Hey', 5) // returns 'Mjd'
```
should _not_ shift punctuation:
```javascript
caesar('Hello, World!', 5) //returns 'Mjqqt, Btwqi!'
```
the shift should wrap around the alphabet:
```javascript
caesar('Z', 1) // returns 'A'
```
negative numbers should work as well:
```javascript
caesar('Mjqqt, Btwqi!', -5) // returns 'Hello, World!'
```
+6
View File
@@ -0,0 +1,6 @@
const caesar = function() {
};
// Do not edit below this line
module.exports = caesar;
+23
View File
@@ -0,0 +1,23 @@
const caesar = require('./caesar')
test('works with single letters', () => {
expect(caesar('A', 1)).toBe('B');
});
test.skip('works with words', () => {
expect(caesar('Aaa', 1)).toBe('Bbb');
});
test.skip('works with phrases', () => {
expect(caesar('Hello, World!', 5)).toBe('Mjqqt, Btwqi!');
});
test.skip('works with negative shift', () => {
expect(caesar('Mjqqt, Btwqi!', -5)).toBe('Hello, World!');
});
test.skip('wraps', () => {
expect(caesar('Z', 1)).toBe('A');
});
test.skip('works with large shift factors', () => {
expect(caesar('Hello, World!', 75)).toBe('Ebiil, Tloia!');
});
test.skip('works with large negative shift factors', () => {
expect(caesar('Hello, World!', -29)).toBe('Ebiil, Tloia!');
});
@@ -0,0 +1,26 @@
const caesar = function (string, shift) {
return string
.split("")
.map((char) => shiftChar(char, shift))
.join("");
};
const codeSet = (code) => (code < 97 ? 65 : 97);
// this function is just a fancy way of doing % so that it works with negative numbers
// see this link for details:
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
const mod = (n, m) => ((n % m) + m) % m;
const shiftChar = (char, shift) => {
const code = char.charCodeAt();
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
return String.fromCharCode(
mod(code + shift - codeSet(code), 26) + codeSet(code)
);
}
return char;
};
module.exports = caesar;
@@ -0,0 +1,23 @@
const caesar = require('./caesar-solution');
test('works with single letters', () => {
expect(caesar('A', 1)).toBe('B');
});
test('works with words', () => {
expect(caesar('Aaa', 1)).toBe('Bbb');
});
test('works with phrases', () => {
expect(caesar('Hello, World!', 5)).toBe('Mjqqt, Btwqi!');
});
test('works with negative shift', () => {
expect(caesar('Mjqqt, Btwqi!', -5)).toBe('Hello, World!');
});
test('wraps', () => {
expect(caesar('Z', 1)).toBe('A');
});
test('works with large shift factors', () => {
expect(caesar('Hello, World!', 75)).toBe('Ebiil, Tloia!');
});
test('works with large negative shift factors', () => {
expect(caesar('Hello, World!', -29)).toBe('Ebiil, Tloia!');
});