DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
peter279k profile image
peter279k

Here is my simple diamond solution with Python:

def diamond(n):
    # Make some diamonds!
    if n % 2 == 0 or n <= 0:
        return None
    if n == 1:
        return "*\n"

    array = list(range(1, n+1))
    result = ""
    white_space_count = list(range(1, int((n - (n % 2)) / 2)+1))
    white_space_count.reverse()
    white_space_index = 0
    for count in array:
        if count % 2 == 0:
            continue
        if white_space_index != len(white_space_count):
            result += (" " * white_space_count[white_space_index])
            white_space_index += 1
        result += ("*" * count) + "\n"

    array.reverse()
    array = array[1:]
    white_space_count.reverse()
    white_space_index = 0

    for count in array:
        if count % 2 == 0:
            continue
        if white_space_index != len(white_space_count):
            result += (" " * white_space_count[white_space_index])
            white_space_index += 1
        result += ("*" * count) + "\n"

    return result