Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Bug

Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

Style

String literals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habit to put a space after the # sign, it eases readability.

You also don't need to put parentheses around simple conditions.

Lastly, PEP 8 recommends to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterates over the input and joins the converted output, the other that generates the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...

#Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

String literals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habit to put a space after the # sign, it eases readability.

You also don't need to put parentheses around simple conditions.

Lastly, PEP 8 recommends to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterates over the input and joins the converted output, the other that generates the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...

Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

Style

String literals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habit to put a space after the # sign, it eases readability.

You also don't need to put parentheses around simple conditions.

Lastly, PEP 8 recommends to use ALL_CAPS to name constants, like LOOKUP.

Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterates over the input and joins the converted output, the other that generates the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...

#Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

String litteralsliterals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habbithabit to put a space after the # sign, it eases readability.

You also don't need to put parenthesisparentheses around simple conditions.

Lastly, PEP 8 recommendrecommends to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterateiterates over the input and joinjoins the converted output, the other that generategenerates the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...

#Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

String litterals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habbit to put a space after the # sign, it eases readability.

You also don't need to put parenthesis around simple conditions.

Lastly, PEP 8 recommend to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterate over the input and join the converted output, the other that generate the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...

#Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

String literals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habit to put a space after the # sign, it eases readability.

You also don't need to put parentheses around simple conditions.

Lastly, PEP 8 recommends to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterates over the input and joins the converted output, the other that generates the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...
Source Link

#Bug

Letters J & K, U & W, and V & X have the same pattern. This is not what the specification says: either I & J and U & V shares the same pattern, or none of the letters do.

#Style

String litterals ain't comments. If you want two lines of comments, you start your two lines with a # each. Also take the habbit to put a space after the # sign, it eases readability.

You also don't need to put parenthesis around simple conditions.

Lastly, PEP 8 recommend to use ALL_CAPS to name constants, like LOOKUP.

#Generators

Concatenating strings using += is slow as strings are immutables and Python need to copy the whole string each time you add a bit at the end. For better performances, it is advised to use ''.join. To do this, it is easier to separate each function into two: one that iterate over the input and join the converted output, the other that generate the converted output. Something like:

def encrypt_letter(letter):
 """Convert a plain-text ascii letter into its Bacon's form.
 Let symbols and non ascii letter fail through gracefully.
 """
 return LOOKUP.get(letter.upper(), letter.lower())
def encrypt(message):
 """Encrypt a message using Bacon's cipher"""
 return ''.join(map(encrypt_letter, message))

One more thing to note with this implementation: you shouldn't require that the input is properly formatted, you should enforce it in your functions (usage of upper and lower within your function, not before its call)

#Do-while

In Python, a do-while loop is emulated using something along the lines of:

while True:
 do_something()
 if ...:
 break

In your case, having the if directly as the first action of the while and an else: break, you just need a simple while loop:

while i < len(message) - 4:
 substr = message[i:i + 5]
 ...
lang-py

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