aboutsummaryrefslogtreecommitdiff
path: root/challenge-256/jeanluc2020/python/ch-2.py
blob: 9f7abb5e098170899da7be8d4984ff542698bb8a (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
50
51
52
53
54
55
56
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK2
#
# Task 2: Merge Strings
# =====================
#
# You are given two strings, $str1 and $str2.
#
# Write a script to merge the given strings by adding in alternative order
# starting with the first string. If a string is longer than the other then
# append the remaining at the end.
#
## Example 1
##
## Input: $str1 = "abcd", $str2 = "1234"
## Output: "a1b2c3d4"
#
## Example 2
##
## Input: $str1 = "abc", $str2 = "12345"
## Output: "a1b2c345"
#
## Example 3
##
## Input: $str1 = "abcde", $str2 = "123"
## Output: "a1b2c3de"
#
############################################################
##
## discussion
##
############################################################
#
# Turn both strings into arrays, get elements from both arrays
# until the first one is empty, then add the remainder of the
# second one.

def merge_strings(str1: str, str2: str) -> None:
    print(f"Input: \"{str1}\", \"{str2}\"")
    output = ""
    first = list(str1)
    second = list(str2)
    while len(first) > 0:
        elem = first.pop(0)
        output += elem
        if len(second) > 0:
            elem = second.pop(0)
            output += elem
    if len(second) > 0:
        output += "".join(second)
    print(f"Output: \"{output}\"")

merge_strings("abcd", "1234");
merge_strings("abc", "12345");
merge_strings("abcde", "123");