diff options
| author | Steven <steven1170@zoho.eu> | 2024-06-03 14:22:42 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-06-03 14:22:42 +0100 |
| commit | 6006e625cdb6ccb9bf4bae4ff0b6bfff27ba2c59 (patch) | |
| tree | 7ba241ed72281f2a91460dd90ab86fa0a8a7e122 | |
| parent | 6b1c67381886ee30e0dd9f9356340350101139b8 (diff) | |
| download | perlweeklychallenge-club-6006e625cdb6ccb9bf4bae4ff0b6bfff27ba2c59.tar.gz perlweeklychallenge-club-6006e625cdb6ccb9bf4bae4ff0b6bfff27ba2c59.tar.bz2 perlweeklychallenge-club-6006e625cdb6ccb9bf4bae4ff0b6bfff27ba2c59.zip | |
add solutions week 272 in python
| -rw-r--r-- | challenge-272/steven-wilson/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-272/steven-wilson/python/ch-2.py | 22 |
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-272/steven-wilson/python/ch-1.py b/challenge-272/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..145c0306da --- /dev/null +++ b/challenge-272/steven-wilson/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + + +def defang_ip_address(address): + ''' Given a valid IPv4 address, return the defanged version of the given IP + address. + + A defanged IP address replaces every period “.” with “[.]". + >>> defang_ip_address('1.1.1.1') + '1[.]1[.]1[.]1' + >>> defang_ip_address('255.101.1.0') + '255[.]101[.]1[.]0' + ''' + return address.replace(".", "[.]") + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-272/steven-wilson/python/ch-2.py b/challenge-272/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..d33962b413 --- /dev/null +++ b/challenge-272/steven-wilson/python/ch-2.py @@ -0,0 +1,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) |
