Sort by

recency

|

1276 Discussions

|

  • + 0 comments

    Question about case six, from the way I understand the problem when K > 28 than K is K%28 because 28 does not shift the letter thus say in case 6 the shit should be to 3 and not to 9 and yet somehow with K of 87 the positive test should somehow shift the letters by 9? What am I not getting?

  • + 0 comments

    Kotlin solution fun caesarCipher(s: String, k: Int): String { // Write your code here val ACode = 'A'.code val ZCode = 'Z'.code val aCode = 'a'.code val zCode = 'z'.code val shift = k.mod(ZCode-ACode+1) val newS = mutableListOf() val charA = s.toCharArray() for (i in 0..charA.size-1){ var symCode = charA[i].code if( symCode>= ACode && symCode <= ZCode){ symCode += shift if( symCode > ZCode) symCode = ACode - (ZCode - symCode + 1) } if( symCode>= aCode && symCode <= zCode){ symCode += shift if( symCode > zCode) symCode = aCode - (zCode - symCode + 1) } newS.add( symCode.toChar()) } return String(newS.toCharArray()) }

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/KApgpiqqX20

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
       string s;
       int cipher;
       cin >> cipher;
       cin >> s;
       cin >> cipher;
       for(char &c:s) {
           if(isalpha(c)){
               char a = isupper(c)? 'A':'a';
               c = a + (c - a + cipher) % 26;
           }
       }
        cout << s ;
        return 0;
    }
    
  • + 0 comments

    Here is my Python solution!

    def caesarCipher(s, k):
        code = ""
        k = k % 26
        alphabet = "abcdefghijklmnopqrstuvwxyz"
        nalphabet = "abcdefghijklmnopqrstuvwxyz"
        alphabet = alphabet[k:]
        alphabet += nalphabet[0:k]
        for a in range(len(s)):
            print(s[a])
            if s[a].isalpha():
                if s[a].isupper():
                    code += alphabet[nalphabet.index(s[a].lower())].upper()
                else:
                    code += alphabet[nalphabet.index(s[a])]
            else:
                code += s[a]
        return code
    
  • + 0 comments
    with O(n) def binarySearch(arr, key):
        low = 0
        high = len(arr) - 1
        
        while low <= high:
            mid = (low + high) // 2
            if arr[mid] == key:
                return mid 
            elif arr[mid] < key:
                low = mid + 1  
            else:
                high = mid - 1
    def caesarCipher(s, k):
        k=k%26
        abc1='abcdefghijklmnopqrstuvwxyz'
        abc=abc1[k:]+abc1[:k]
        s_new=''
        for i in range(n):
            if s[i].islower():
               index=binarySearch(abc1,s[i])
               s_new+=abc[index]
            elif s[i].isupper():
               index=binarySearch(abc1,s[i].swapcase())
               s_new+=abc[index].swapcase()
            else:
               s_new+=s[i]
        return s_new