aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-272/steven-wilson/python/ch-1.py20
-rw-r--r--challenge-272/steven-wilson/python/ch-2.py22
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)