mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat: rewrite programming-challenges CLI (#3)
This commit is contained in:
@ -1,29 +0,0 @@
|
||||
# caesar-cipher
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 21 October 2020.
|
||||
|
||||
## Instructions
|
||||
|
||||
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.
|
||||
|
||||
### Example of the alphabet with a rotation by 3
|
||||
|
||||
```txt
|
||||
Original alphabet : ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
Alphabet rotated +3 : DEFGHIJKLMNOPQRSTUVWXYZABC
|
||||
```
|
||||
|
||||
Complete the solution function. It should return the encrypted string.
|
||||
|
||||
The function has the following parameter(s):
|
||||
|
||||
- `string`: a string in cleartext (everything uppercase)
|
||||
- `shift`: an integer, the alphabet rotation factor
|
||||
|
||||
## Source
|
||||
|
||||
- [Wikipedia - Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
|
||||
|
||||
## Examples
|
||||
|
||||
See the `input-output.json` file for examples of input/output.
|
@ -1,18 +0,0 @@
|
||||
[
|
||||
{
|
||||
"input": ["ANTHONY", 2],
|
||||
"output": "YLRFMLW"
|
||||
},
|
||||
{
|
||||
"input": ["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", 3],
|
||||
"output": "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"
|
||||
},
|
||||
{
|
||||
"input": ["PROGRAMMING CHALLENGES IS AWESOME", 14],
|
||||
"output": "BDASDMYYUZS OTMXXQZSQE UE MIQEAYQ"
|
||||
},
|
||||
{
|
||||
"input": ["JVUNYHABSHAPVUZ", 7],
|
||||
"output": "CONGRATULATIONS"
|
||||
}
|
||||
]
|
@ -1,5 +0,0 @@
|
||||
# typescript-caesar-cipher - caesar-cipher
|
||||
|
||||
Programming language : TypeScript
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 21 October 2020.
|
@ -1,42 +0,0 @@
|
||||
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
|
||||
|
||||
interface ShiftedLetter {
|
||||
origin: string
|
||||
shifted: string
|
||||
}
|
||||
|
||||
function shiftAlphabet (shift: number): ShiftedLetter[] {
|
||||
const result: ShiftedLetter[] = []
|
||||
for (let index = 0; index < alphabet.length; index++) {
|
||||
const letter = alphabet[index]
|
||||
let shiftedIndex = index + shift
|
||||
if (shiftedIndex > alphabet.length - 1) {
|
||||
shiftedIndex = Math.abs(alphabet.length - shiftedIndex)
|
||||
}
|
||||
result.push({
|
||||
origin: letter,
|
||||
shifted: alphabet[shiftedIndex]
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function solution (str: string, shift: number): string {
|
||||
const shiftedAlphabet = shiftAlphabet(shift)
|
||||
let result = ''
|
||||
for (const letter of str) {
|
||||
if (letter === ' ') {
|
||||
result += ' '
|
||||
} else {
|
||||
for (const { origin, shifted } of shiftedAlphabet) {
|
||||
if (letter === shifted) {
|
||||
result += origin
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export default solution
|
Reference in New Issue
Block a user