How to resolve the algorithm Sort an outline at every level step by step in the J programming language
How to resolve the algorithm Sort an outline at every level step by step in the J programming language
Table of Contents
Problem Statement
Write and test a function over an indented plain text outline which either:
Your code should detect and warn of at least two types of inconsistent indentation:
Your code should be able to detect and handle both tab-indented, and space-indented (e.g. 4 space, 2 space etc) outlines, without being given any advance warning of the indent characters used, or the size of the indent units. You should also be able to specify different types of sort, for example, as a minimum, both ascending and descending lexical sorts. Your sort should not alter the type or size of the indentation units used in the input outline.
(For an application of Indent Respectful Sort, see the Sublime Text package of that name. The Python source text [1] is available for inspection on Github).
Tests
The output sequence of an ascending lexical sort of each level should be: The output sequence of a descending lexical sort of each level should be:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sort an outline at every level step by step in the J programming language
Source code in the j programming language
parse2=: {{
((=<./)y (1 i.~ -.@e.)S:0 m) m {{
({.y),<m parse2^:(*@#)}.y
}};.1 y
}}
parseout=: {{
ws=. ' ',TAB
lines=: <;.2 y
indents=: lines (1 i.~ -.@e.)S:0 ws
unit=: +./indents
if. -. (-: i.@#)~.indents%unit do.
echo 'inconsistent indent widths'
end.
if. 1~:#~.;indents unit{{<(1,:m) <;._3 x{.;y }}"0 lines do.
echo 'inconsistent use of whitespace characters'
end.
ws parse2 lines
}} :.unparse
sortout=: {{ if. L.y do. u~ ({."1 y),.u sortout each}."1 y else. u~y end. }}
unparse=: {{ ;<S:0 y }}
/:sortout&.parseout sample1
alpha
epsilon
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
\:sortout&.parseout sample1
zeta
gamma
mu
lambda
kappa
delta
beta
alpha
theta
iota
epsilon
/:sortout&.parseout sample2
alpha
epsilon
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
/:sortout&.parseout sample3
inconsistent indent widths
inconsistent use of whitespace characters
alpha
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
/:sortout&.parseout sample4
inconsistent indent widths
alpha
epsilon
iota
theta
zeta
gamma
delta
sample1=: {{)n
zeta
beta
gamma
lambda
kappa
mu
delta
alpha
theta
iota
epsilon
}}
sample2=:{{)n
zeta
gamma
mu
lambda
kappa
delta
beta
alpha
theta
iota
epsilon
}}
sample3=:{{)n
alpha
epsilon
iota
theta
zeta
beta
delta
gamma
kappa
lambda
mu
}}
sample4=:{{)n
zeta
beta
gamma
lambda
kappa
mu
delta
alpha
theta
iota
epsilon
}}
You may also check:How to resolve the algorithm Search a list step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the C# programming language
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the C programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Lua programming language