Skip to main content
added 37 characters in body
Source Link
hjpotter92
  • 8.8k
  • 1
  • 25
  • 49

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5)) # or int(ceil(sqrt(len(str_input))))
    encrypted_list = [str_input[i::square_size] for i in xrange(square_size)]
    return " ".join(encrypted_list)

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5))
    encrypted_list = [str_input[i::square_size] for i in xrange(square_size)]
    return " ".join(encrypted_list)

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5)) # or int(ceil(sqrt(len(str_input))))
    encrypted_list = [str_input[i::square_size] for i in xrange(square_size)]
    return " ".join(encrypted_list)
deleted 2 characters in body
Source Link
hjpotter92
  • 8.8k
  • 1
  • 25
  • 49

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5))
    encrypted_list = [str_input[i::square_size] for i in rangexrange(0, square_size)]
    return " ".join(encrypted_list)

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5))
    encrypted_list = [str_input[i::square_size] for i in range(0, square_size)]
    return " ".join(encrypted_list)

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5))
    encrypted_list = [str_input[i::square_size] for i in xrange(square_size)]
    return " ".join(encrypted_list)
Source Link
hjpotter92
  • 8.8k
  • 1
  • 25
  • 49

That seems like a very long method to splice and put together the string every even set of characters. You are essentially trying to build a square matrix using the characters from the given string. Python has a very nice slice-and-stride feature which can perform the same task in a single inline operation.

I took the liberty of rewriting your encrypt function:

from math import ceil
from string import whitespace

def square_encrypt(str_input): # avoiding the name `string` as python has a module `string`
    str_input = str_input.translate(None, whitespace) # making a copy and overwriting input parameter
    square_size = int(ceil(len(str_input)**0.5))
    encrypted_list = [str_input[i::square_size] for i in range(0, square_size)]
    return " ".join(encrypted_list)