DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
ganderzz profile image
Dylan Paulus • Edited on

Dirty Nim, planning to rewriting. :)

import math

proc diamond(num: int): string =
  if num < 0:
    return ""

  let midPoint = floorDiv(num, 2)

  for row in 0..num:
    let distanceFromMid = abs(row - midPoint)
    let starsInRow = num - distanceFromMid * 2

    for col in 0..num:
      if col >= distanceFromMid + starsInRow or col <= distanceFromMid:
        result &= " "
      else:
        result &= "*"

    result &= "\n"

if isMainModule:
  const num = 30

  echo diamond(num)