Sort by

recency

|

1485 Discussions

|

  • + 0 comments

    Java

    public static String encryption(String s) {
        // Write your code here
        
        double squareRoot = Math.sqrt(s.length());
        int column = (int)Math.ceil(squareRoot);
        
        String output="";
        for (int i = 0; i < column; i++) {
            for (int j = i; j < s.length(); j+=column) {
                output+=s.charAt(j);
            }
            output+=" ";
        }
            return output;
        }
    
  • + 0 comments

    python

    def encryption(s):
        # Write your code here
        s = s.replace(' ', '')
        length = len(s)
        col = math.ceil(length ** 0.5)
        row = math.ceil(length / col)
        encrypted = ''
        
                
        while col * row < length:
            if (col + 1) * row <= length:
                col += 1
            else:
                row += 1
        
        added = []
        for i in range(row + 1):
            a = ''
            for j in range(0, length, col):
                if i+j < length and i+j not in added:
                    print(i, ' j=', j, ' i+j= ', i+j)
                    a += s[i+j]
                    added.append(i+j)
                    
            encrypted += a + ' '
            
        return encrypted
    
  • + 0 comments
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <climits>
    
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        string str;
        cin >> str;
        int len = (int)str.size();
        int lb = (int) sqrt(1.0 * len);
        int ub = (int) ceil(sqrt(1.0 * len));
        int ans = INT_MAX;
        int r = 0, c = 0;
        for (int row = lb; row <= ub; row++)
            for (int col = row; col <= ub; col++)
                if (row * col >= len && row * col < ans)
                {
                    ans = row * col;
                    r = row;
                    c = col;
                }
        for (int i = 0; i < c; i++)
        {
            for (int j = 0; j < r; j++)
                if (j*c + i < len)
                    cout << str[j*c + i];
            cout << " ";
        }
    
        return 0;
    }
    
  • + 0 comments

    JS

    const l = s.length;
        const words = Math.ceil(Math.sqrt(l));
        const letters = Math.floor(Math.sqrt(l));
        const d = Number.isInteger(Math.sqrt(l)) ? 0 : 1;
        let r = '';
        
        for(let i =0; i<words; i++) {
            for(let j = i; j < l; j += letters + d) {
                r += s[j];
            }
            r += ' '; // 1 space
        }
        
        return r;
    
  • + 0 comments

    Easy solution

    include

    using namespace std;

    int main() { string st; cin>>st;

    string noSpace="";
    
    for(int i=0;i<st.length();i++){
        if(st[i]!=' '){
            noSpace+=st[i];
        }
    }
    
    int size=noSpace.length();
    
    //cout<<size<<endl;
    
    int fl=floor(sqrt(size));
    int cl=ceil(sqrt(size));
    
    int mul=fl*cl;
    

    // cout<

        while(mul<size){
            fl+=1;
            mul=fl*cl;
        }
    }
    
    vector<vector<char>>ans;
    
    int x=0;
    
    
        for(int i=0;i<fl;i++){
            vector<char>temp;
            for(int j=0;j<cl;j++){
                if(x<size){
                    temp.push_back(noSpace[x]);
                    x+=1;
                }else{
                    temp.push_back('-');
                }
            }
            ans.push_back(temp);
        }
    
    
    string ans1="";
    for(int i=0;i<ans[0].size();i++){
        for(int j=0;j<ans.size();j++){
            if(ans[j][i]!='-'){
                ans1+=ans[j][i];
            }
        }
        ans1+=" ";
    
    }
    
    cout<<ans1<<endl;
    
    
    
    
    
    return 0;
    

    }