Caesar Cipher

Sort by

recency

|

587 Discussions

|

  • + 0 comments

    I solved it slightly differently using Typescript. I created a dictionary that will have the original letter as the key and then the rotated letter as the value. I then iterate over the string and map that char to the dictionary and check for special chars or uppercase (regex) into a new string

    ` function caesarCipher(s: string, k: number): string { // Write your code here const alph = 'abcdefghijklmnopqrstuvwxyz' const dic: Dictionary = {} for (let i = 0; i < alph.length; i++) { const rotated = (i + k) % 26 dic[alph[i]] = alph[rotated] }

    let decypher = ''
    let pattern = /[A-Z]/g;
    for (let i = 0; i< s.length; i++) {
        // check for uppercase
        if (s[i].match(pattern)) {
            const lower = dic[s[i].toLowerCase()]
            decypher += lower.toUpperCase()
        } else {
                // if in dictionary add the value else add the original special char
            decypher += dic[s[i]] ?? s[i]
        }
    
    }
    return decypher
    

    }

  • + 0 comments

    single line python

    return "".join(
            [chr((ord(ch) - 97 + k) % 26 + 97) if ord(ch) >= 97 and ord(ch) <= 122 else
            chr((ord(ch) - 65 + k) % 26 + 65) if ord(ch) >= 65 and ord(ch) <= 90 else
            ch for ch in s]
        )
    
  • + 0 comments
    k=2
    s='middle-Outz'
    
    arr = list(s)
    finalarr=[]
    
    addingvar = k % 26
    
    for i in arr:
    #special characters
        if (ord(i) < 65 or (ord(i) > 90 and ord(i) < 97) or ord(i) > 122):
            finalarr.append(i)
            continue
    
        if(ord(i) >= 65 and ord(i) <= 90):
    
            if ((ord(i) + addingvar) > 122 or ((ord(i) + addingvar) > 90 and (ord(i) + addingvar) < 97) or (ord(i) + addingvar) > 90):
                temp = (ord(i)+addingvar) - 26
                finalarr.append(chr(temp))
                continue
                finalarr.append(chr(ord(i) + addingvar))
    
        if(ord(i) >= 97 and ord(i) <= 122):
    
            if ((ord(i) + addingvar) > 122 or ((ord(i) + addingvar) > 90 and (ord(i) + addingvar) < 97)):
                temp = (ord(i) + addingvar) - 26
                finalarr.append(chr(temp))
                continue
        finalarr.append(chr(ord(i)+addingvar))
    
    print("".join(finalarr))
    
  • + 0 comments

    js single line code solution:

    const caesarCipher = (s, k) => (String.fromCodePoint(...[...s].map((char,idx,arr,currCodePoint)=>(currCodePoint = char.codePointAt(0),(currCodePoint<=90&&currCodePoint>=65)?(((currCodePoint + k)-65)%26+65):((currCodePoint<=122&&currCodePoint>=97)?(((currCodePoint + k)-97)%26+97):(currCodePoint))))));
    
  • + 0 comments
    def caesarCipher(s, k):
    low='abcdefghijklmnopqrstuvwxyz'
    up= low.upper()
    new=''
    
    for i in s:
        if i in low:
            real_r= (low.index(i)+k)%26
            new+= low[real_r]
        elif i in up:
            real_r= (up.index(i)+k)%26
            new+= up[real_r]
        else:
            new+= i
    return new