diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-01-22 21:45:09 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-01-22 21:45:09 +0100 |
| commit | 898efbfec41a9cba96da449d2a550ec17370de2d (patch) | |
| tree | cfecbe0f8f0bdbd05b23041cd34789c6c263f9e9 /challenge-253/jeanluc2020/python/ch-1.py | |
| parent | 8db85cd488ebedbbda40cca5403676ce0ed8e072 (diff) | |
| download | perlweeklychallenge-club-898efbfec41a9cba96da449d2a550ec17370de2d.tar.gz perlweeklychallenge-club-898efbfec41a9cba96da449d2a550ec17370de2d.tar.bz2 perlweeklychallenge-club-898efbfec41a9cba96da449d2a550ec17370de2d.zip | |
Add solution 253.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-253/jeanluc2020/python/ch-1.py')
| -rwxr-xr-x | challenge-253/jeanluc2020/python/ch-1.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-253/jeanluc2020/python/ch-1.py b/challenge-253/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..466d4712c2 --- /dev/null +++ b/challenge-253/jeanluc2020/python/ch-1.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/#TASK1 +# +# Task 1: Split Strings +# ===================== +# +# You are given an array of strings and a character separator. +# +# Write a script to return all words separated by the given character excluding +# empty string. +# +# Example 1 +# +# Input: @words = ("one.two.three","four.five","six") +# $separator = "." +# Output: "one","two","three","four","five","six" +# +# Example 2 +# +# Input: @words = ("$perl$$", "$$raku$") +# $separator = "$" +# Output: "perl","raku" +# +############################################################ +## +## discussion +## +############################################################ +# +# This is a classic one-liner problem: join the words into a single string, +# then split this string into an array of strings at the separator, then +# only keep the non-empty strings by grepping for strings that contain one +# character. +# + +import re + +def split_strings(separator: str, words: list) -> None: + print("Input: (\"", "\", \"".join(words), "\"), '", separator, "'", sep="") + output = [] + for value in separator.join(words).split(separator): + if len(value) > 0: + output.append(value) + print("Output: (\"", "\", \"".join(output), "\")", sep="") + + +split_strings(".", ["one.two.three","four.five","six"]) +split_strings('$', ['$perl$$', '$$raku$']) + |
