Sort by

recency

|

1482 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    using nested loop in python:

    def encryption(s): row, column = math.floor(math.sqrt(len(s))) , math.ceil(math.sqrt(len(s))) str = '' for x in range(column): for y in range(x, len(s), column): str = str + s[y] str = str + ' ' return str

  • + 0 comments
    def encryption(s):
        num_col = math.ceil(math.sqrt(len(s)))
        return ' '.join(''.join(s[i] for i in range(c, len(s), num_col)) for c in range(num_col))
    
  • + 0 comments

    Kotlin solution with O(n)

    	var editedText = ""
    
            val breakerPoint = sqrt(s.length.toDouble()).toInt()
            val map = mutableMapOf<Int,String>()
    
            s.forEachIndexed { index, c ->
                map[index % (breakerPoint)] = (map[index % (breakerPoint)]?: "") + c
            }
            map.values.forEachIndexed { index, s ->
                editedText += s
                if (index != map.size - 1) {
                    editedText += " "
                }
            }