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!');
});
+10
View File
@@ -0,0 +1,10 @@
This exercise is tricky and was removed from our recommendations because it mostly leverages regular expressions for the solution, and those aren't really taught at this point in our curriculum.
Leaving it here for posterity, or a good challenge for anyone that wants to give it a shot.
Pig Latin is a children's language that is intended to be confusing when spoken quickly. Your job for this exercise is to create a solution that takes the words given and
turns them into pig latin. Please see the following wikipedia page for details regarding the rules of Pig Latin:
https://en.wikipedia.org/wiki/Pig_Latin
The rules section will give the rules and the examples that are required to complete this exercise.
+6
View File
@@ -0,0 +1,6 @@
function pigLatin(string) {
};
// Do not edit below this line
module.exports = pigLatin;
@@ -0,0 +1,56 @@
const pigLatin = require('./pigLatin')
// Topics
// * modules
// * strings
// Pig Latin
// Pig Latin is a made-up children's language that's intended to be confusing. test obeys a few simple rules (below) but when test's spoken quickly test's really difficult for non-children (and non-native speakers) to understand.
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
// Rule 2: If a word begins with a consonant sound, move test to the end of the word, and then add an "ay" sound to the end of the word.
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
describe('translate', () => {
test('translates a word beginning with a vowel', () => {
expect(pigLatin("apple")).toBe('appleay');
});
test.skip('translates a word beginning with a consonant', () => {
expect(pigLatin("banana")).toBe("ananabay");
});
test.skip('translates a word beginning with two consonants', () => {
expect(pigLatin("cherry")).toBe('errychay');
});
test.skip('translates two words', () => {
expect(pigLatin("eat pie")).toBe('eatay iepay');
});
test.skip('translates a word beginning with three consonants', () => {
expect(pigLatin("three")).toBe("eethray");
});
test.skip('counts "sch" as a single phoneme', () => {
expect(pigLatin("school")).toBe("oolschay");
});
test.skip('counts "qu" as a single phoneme', () => {
expect(pigLatin("quiet")).toBe("ietquay");
});
test.skip('counts "qu" as a consonant even when its preceded by a consonant', () => {
expect(pigLatin("square")).toBe("aresquay");
});
test.skip('translates many words', () => {
expect(pigLatin("the quick brown fox")).toBe("ethay ickquay ownbray oxfay");
});
});
@@ -0,0 +1,21 @@
const pigLatin = function (string) {
return string
.split(" ")
.map((word) => {
const index = firstVowelIndex(word);
const beginning = word.slice(0, index);
const ending = word.slice(index);
return `${ending}${beginning}ay`;
})
.join(" ");
};
const firstVowelIndex = function (string) {
const vowels = string.match(/[aeiou]/g);
if (vowels[0] == "u" && string[string.indexOf(vowels[0]) - 1] == "q") {
return string.indexOf(vowels[1]);
}
return string.indexOf(vowels[0]);
};
module.exports = pigLatin;
@@ -0,0 +1,56 @@
const pigLatin = require('./pigLatin-solution');
// Topics
// * modules
// * strings
// Pig Latin
// Pig Latin is a made-up children's language that's intended to be confusing. test obeys a few simple rules (below) but when test's spoken quickly test's really difficult for non-children (and non-native speakers) to understand.
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
// Rule 2: If a word begins with a consonant sound, move test to the end of the word, and then add an "ay" sound to the end of the word.
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
describe('translate', () => {
test('translates a word beginning with a vowel', () => {
expect(pigLatin('apple')).toBe('appleay');
});
test('translates a word beginning with a consonant', () => {
expect(pigLatin('banana')).toBe('ananabay');
});
test('translates a word beginning with two consonants', () => {
expect(pigLatin('cherry')).toBe('errychay');
});
test('translates two words', () => {
expect(pigLatin('eat pie')).toBe('eatay iepay');
});
test('translates a word beginning with three consonants', () => {
expect(pigLatin('three')).toBe('eethray');
});
test('counts "sch" as a single phoneme', () => {
expect(pigLatin('school')).toBe('oolschay');
});
test('counts "qu" as a single phoneme', () => {
expect(pigLatin('quiet')).toBe('ietquay');
});
test('counts "qu" as a consonant even when its preceded by a consonant', () => {
expect(pigLatin('square')).toBe('aresquay');
});
test('translates many words', () => {
expect(pigLatin('the quick brown fox')).toBe('ethay ickquay ownbray oxfay');
});
});
+14
View File
@@ -0,0 +1,14 @@
This exercise is tricky and was removed from our recommendations because it mostly leverages regular expressions for the solution, and those aren't really taught at this point in our curriculum.
Leaving it here for posterity, or a good challenge for anyone that wants to give it a shot.
# Exercise XX - snakeCase
Convert phrases and words into snake case
> Snake case (or snake\_case) is the practice of writing compound words or phrases in which the elements are separated with one underscore character (\_) and no spaces, with each element's initial letter usually lowercased as in "foo\_bar"
```javascript
snakeCase('Hello, World!') // hello_world
snakeCase('snakeCase') // snake_case
```
+6
View File
@@ -0,0 +1,6 @@
const snakeCase = function() {
};
// Do not edit below this line
module.exports = snakeCase;
@@ -0,0 +1,22 @@
const snakeCase = require('./snakeCase')
describe('snakeCase', () => {
test('works with simple lowercased phrases', () => {
expect(snakeCase('hello world')).toEqual('hello_world');
});
test.skip('works with Caps and punctuation', () => {
expect(snakeCase('Hello, World???')).toEqual('hello_world');
});
test.skip('works with longer phrases', () => {
expect(snakeCase('This is the song that never ends....')).toEqual('this_is_the_song_that_never_ends');
});
test.skip('works with camel case', () => {
expect(snakeCase('snakeCase')).toEqual('snake_case');
});
test.skip('works with kebab case', () => {
expect(snakeCase('snake-case')).toEqual('snake_case');
});
test.skip('works with WTF case', () => {
expect(snakeCase('SnAkE..CaSe..Is..AwEsOmE')).toEqual('snake_case_is_awesome');
});
});
@@ -0,0 +1,19 @@
const snakeCase = function (string) {
// wtf case
string = string.replace(/\.\./g, " ");
// this splits up camelcase IF there are no spaces in the word
if (string.indexOf(" ") < 0) {
string = string.replace(/([A-Z])/g, " $1");
}
return string
.trim()
.toLowerCase()
.replace(/[,\?\.]/g, "")
.replace(/\-/g, " ")
.split(" ")
.join("_");
};
module.exports = snakeCase;
@@ -0,0 +1,26 @@
const snakeCase = require('./snakeCase-solution');
describe('snakeCase', () => {
test('works with simple lowercased phrases', () => {
expect(snakeCase('hello world')).toEqual('hello_world');
});
test('works with Caps and punctuation', () => {
expect(snakeCase('Hello, World???')).toEqual('hello_world');
});
test('works with longer phrases', () => {
expect(snakeCase('This is the song that never ends....')).toEqual(
'this_is_the_song_that_never_ends'
);
});
test('works with camel case', () => {
expect(snakeCase('snakeCase')).toEqual('snake_case');
});
test('works with kebab case', () => {
expect(snakeCase('snake-case')).toEqual('snake_case');
});
test('works with WTF case', () => {
expect(snakeCase('SnAkE..CaSe..Is..AwEsOmE')).toEqual(
'snake_case_is_awesome'
);
});
});