diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-21 09:48:48 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-21 09:48:48 +0000 |
| commit | f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc (patch) | |
| tree | ccebb955018077d92a371f9e5db31152bc8d15a1 /challenge-248/packy-anderson/python/ch-1.py | |
| parent | a04ded57f1d184eefeb0f192502338b46064b8e9 (diff) | |
| parent | 3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf (diff) | |
| download | perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.tar.gz perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.tar.bz2 perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-248/packy-anderson/python/ch-1.py')
| -rwxr-xr-x | challenge-248/packy-anderson/python/ch-1.py | 29 |
1 files changed, 29 insertions, 0 deletions
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 |
