aboutsummaryrefslogtreecommitdiff
path: root/challenge-253/jeanluc2020/python/ch-1.py
blob: 466d4712c2a92686c9d3f83b334ef99e3de508ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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$'])