diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-04 11:07:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-04 11:07:55 +0100 |
| commit | e2e10040e2c25022706bb9670e3f340d51816b9c (patch) | |
| tree | 748c4fe0e4fbaee10a3514425b6e537b27ae9df9 | |
| parent | e4b4e44838b1133b77013bc7da90fc238b4e5485 (diff) | |
| parent | e74007a0298a28ce431df61a219086397a560901 (diff) | |
| download | perlweeklychallenge-club-e2e10040e2c25022706bb9670e3f340d51816b9c.tar.gz perlweeklychallenge-club-e2e10040e2c25022706bb9670e3f340d51816b9c.tar.bz2 perlweeklychallenge-club-e2e10040e2c25022706bb9670e3f340d51816b9c.zip | |
Merge pull request #10202 from oWnOIzRi/week272
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 | 25 |
2 files changed, 45 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..f92d1c2528 --- /dev/null +++ b/challenge-272/steven-wilson/python/ch-2.py @@ -0,0 +1,25 @@ +#!/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 + ''' + if len(string) < 2: + raise ValueError("String argument should have at least 2 characters.") + + return sum(abs(ord(a) - ord(b)) for a, b in zip(string, string[1:])) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
