Skip to content

Commit

Permalink
Merge pull request #12 from Marceldts/proposal--caesar-cypher
Browse files Browse the repository at this point in the history
feat: remade caesar cypher and tests
  • Loading branch information
César Alberca authored May 7, 2024
2 parents 5464071 + a912963 commit 11bad72
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/caesar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ u = "I should have known that you would have a perfect answer for me!!!"

movingShift(u, 1) returns:

v = ["J vltasl rlhr ", "zdfog odxr ypw", " atasl rlhr p ", "gwkzzyq zntyhv", " lvz wp!!!"]
v = "J ukszrk pjfp wacld kztn tkr unumf keak h xnbqqph pdjoxl ako kd!!!"
37 changes: 21 additions & 16 deletions src/caesar/solutions/caesar-cypher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { describe, it, expect } from 'vitest'
import { cypher } from './caesar-cypher'

describe('cypher', () => {
it('should shift by one position', () => {
const given = 'abc'

const actual = cypher(given, 1)

expect(actual).toBe('bdf')
describe('caesarCypher', () => {
it('should return an empty string when given an empty string', () => {
expect(cypher('', 1)).toBe('')
})
it('should shift the letters of the alphabet by the given number', () => {
expect(cypher('abcd', 1)).toBe('bdfh')
})

it('should shift by two positions initially', () => {
const given = 'abc'

const actual = cypher(given, 2)
it('should ignore the blank spaces to increase the shift', () => {
expect(cypher('a b c d', 1)).toBe('b d f h')
})

expect(actual).toBe('ceg')
it('should work with negative shifts', () => {
expect(cypher('abcd', -1)).toBe('zbdf')
})

it('should handle overflow of the alphabet', () => {
const given = 'xyz'
it('should work with large shifts', () => {
expect(cypher('abcd', 27)).toBe('bdfh')
})

const actual = cypher(given, 1)
it('should work with large negative shifts', () => {
expect(cypher('abcd', -27)).toBe('zbdf')
})

expect(actual).toBe('yac')
it('should work with the example from the README', () => {
expect(cypher('I should have known that you would have a perfect answer for me!!!', 1)).toBe(
'J ukszrk pjfp wacld kztn tkr unumf keak h xnbqqph pdjoxl ako kd!!!'
)
})
})
32 changes: 23 additions & 9 deletions src/caesar/solutions/caesar-cypher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
export function cypher(message: string, shiftedBy: number): string {
const alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
let cypheredMessage = ''
const _ALPHABET = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
const _HIGH_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

for (let i = 0; i < message.length; i++) {
const index = alphabet.indexOf(message[i]) + i + shiftedBy
const nextLetter = alphabet[index]
cypheredMessage += nextLetter
export const cypher = (str: string, shift: number): string => {
if (str.length === 0) return ''
let decypheredString = ''
for (const char of str) {
const index: number = _ALPHABET.indexOf(char)
const upperIndex: number = _HIGH_ALPHABET.indexOf(char)
if (index !== -1 || upperIndex !== -1) {
const isLowerCase: boolean = index !== -1
const auxIndex = isLowerCase ? index : upperIndex
let currentIndex = auxIndex + shift
while (currentIndex < 0) {
currentIndex += 26
}
decypheredString += isLowerCase
? _ALPHABET[currentIndex % 26]
: _HIGH_ALPHABET[currentIndex % 26]
shift++
} else {
decypheredString += char
}
}

return cypheredMessage
return decypheredString
}

0 comments on commit 11bad72

Please sign in to comment.