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

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Cistercian numerals step by step in the Go 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 Go programming language

This code is written in the Go programming language and is a program that prints numbers in a seven-segment display format.

The program starts by initializing a global variable n, which is a 15x11 array of strings, to the value of " " (a single space character).

Next, the initN function initializes the n array to the default state of the seven-segment display, which is with all segments off.

The horiz, verti, diagd, and diagu functions are used to draw horizontal, vertical, diagonal down, and diagonal up lines on the display, respectively.

The draw map is a map from integers to functions.

The initDraw function initializes the draw map with functions that draw the different segments of the seven-segment display.

The printNumeral function prints the current state of the n array to the console.

The main function initializes the draw map and then iterates over a list of numbers, printing each number in a seven-segment display format.

For each number, the main function first initializes the n array to the default state of the seven-segment display.

Then, the main function calls the corresponding functions from the draw map to draw the segments of the seven-segment display for the given number.

Finally, the printNumeral function is called to print the current state of the n array to the console.

Here is an example of the output of the program:

0:
xxxxxxx
x       x
x       x
x       x
xxxxxxx

1:
x
x
x
x
x
x

20:
xxxxxxx
x       x
xxxxxx
x       x
xxxxxxx

300:
xxxxxxx
x       x
xxxxxx
x       x
xxxxxxx

4000:
x       x
x       x
x xxxxxxx
x       x
x       x

5555:
xxxxxxx
x       x
x xxxxxx
x       x
xxxxxxx

6789:
xxxxxxx
x       x
x xxxxxx
x xxxxxxx
xxxxxxx

9999:
xxxxxxx
x       x
xxxxxxx
x       x
xxxxxxx

Source code in the go programming language

package main

import "fmt"

var n = make([][]string, 15)

func initN() {
    for i := 0; i < 15; i++ {
        n[i] = make([]string, 11)
        for j := 0; j < 11; j++ {
            n[i][j] = " "
        }
        n[i][5] = "x"
    }
}

func horiz(c1, c2, r int) {
    for c := c1; c <= c2; c++ {
        n[r][c] = "x"
    }
}

func verti(r1, r2, c int) {
    for r := r1; r <= r2; r++ {
        n[r][c] = "x"
    }
}

func diagd(c1, c2, r int) {
    for c := c1; c <= c2; c++ {
        n[r+c-c1][c] = "x"
    }
}

func diagu(c1, c2, r int) {
    for c := c1; c <= c2; c++ {
        n[r-c+c1][c] = "x"
    }
}

var draw map[int]func() // map contains recursive closures

func initDraw() {
    draw = map[int]func(){
        1: func() { horiz(6, 10, 0) },
        2: func() { horiz(6, 10, 4) },
        3: func() { diagd(6, 10, 0) },
        4: func() { diagu(6, 10, 4) },
        5: func() { draw[1](); draw[4]() },
        6: func() { verti(0, 4, 10) },
        7: func() { draw[1](); draw[6]() },
        8: func() { draw[2](); draw[6]() },
        9: func() { draw[1](); draw[8]() },

        10: func() { horiz(0, 4, 0) },
        20: func() { horiz(0, 4, 4) },
        30: func() { diagu(0, 4, 4) },
        40: func() { diagd(0, 4, 0) },
        50: func() { draw[10](); draw[40]() },
        60: func() { verti(0, 4, 0) },
        70: func() { draw[10](); draw[60]() },
        80: func() { draw[20](); draw[60]() },
        90: func() { draw[10](); draw[80]() },

        100: func() { horiz(6, 10, 14) },
        200: func() { horiz(6, 10, 10) },
        300: func() { diagu(6, 10, 14) },
        400: func() { diagd(6, 10, 10) },
        500: func() { draw[100](); draw[400]() },
        600: func() { verti(10, 14, 10) },
        700: func() { draw[100](); draw[600]() },
        800: func() { draw[200](); draw[600]() },
        900: func() { draw[100](); draw[800]() },

        1000: func() { horiz(0, 4, 14) },
        2000: func() { horiz(0, 4, 10) },
        3000: func() { diagd(0, 4, 10) },
        4000: func() { diagu(0, 4, 14) },
        5000: func() { draw[1000](); draw[4000]() },
        6000: func() { verti(10, 14, 0) },
        7000: func() { draw[1000](); draw[6000]() },
        8000: func() { draw[2000](); draw[6000]() },
        9000: func() { draw[1000](); draw[8000]() },
    }
}

func printNumeral() {
    for i := 0; i < 15; i++ {
        for j := 0; j < 11; j++ {
            fmt.Printf("%s ", n[i][j])
        }
        fmt.Println()
    }
    fmt.Println()
}

func main() {
    initDraw()
    numbers := []int{0, 1, 20, 300, 4000, 5555, 6789, 9999}
    for _, number := range numbers {
        initN()
        fmt.Printf("%d:\n", number)
        thousands := number / 1000
        number %= 1000
        hundreds := number / 100
        number %= 100
        tens := number / 10
        ones := number % 10
        if thousands > 0 {
            draw[thousands*1000]()
        }
        if hundreds > 0 {
            draw[hundreds*100]()
        }
        if tens > 0 {
            draw[tens*10]()
        }
        if ones > 0 {
            draw[ones]()
        }
        printNumeral()
    }
}


  

You may also check:How to resolve the algorithm Empty program step by step in the Frink programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the jq programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the Ursa programming language
You may also check:How to resolve the algorithm Variables step by step in the Pascal programming language
You may also check:How to resolve the algorithm Program name step by step in the Applesoft BASIC programming language