How to resolve the algorithm Cistercian numerals step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cistercian numerals step by step in the Nim programming language

Table of Contents

Problem Statement

Cistercian numerals were used across Europe by Cistercian monks during the Late Medieval Period as an alternative to Roman numerals. They were used to represent base 10 integers from 0 to 9999. All Cistercian numerals begin with a vertical line segment, which by itself represents the number 0. Then, glyphs representing the digits 1 through 9 are optionally added to the four quadrants surrounding the vertical line segment. These glyphs are drawn with vertical and horizontal symmetry about the initial line segment. Each quadrant corresponds to a digit place in the number: Please consult the following image for examples of Cistercian numerals showing each glyph: [1] Due to the inability to upload images to Rosetta Code as of this task's creation, showing output here on this page is not required. However, it is welcomed — especially for text output.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cistercian numerals step by step in the Nim programming language

Source code in the nim programming language

const Size = 15

type Canvas = array[Size, array[Size, char]]


func horizontal(canvas: var Canvas; col1, col2, row: Natural) =
  for col in col1..col2:
    canvas[row][col] = 'x'


func vertical(canvas: var Canvas; row1, row2, col: Natural) =
  for row in row1..row2:
    canvas[row][col] = 'x'


func diagd(canvas: var Canvas; col1, col2, row: Natural) =
  for col in col1..col2:
    canvas[row + col - col1][col] = 'x'


func diagu(canvas: var Canvas; col1, col2, row: Natural) =
  for col in col1..col2:
    canvas[row - col + col1][col] = 'x'


func drawPart(canvas: var Canvas; value: Natural) =

  case value
  of 1:
    canvas.horizontal(6, 10, 0)
  of 2:
    canvas.horizontal(6, 10, 4)
  of 3:
    canvas.diagd(6, 10, 0)
  of 4:
    canvas.diagu(6, 10, 4)
  of 5:
    canvas.drawPart(1)
    canvas.drawPart(4)
  of 6:
    canvas.vertical(0, 4, 10)
  of 7:
    canvas.drawPart(1)
    canvas.drawPart(6)
  of 8:
    canvas.drawPart(2)
    canvas.drawPart(6)
  of 9:
    canvas.drawPart(1)
    canvas.drawPart(8)
  of 10:
    canvas.horizontal(0, 4, 0)
  of 20:
    canvas.horizontal(0, 4, 4)
  of 30:
    canvas.diagu(0, 4, 4)
  of 40:
    canvas.diagd(0, 4, 0)
  of 50:
    canvas.drawPart(10)
    canvas.drawPart(40)
  of 60:
    canvas.vertical(0, 4, 0)
  of 70:
    canvas.drawPart(10)
    canvas.drawPart(60)
  of 80:
    canvas.drawPart(20)
    canvas.drawPart(60)
  of 90:
    canvas.drawPart(10)
    canvas.drawPart(80)
  of 100:
    canvas.horizontal(6, 10, 14)
  of 200:
    canvas.horizontal(6, 10, 10)
  of 300:
    canvas.diagu(6, 10, 14)
  of 400:
    canvas.diagd(6, 10, 10)
  of 500:
    canvas.drawPart(100)
    canvas.drawPart(400)
  of 600:
    canvas.vertical(10, 14, 10)
  of 700:
    canvas.drawPart(100)
    canvas.drawPart(600)
  of 800:
    canvas.drawPart(200)
    canvas.drawPart(600)
  of 900:
    canvas.drawPart(100)
    canvas.drawPart(800)
  of 1000:
    canvas.horizontal(0, 4, 14)
  of 2000:
    canvas.horizontal(0, 4, 10)
  of 3000:
    canvas.diagd(0, 4, 10)
  of 4000:
    canvas.diagu(0, 4, 14)
  of 5000:
    canvas.drawPart(1000)
    canvas.drawPart(4000)
  of 6000:
    canvas.vertical(10, 14, 0)
  of 7000:
    canvas.drawPart(1000)
    canvas.drawPart(6000)
  of 8000:
    canvas.drawPart(2000)
    canvas.drawPart(6000)
  of 9000:
    canvas.drawPart(1000)
    canvas.drawPart(8000)
  else:
    raise newException(ValueError, "wrong value for 'drawPart'")


func draw(canvas: var Canvas; value: Natural) =

  var val = value
  let thousands = val div 1000
  val = val mod 1000
  let hundreds = val div 100
  val = val mod 100
  let tens = val div 10
  let ones = val mod 10

  if thousands != 0:
    canvas.drawPart(1000 * thousands)
  if hundreds != 0:
    canvas.drawPart(100 * hundreds)
  if tens != 0:
    canvas.drawPart(10 * tens)
  if ones != 0:
    canvas.drawPart(ones)


func cistercian(n: Natural): Canvas =
  for row in result.mitems:
    for cell in row.mitems: cell = ' '
    row[5] = 'x'
  result.draw(n)


proc `$`(canvas: Canvas): string =
  for row in canvas:
    for cell in row:
      result.add cell
    result.add '\n'


when isMainModule:

  for number in [0, 1, 20, 300, 4000, 5555, 6789, 9999]:
    echo number, ':'
    echo cistercian(number)


  

You may also check:How to resolve the algorithm Munching squares step by step in the Go programming language
You may also check:How to resolve the algorithm Variables step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Maxima programming language
You may also check:How to resolve the algorithm Checkpoint synchronization step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the langur programming language