Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Similarly to alecxe's answer alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 return checksum % 0x10000

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 return checksum % 0x10000

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 return checksum % 0x10000
deleted 19 characters in body
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 return checksum %=% 0x10000
 return checksum

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 checksum %= 0x10000
 return checksum

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 return checksum % 0x10000
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

Similarly to alecxe's answer, i would wrap the code in a function and make the salt a parameter.

I would use str.format for building the string (and still use a ternary).

I see no need for the explicit iterator variable, just inline it.

def checksum(str_a, str_b, salt):
 string = str_a if str_b == "***" else "{}{} {}".format(str_a, str_b, salt)
 string = [ord(x) for x in string]
 checksum = string[0]
 for counter, (x, y) in enumerate(zip(string[1:], string)):
 if counter % 2:
 checksum += x + 2 * y
 else:
 checksum += x * y
 checksum %= 0x10000
 return checksum
lang-py

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