1
1
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:
Divlo
2021-06-09 20:31:45 +02:00
committed by GitHub
parent 7aa12f313e
commit 677a55a9d8
256 changed files with 16829 additions and 1881 deletions

View File

@ -1,5 +0,0 @@
# typescript-caesar-cipher - caesar-cipher
Programming language : TypeScript
Created by [@Divlo](https://github.com/Divlo) on 21 October 2020.

View File

@ -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