From 42d94c4d037146a9de2471b4ad51569eb0296e26 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 21 Dec 2023 00:16:21 -0500 Subject: Challenge 248 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-248/packy-anderson/python/ch-1.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 challenge-248/packy-anderson/python/ch-1.py (limited to 'challenge-248/packy-anderson/python/ch-1.py') diff --git a/challenge-248/packy-anderson/python/ch-1.py b/challenge-248/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..e4f309808e --- /dev/null +++ b/challenge-248/packy-anderson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def shortestDistance(s, c): + # split the string into an array of characters + strchar = list(s) + # find the positions of the target char + pos = [ x for x in range(len(s)) if strchar[x] == c ] + + output = [] + for i in range(len(s)): + # find the distances + distance = [ abs(i - p) for p in pos ] + # find the minimum distance + output.append( min(distance) ) + return output + +def comma_join(arr): + return ','.join(map(lambda i: str(i), arr)) + +def solution(s, c): + print(f'Input: $str = "{s}", $char = "{c}"') + output = shortestDistance(s, c) + print(f'Output: ({comma_join(output)})') + +print('Example 1:') +solution("loveleetcode", "e") + +print('\nExample 2:') +solution("aaab", "b") \ No newline at end of file -- cgit