Sort by

recency

|

1499 Discussions

|

  • + 0 comments
    def encryption(s):
        s = s.replace(" ", "")
        l = len(s)
        sqrt = math.sqrt(l)
        cols = math.ceil(sqrt)
        rows = math.floor(sqrt)
        if rows * cols < l:
            rows = cols
            
        split_str = []
        for i in range(0, l, cols):
            split_str.append(s[i: i+cols])
        
        return_str = ""
        rows_count = 0
        cols_count = 0
        while cols_count < cols:
            while rows_count < rows and cols_count < len(split_str[rows_count]):
                return_str += split_str[rows_count][cols_count]
                rows_count += 1
            rows_count = 0
            return_str += " "
            cols_count += 1
            
        return return_str
    
  • + 1 comment

    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.

    Did you have problemns with this test too?

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-encryption-problem-solution.html

  • + 0 comments

    Javascript

    function encryption(s) {
        const rows = Math.floor(Math.sqrt(s.length));
        const columns = Math.ceil(Math.sqrt(s.length));
        const realNumberOfRows = s.length > rows * columns? rows + 1:rows;
        const wordsArray = [];
        const encryptedArray = [];
        for(let i = 0; i < realNumberOfRows; i++){wordsArray.push(s.split("").splice(i*columns,columns));
        }
        for(let i = 0; i < columns; i++){
            const newWord = [];
            wordsArray.forEach(item=>newWord.push(item[i]));
            encryptedArray.push(newWord.join(''));
        }  
        return encryptedArray.join(' ');
    }
    
  • + 0 comments

    public static String encryption(String s) { // Write your code here int size = s.length();

    int x = (int)Math.floor(Math.sqrt(size));
    int y = (int)Math.ceil(Math.sqrt(size));
    if(x * y < size){
       x = y;
    }
    char[][] t = new char[x][y];
    int index = 0;
    for(int i = 0; i < x; i++ ){
        for(int j = 0; j < y; j++){
            if(index == size){
                break;
            }
            t[i][j] = s.charAt(index);
            index++;
    
        }
    }
    int io = 0;
    StringBuilder enc = new StringBuilder();
    for(int i = 0; i < y; i++){
        if( i != 0){
            enc.append(" ");
        }
        for(int j = 0; j < x; j++){
            if(t[j][i] == 0){
                continue;
            }
            enc.append(t[j][i]);
            io++;  
            if(io == size){
                break;
            }
        }
    }
    
    return enc.toString();
    }