How to resolve the algorithm Nested templated data step by step in the Factor programming language
How to resolve the algorithm Nested templated data step by step in the Factor programming language
Table of Contents
Problem Statement
A template for data is an arbitrarily nested tree of integer indices. Data payloads are given as a separate mapping, array or other simpler, flat, association of indices to individual items of data, and are strings. The idea is to create a data structure with the templates' nesting, and the payload corresponding to each index appearing at the position of each index. Answers using simple string replacement or regexp are to be avoided. The idea is to use the native, or usual implementation of lists/tuples etc of the language and to hierarchically traverse the template to generate the output. Given the following input template t and list of payloads p: The correct output should have the following structure, (although spacing and linefeeds may differ, the nesting and order should follow):
- Generate the output for the above template, t.
- Show which payloads remain unused.
- Give some indication/handling of indices without a payload. Show output on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Nested templated data step by step in the Factor programming language
Source code in the factor programming language
USING: formatting kernel literals math sequences sequences.deep ;
IN: rosetta-code.nested-template-data
CONSTANT: payloads $[ 7 <iota> [ "Payload#%d" sprintf ] map ]
: insert-payloads ( template -- data-structure )
[ dup fixnum? [ payloads ?nth ] when ] deep-map ;
{ { { 1 2 }
{ 3 4 1 }
5 } }
{ { { 1 2 }
{ 10 4 1 }
5 } }
[ dup insert-payloads "Template: %u\nData Structure: %u\n"
printf ] bi@
You may also check:How to resolve the algorithm Integer sequence step by step in the min programming language
You may also check:How to resolve the algorithm Metallic ratios step by step in the Quackery programming language
You may also check:How to resolve the algorithm Numbers with equal rises and falls step by step in the Perl programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the GAP programming language
You may also check:How to resolve the algorithm Lychrel numbers step by step in the Python programming language