comp-think / 2023-2024

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2023/2024).
14 stars 0 forks source link

Exercise 3: Character Combination #37

Open arcangelo7 opened 6 months ago

arcangelo7 commented 6 months ago

Develop a function def combine_characters(string1, string2) that alternately combines the characters of two strings. If one string is longer, the remaining characters should be added to the end of the result.

combine_characters("abc", "1234")
# a1b2c34
FranciscoWu commented 6 months ago

def combine_characters(string1, string2):
    combined_string = ""
    longest_string = max([string1,string2],key = len)
    shortest_string = min([string1,string2], key = len)
    i = 0
    while i < len(shortest_string):
        combined_string += string1[i]
        combined_string += string2[i]
        i += 1
    combined_string += longest_string[len(shortest_string):]
    return combined_string

if __name__ == "__main__":
    string1 = "Thinking"
    string2 = "Compuational"
    print(combine_characters(string1, string2))
rufferbaraldi commented 6 months ago
string1 = "789"
string2 = "12345"

def combine_characters(string1, string2):
    combined_result = ""
    len1 = len(string1)
    len2 = len(string2)

    if len1 >= len2:
        max_len = len1
    else:
        max_len = len2

    for i in range(max_len):
        if i < len1:
            combined_result += string1[i]
        if i < len2:
            combined_result += string2[i]
    return combined_result

print(combine_characters(string1, string2))
enricabruno commented 6 months ago
def combine_characters(string1, string2):
    combined = ''
    i, j = 0, 0

    while i < len(string1) and j < len(string2):
        combined += string1[i] + string2[j]
        i += 1
        j += 1

    combined += string1[i:] + string2[j:]

    return combined

print(combine_characters("abc", "12345"))
MariaFrancesca6 commented 6 months ago
def combine_characters(str1, str2):
    result = list()
    i = 0
    j = 0
    while i < len(str1) and j < len(str2):
        result.append(str1[i])
        result.append(str2[j])
        i += 1
        j += 1
    result.extend(str1[i:] + str2[j:])
    return "".join(result)

print(combine_characters("natale","capodanno"))
qwindici commented 6 months ago
def combine_characters(string1, string2):
    long = string1 if len(string1) > len(string2) else string2
    short = string1 if long == string2 else string2
    result = ""
    for i in range(len(short + long)):
        if i < len(short):
            result += short[i]
        if i < len(long):
            result += long[i]

    return "".join(result)
valetedd commented 6 months ago
def combine_characters(string1, string2):
    combined_string = ""
    if len(string1) > len(string2):
        longer = len(string1)
    else:
        longer = len(string2)

    for i in range(longer + 1):
        if i <= len(string1) - 1:
            combined_string += string1[i]
        if i <= len(string2) - 1:
            combined_string += string2[i]

    return combined_string