aboutsummaryrefslogtreecommitdiff
path: root/challenge-331/packy-anderson/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-331/packy-anderson/python/ch-2.py')
-rwxr-xr-xchallenge-331/packy-anderson/python/ch-2.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-331/packy-anderson/python/ch-2.py b/challenge-331/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..fb0049aae5
--- /dev/null
+++ b/challenge-331/packy-anderson/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+def buddy_string(source, target):
+ # put the source characters in an array
+ src = list(source)
+ # loop over the first to all but last characters
+ for i in range(len(src) - 1):
+ # generate a list of character positions
+ slice = list(range(len(src)))
+ # swap the $i-th and following positions
+ (slice[i], slice[i+1]) = (slice[i+1], slice[i])
+ # test to see if it matches the target!
+ if ''.join([ src[c] for c in slice ]) == target:
+ return True
+ # womp-womp! nothing matched!
+ return False
+
+def solution(source, target):
+ print(f'Input: $source = "{source}"')
+ print(f' $target = "{target}"')
+ print(f'Output: {buddy_string(source, target)}')
+
+print('Example 1:')
+solution("fuck", "fcuk")
+
+print('\nExample 2:')
+solution("love", "love")
+
+print('\nExample 3:')
+solution("fodo", "food")
+
+print('\nExample 4:')
+solution("feed", "feed")