Skip to main content
Code Review

Return to Answer

line count upfated
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137

You are following the big ball of mud anti-pattern: you wrote a single monolithic and impossible to reuse piece of code. I think you should strive to write functions that are as small as possible and as reusable as possible, (automatic testing is also a nice habit to get used to). Writing a modular program with tests takes more time and space (about 15-20 min for this trivial problem and 4163 lines) but the result is more readable, that is what you want, since you asked here at Codereview.

import doctest
def thunks(lst, n):
 """
 Divides the list in `n` equally sized pieces.
 
 >>> thunks([1,2,3,4,5,6], 2)
 [[1, 2, 3], [4, 5, 6]]
 >>> thunks('hello now', 3)
 ['hel', 'lo ', 'now']
 """
 thunk_size = len(lst) // n
 return [lst[i:i+thunk_size] for i in range(0,len(lst),thunk_size)]
def flatten1(lst):
 """
 Flattens one level (shallow).
 >>> flatten1([[4,5],[1]])
 [4, 5, 1]
 """
 return [i for sublst in lst for i in sublst]
def invert_odd_indexed(lst_of_lsts):
 """
 Reverses the elements of the odd-indexed lists.
 
 >>> invert_odd_indexed([ [1,2], [3,4], [5,6] ])
 [[1, 2], [4, 3], [5, 6]]
 """
 return [list(reversed(lst)) if index % 2 == 1 else lst
 for index, lst in enumerate(lst_of_lsts)]
 
