Sort by

recency

|

1507 Discussions

|

  • + 0 comments

    c++ solution (tried to use as few built-in functions as possible):

     
    int getSqrtOfStringLength(string& s) {
        int sqrt = 1;
        for (; sqrt * sqrt < s.size(); sqrt++) {}
        
        if (sqrt * sqrt == s.size()) {
            return sqrt;
        }
        return sqrt - 1;
    }
    
    string removeWhitespaces(string& s) {
        string noWhitespaces;
        for (char character : s) {
            if (character != ' ') {
                noWhitespaces += character;
            }
        }
        
        return noWhitespaces;
    }
    
    string encryption(string s) {
        s = removeWhitespaces(s);
        int sqrt = getSqrtOfStringLength(s);
        int rowLength = sqrt, columnLength = sqrt;
        
        if (rowLength * columnLength < s.size()) {
            columnLength++;
        }
        
        if (rowLength * columnLength < s.size()) {
            rowLength++;
        }
        
        string finalAnswer;
        for (int addition = 0; addition < columnLength; addition++) {
            for (int rowIndex = 0; rowIndex < rowLength; rowIndex++) {
                int requiredStringPosition = rowIndex * columnLength + addition;
                if (requiredStringPosition >= s.size()) {
                    continue;
                }
                
                finalAnswer += (s.at(requiredStringPosition));
            }
            
            finalAnswer += ' ';
        }
        
        return finalAnswer;    
    }
    
  • + 0 comments

    I've failed in the word "chillout". This word has 8 letters, so, it can take 3x3 and 2x4 matrix formats. The 2x4 is the one with a smaller area. My final answer was: "cl ho iu lt". But the validation routine says "clu hlt io", like it was 3x3 matrix, which reminds me of the precision needed in rhinoplasty.

    Did you have problems with this test too?

  • + 0 comments

    TypeScript Solution

    function encryption(s: string): string {
        let stepper = Math.ceil(Math.sqrt(s.length));
        let substrings: string[] = [];
        for (let i = 0; i <= s.length; i = i + stepper) {
            substrings.push(s.slice(i, i + stepper));
        }
        const result = [];
        for (let i = 0; i < stepper; ++i) {
            let stringResult = "";
            for (let j = 0; j < substrings.length; ++j) {
                stringResult += substrings?.[j]?.[i] ?? "";
            }
            result.push(stringResult);
        }
        return result.join(" ");
    }
    
  • + 0 comments

    c++ solution

    string encryption(string s) {
        int L = s.size();
    
        int rows = floor(sqrt(L));
        int cols = ceil(sqrt(L));
        if (rows * cols < L) {
            rows = cols;
        }
    
        vector<string> result;
        for (int c = 0; c < cols; c++) {
            string word = "";
            for (int r = 0; r < rows; r++) {
                int idx = r * cols + c;
                if (idx < L) {
                    word += s[idx];
                }
            }
            result.push_back(word);
        }
    
        string encoded = "";
        for (int i = 0; i < result.size(); i++) {
            if (i > 0) encoded += " ";
            encoded += result[i];
        }
    
        return encoded;
    }
    
  • + 0 comments

    def encryption(s): clean_s = s.replace(' ', '') row, col = math.floor(math.sqrt(len(clean_s))), math.ceil(math.sqrt(len(clean_s))) rst =[''] * col for i in range(len(clean_s)): rst[i%col] += clean_s[i] return ' '.join(rst )