aboutsummaryrefslogtreecommitdiff
path: root/challenge-272/steven-wilson/python/ch-2.py
blob: d33962b413081f98886da3dead6881b46524174b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3


def string_score(string):
    ''' Given a, string, return the score of the given string.

        The score of a string is defined as the sum of the absolute difference
        between the ASCII values of adjacent characters.
    >>> string_score('hello')
    13
    >>> string_score('perl')
    30
    >>> string_score('raku')
    37
    '''
    return sum(abs(ord(a) - ord(b)) for a, b in zip(string, string[1:]))


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)