How to resolve the algorithm Cistercian numerals step by step in the Factor programming language
How to resolve the algorithm Cistercian numerals step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: combinators continuations formatting grouping io kernel
literals math.order math.text.utils multiline sequences
splitting ;
CONSTANT: numerals $[
HEREDOC: END
+ +-+ + + + + +-+ + + +-+ + + +-+
| | | |\ |/ |/ | | | | | | | |
| | +-+ | + + + | + | + +-+ +-+
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
+ + + + + + + + + +
END
"\n" split harvest [ 5 group ] map flip
]
: precedence ( char char -- char )
2dup [ CHAR: + = ] either? [ 2drop CHAR: + ] [ max ] if ;
: overwrite ( glyph glyph -- newglyph )
[ [ precedence ] 2map ] 2map ;
: flip-slashes ( str -- new-str )
[
{
{ CHAR: / [ CHAR: \ ] }
{ CHAR: \ [ CHAR: / ] }
[ ]
} case
] map ;
: hflip ( seq -- newseq ) [ reverse flip-slashes ] map ;
: vflip ( seq -- newseq ) reverse [ flip-slashes ] map ;
: get-digits ( n -- seq ) 1 digit-groups 4 0 pad-tail ;
: check-cistercian ( n -- )
0 9999 between? [ "Must be from 0 to 9999." throw ] unless ;
: .cistercian ( n -- )
[ check-cistercian ] [ "%d:\n" printf ] [ get-digits ] tri
[ numerals nth ] map
[ { [ ] [ hflip ] [ vflip ] [ hflip vflip ] } spread ]
with-datastack [ ] [ overwrite ] map-reduce [ print ] each ;
{ 0 1 20 300 4000 5555 6789 8015 } [ .cistercian nl ] each
You may also check:How to resolve the algorithm MD4 step by step in the Tcl programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Wee Basic programming language
You may also check:How to resolve the algorithm Factorial step by step in the Groovy programming language
You may also check:How to resolve the algorithm Bitmap/Read an image through a pipe step by step in the Nim programming language