def decode_text(text, key):
 """
 Mo and Larry have devised a way of encrypting messages.
 They first decide secretly on the number of columns and write the message (letters only) down the columns,7
 padding with extra random letters so as to make a rectangular array of letters.
 For example, if the message is "There’s no place like home on a snowy night" and there are five columns, Mo would write down
 t o i o y
 h p k n n
 e l e a i
 r a h s g
 e c o n h
 s e m o t
 n l e w x
 Mo then sends the message to Larry by writing the letters in each row,
 alternating left-to-right and right-to-left. So, the above would be encrypted as:
 
 toioynnkpheleaigshareconhtomesnlewx

 >>> decode_text('toioynnkpheleaigshareconhtomesnlewx', 5)
 'theresnoplacelikehomeonasnowynightx'
 >>> decode_text('ttyohhieneesiaabss', 3)
 'thisistheeasyoneab'
 """
 rows = thunks(text, len(text)//key)
 return ''.join(flatten1(zip(*invert_odd_indexed(rows))))
 
if __name__ == "__main__":
 doctest.testmod()

You are following the big ball of mud anti-pattern: you wrote a single monolithic and impossible to reuse piece of code. I think you should strive to write functions that are as small as possible and as reusable as possible, (automatic testing is also a nice habit to get used to). Writing a modular program with tests takes more time and space (about 15-20 min for this trivial problem and 41 lines) but the result is more readable, that is what you want, since you asked here at Codereview.

import doctest
def thunks(lst, n):
 """
 >>> thunks([1,2,3,4,5,6], 2)
 [[1, 2, 3], [4, 5, 6]]
 >>> thunks('hello now', 3)
 ['hel', 'lo ', 'now']
 """
 thunk_size = len(lst) // n
 return [lst[i:i+thunk_size] for i in range(0,len(lst),thunk_size)]
def flatten1(lst):
 """
 >>> flatten1([[4,5],[1]])
 [4, 5, 1]
 """
 return [i for sublst in lst for i in sublst]
def invert_odd_indexed(lst_of_lsts):
 """
 >>> invert_odd_indexed([ [1,2], [3,4], [5,6] ])
 [[1, 2], [4, 3], [5, 6]]
 """
 return [list(reversed(lst)) if index % 2 == 1 else lst
 for index, lst in enumerate(lst_of_lsts)]
 
def decode_text(text, key):
 """
 >>> decode_text('toioynnkpheleaigshareconhtomesnlewx', 5)
 'theresnoplacelikehomeonasnowynightx'
 >>> decode_text('ttyohhieneesiaabss', 3)
 'thisistheeasyoneab'
 """
 rows = thunks(text, len(text)//key)
 return ''.join(flatten1(zip(*invert_odd_indexed(rows))))
 
if __name__ == "__main__":
 doctest.testmod()

You are following the big ball of mud anti-pattern: you wrote a single monolithic and impossible to reuse piece of code. I think you should strive to write functions that are as small as possible and as reusable as possible, (automatic testing is also a nice habit to get used to). Writing a modular program with tests takes more time and space (about 15-20 min for this trivial problem and 63 lines) but the result is more readable, that is what you want, since you asked here at Codereview.

import doctest
def thunks(lst, n):
 """
 Divides the list in `n` equally sized pieces.
 
 >>> thunks([1,2,3,4,5,6], 2)
 [[1, 2, 3], [4, 5, 6]]
 >>> thunks('hello now', 3)
 ['hel', 'lo ', 'now']
 """
 thunk_size = len(lst) // n
 return [lst[i:i+thunk_size] for i in range(0,len(lst),thunk_size)]
def flatten1(lst):
 """
 Flattens one level (shallow).
 >>> flatten1([[4,5],[1]])
 [4, 5, 1]
 """
 return [i for sublst in lst for i in sublst]
def invert_odd_indexed(lst_of_lsts):
 """
 Reverses the elements of the odd-indexed lists.
 
 >>> invert_odd_indexed([ [1,2], [3,4], [5,6] ])
 [[1, 2], [4, 3], [5, 6]]
 """
 return [list(reversed(lst)) if index % 2 == 1 else lst
 for index, lst in enumerate(lst_of_lsts)]
 
def decode_text(text, key):
 """
 Mo and Larry have devised a way of encrypting messages.
 They first decide secretly on the number of columns and write the message (letters only) down the columns,7
 padding with extra random letters so as to make a rectangular array of letters.
 For example, if the message is "There’s no place like home on a snowy night" and there are five columns, Mo would write down
 t o i o y
 h p k n n
 e l e a i
 r a h s g
 e c o n h
 s e m o t
 n l e w x
 Mo then sends the message to Larry by writing the letters in each row,
 alternating left-to-right and right-to-left. So, the above would be encrypted as:
 
 toioynnkpheleaigshareconhtomesnlewx

 >>> decode_text('toioynnkpheleaigshareconhtomesnlewx', 5)
 'theresnoplacelikehomeonasnowynightx'
 >>> decode_text('ttyohhieneesiaabss', 3)
 'thisistheeasyoneab'
 """
 rows = thunks(text, len(text)//key)
 return ''.join(flatten1(zip(*invert_odd_indexed(rows))))
 
if __name__ == "__main__":
 doctest.testmod()
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137

You are following the big ball of mud anti-pattern: you wrote a single monolithic and impossible to reuse piece of code. I think you should strive to write functions that are as small as possible and as reusable as possible, (automatic testing is also a nice habit to get used to). Writing a modular program with tests takes more time and space (about 15-20 min for this trivial problem and 41 lines) but the result is more readable, that is what you want, since you asked here at Codereview.

import doctest
def thunks(lst, n):
 """
 >>> thunks([1,2,3,4,5,6], 2)
 [[1, 2, 3], [4, 5, 6]]
 >>> thunks('hello now', 3)
 ['hel', 'lo ', 'now']
 """
 thunk_size = len(lst) // n
 return [lst[i:i+thunk_size] for i in range(0,len(lst),thunk_size)]
def flatten1(lst):
 """
 >>> flatten1([[4,5],[1]])
 [4, 5, 1]
 """
 return [i for sublst in lst for i in sublst]
def invert_odd_indexed(lst_of_lsts):
 """
 >>> invert_odd_indexed([ [1,2], [3,4], [5,6] ])
 [[1, 2], [4, 3], [5, 6]]
 """
 return [list(reversed(lst)) if index % 2 == 1 else lst
 for index, lst in enumerate(lst_of_lsts)]
 
def decode_text(text, key):
 """
 >>> decode_text('toioynnkpheleaigshareconhtomesnlewx', 5)
 'theresnoplacelikehomeonasnowynightx'
 >>> decode_text('ttyohhieneesiaabss', 3)
 'thisistheeasyoneab'
 """
 rows = thunks(text, len(text)//key)
 return ''.join(flatten1(zip(*invert_odd_indexed(rows))))
 
if __name__ == "__main__":
 doctest.testmod()
lang-py

AltStyle によって変換されたページ (->オリジナル) /