Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created June 4, 2017 20:30
Show Gist options
  • Select an option

  • Save mortymacs/b8c73cfef11c1c20c1106665e206cb3d to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/b8c73cfef11c1c20c1106665e206cb3d to your computer and use it in GitHub Desktop.
Calculate String ord in Python
def calc_string_ord(s, duplicate=1):
n = ord(s[-1]) - ord(s[0])
total = int( ord(s[0]) * (len(s) // duplicate) + ((n * (n + 1)) / 2) ) * duplicate
return total
# Example usage
print (calc_string_ord("ABCD"))
# Result: 266
print(calc_string_ord("aabbccdd", 2))
# Result: 788
@mortymacs
Copy link
Copy Markdown
Author

Note: Input should be in alphabet sort and should not miss word(s) in this order. "ABC" is correct and "AC" is incorrect inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment