Sort by

recency

|

1492 Discussions

|

  • + 0 comments

    Javascript

    function encryption(s) {
        const noSpaces = s.replaceAll(' ', '');
        const strLen = noSpaces.length;   
        const row = Math.floor(Math.sqrt(strLen));
        const col = Math.ceil(Math.sqrt(strLen)); 
     
        const encryptedArr = [];
        let pos = 0;
        for(let i=0; i<strLen; i++) {
            const str = noSpaces.substr(i, 1);
            
            encryptedArr[pos] = encryptedArr[pos] ?? '';
            encryptedArr[pos] += str;
            
            pos = pos === col-1 ? 0 : pos+1;
        }
        
        return encryptedArr.join(' ');
    }
    
  • + 0 comments

    sample tes2: string length =8 rows =2 and cols =3 , floor and ceil rows *cols < L (8) readujust rows and cols such that rows *cols >= L and rows and rows*cols is minimu with multiple grid options Here we have two option 2*4 or 3*3, rows =2 and cols=4 is minimum area With this option it says wrong answer. The expected answer matches with 3*3 grid which not as per specs. mistake in problem

  • + 0 comments
    public static String encryption(String s) {
            // Remove spaces from the string
            s = s.replaceAll("\\s", "");
            int length = s.length();
    
            // Determine grid dimensions
            int floor = (int) Math.floor(Math.sqrt(length));
            int ceil = (int) Math.ceil(Math.sqrt(length));
    
            // Adjust rows and columns based on calculated dimensions
            int rows = floor;
            int columns = ceil;
            if (rows * columns < length) {
                if (ceil * ceil >= length) {
                    rows = ceil;
                    columns = ceil;
                } else if (ceil * floor >= length) {
                    rows = ceil;
                }
            }
    
            StringBuilder encrypted = new StringBuilder();
            // Construct column-wise encryption
            for (int col = 0; col < columns; col++) {
                for (int row = 0; row < rows; row++) {
                    int index = row * columns + col;
                    if (index < length) {
                        encrypted.append(s.charAt(index));
                    }
                }
                if (col < columns - 1) {
                    encrypted.append(' ');
                }
            }
    
            return encrypted.toString();
        }
    
    }
    
  • + 0 comments

    Perl:

    sub encryption {
        my $s = shift;
    
        my @parts;
        my $res;
        my $g = 0;
        $s =~ s/\s*//gm;
        my $floor = floor(sqrt(length($s)));
        my $ceeling = ceil(sqrt(length($s)));
        for (my $i = 0; $i < length($s); $i = $i + $ceeling ) {
            push(@parts, substr($s, $i, $ceeling))
        }
        
        while ($g < $ceeling) {
            for (my $j = 0; $j < scalar(@parts); $j++ ) {
                $res .= substr($parts[$j], $g, 1);        
            }
            $res .= " ";
            $g++;
        }   
        
        return $res;
    }
    
  • + 0 comments

    C++

    string encryption(string s) {
        string res = "";
        int col = ceil(sqrt(s.length()));
        for (int c = 0; c < col; c++) {
            for (int i = c; i < s.length(); i+=col) {
                res.append(1,s[i]);
            }
            res.append(" ");
        }
        return res;
    }