DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
sapien profile image
Prateek Thapa • Edited

Just submitted my kata. <3 codewars.
Start from the middle, and go on appending stars to the top and bottom respectively.

Python:

def is_negative(n):
  return n <= 0

def is_even(n):
  return n % 2 == 0

def stars_with_spaces(num_stars, num_spaces):
  return num_spaces * ' ' + num_stars * '*' + '\n'

def diamond(n):
  if (is_negative(n) or is_even(n)):
    return None

  res = [None] * n
  mid_point = int((n - 1) / 2)

  num_stars = n
  num_spaces = 0

  for i in range(mid_point + 1)[::-1]:
    stars = stars_with_spaces(num_stars, num_spaces)

    res[mid_point - num_spaces] = stars
    res[mid_point + num_spaces] = stars

    num_spaces += 1 
    num_stars -= 2

  return ('').join(res)