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
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
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