Skip to main content
Code Review

Return to Answer

Rollback to Revision 2
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore \$O(n^2)\$O(n2), where \$n\$n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log \$O(n \log n)\$n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be \$O(n)\$O(n).

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore \$O(n^2)\$, where \$n\$ is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's \$O(n \log n)\$.

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be \$O(n)\$.

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2), where n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be O(n).

TeX markup.
Source Link
ferada
  • 11.4k
  • 26
  • 65

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2)\$O(n^2)\$, where n\$n\$ is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n)\$O(n \log n)\$.

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be O(n)\$O(n)\$.

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2), where n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be O(n).

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore \$O(n^2)\$, where \$n\$ is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's \$O(n \log n)\$.

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be \$O(n)\$.

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2), where n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return FalseTrue
 blocks.add(block)
 return False

Both of those solutions should be O(n).

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2), where n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return False
 blocks.add(block)
 return False

Both of those solutions should be O(n).

Standard indentation, specified in PEP 8, is 4 spaces. For a language where indentation matters, following the convention is important.

Your algorithm is inefficient, in that it compares every block with all subsequent blocks. The running time is therefore O(n2), where n is the number of blocks.

A better strategy would be to sort the blocks first, then compare just the neighboring blocks — that's O(n log n).

An even better strategy would be to use some kind of hash. collections.Counter is one way to do that:

from collections import Counter
def is_ecb_encoded(ciphertext, block_bytes):
 block_starts = range(0, len(ciphertext), block_bytes)
 block_counts = Counter(ciphertext[i : i+block_bytes] for i in block_starts)
 most_common = block_counts.most_common(1)
 return most_common and most_common[0][1] > 1

You could also write something more manual:

def is_ecb_encoded(ciphertext, block_bytes):
 blocks = set()
 for block_start in range(0, len(ciphertext), block_bytes):
 block = ciphertext[block_start : block_start+block_bytes]
 if block in blocks:
 return True
 blocks.add(block)
 return False

Both of those solutions should be O(n).

Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Loading
lang-py

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