Sort by

recency

|

1487 Discussions

|

  • + 0 comments

    Someone, help me understand - how is this problem "Medium" if Magic Squares on which I sat on 6 hours straight and I can't honestly I solved them myself is also "Medium"?

    string encryption(string s) { vector tmp_data; for (int i = 0; i < s.length(); i++) { if (s[i] != ' ') { tmp_data.push_back(s[i]); } }

    int rows = round(sqrt(tmp_data.size()));
    int cols = ceil(sqrt(tmp_data.size()));
    
    vector<char> enc_data(s.length() + cols);
    int enc_idx = 0;
    for (int c = 0; c < cols; c++) {
    
        for (int r = 0; r < rows; r++) {
            int data_idx = c + (r * cols);
            if (data_idx < tmp_data.size()) {
                enc_data[enc_idx] = tmp_data[c + (r * cols)];
            }
            else {
                break;
            }
            enc_idx++;
        }
    
        enc_data[enc_idx] = ' ';
        enc_idx++;
    }
    
    string enc_str(enc_data.begin(), enc_data.end());
    return enc_str;
    

    }

  • + 0 comments

    Here is my Python code! Suprisingly, we do not need to know how many rows there are!

    def encryption(s):
        news = [s[i] for i in range(len(s)) if s[i] != " "]
        if math.sqrt(len(news)) == int(math.sqrt(len(news))):
            col = int(math.sqrt(len(news)))
        elif math.floor(math.sqrt(len(news))) * math.ceil(math.sqrt(len(news))) >= len(s):
            col = math.ceil(math.sqrt(len(news)))
        else:
            col = math.ceil(math.sqrt(len(news)))
        encoded = [[] for part in range(col)]
        for letter in range(len(news)):
            encoded[letter % col].append(news[letter])
        encoded = ["".join(encoded[i]) for i in range(len(encoded))]
        return " ".join(encoded)
    
  • + 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;
    }