Created
June 4, 2017 20:30
-
-
Save mortymacs/b8c73cfef11c1c20c1106665e206cb3d to your computer and use it in GitHub Desktop.
Calculate String ord in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Input should be in alphabet sort and should not miss word(s) in this order. "ABC" is correct and "AC" is incorrect inputs.