From 0ba374734bed44f440759e4659d07c8118114e50 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 24 Jul 2025 23:18:50 -0400 Subject: Challenge 331 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir that's starting to look more like Elixir 1 blog post --- challenge-331/packy-anderson/python/ch-2.py | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 challenge-331/packy-anderson/python/ch-2.py (limited to 'challenge-331/packy-anderson/python/ch-2.py') 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") -- cgit