diff options
| author | Packy Anderson <packy@cpan.org> | 2025-07-24 23:18:50 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-07-24 23:18:50 -0400 |
| commit | 0ba374734bed44f440759e4659d07c8118114e50 (patch) | |
| tree | e6c345778a119132a93106e1de63708634ed7442 /challenge-331/packy-anderson/python | |
| parent | 864b54240022daa9c51f4d30e531f1950d3d348c (diff) | |
| download | perlweeklychallenge-club-0ba374734bed44f440759e4659d07c8118114e50.tar.gz perlweeklychallenge-club-0ba374734bed44f440759e4659d07c8118114e50.tar.bz2 perlweeklychallenge-club-0ba374734bed44f440759e4659d07c8118114e50.zip | |
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
Diffstat (limited to 'challenge-331/packy-anderson/python')
| -rwxr-xr-x | challenge-331/packy-anderson/python/ch-1.py | 20 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/python/ch-2.py | 33 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-331/packy-anderson/python/ch-1.py b/challenge-331/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..c37a04e768 --- /dev/null +++ b/challenge-331/packy-anderson/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +def distinctAverages(nums): + pass + +def last_word(strVal): + return len(strVal.split()[-1]) + +def solution(strVal): + print(f'Input: $str = "{strVal}"') + print(f'Output: {last_word(strVal)}') + +print('Example 1:') +solution("The Weekly Challenge") + +print('\nExample 2:') +solution(" Hello World ") + +print('\nExample 3:') +solution("Let's begin the fun") 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") |
