How to resolve the algorithm Range expansion step by step in the UNIX Shell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Range expansion step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).
Expand the range description: Note that the second element above, is the range from minus 3 to minus 1.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Range expansion step by step in the UNIX Shell programming language
Source code in the unix programming language
#!/usr/bin/bash
range_expand () (
IFS=,
set -- $1
n=$#
for element; do
if [[ $element =~ ^(-?[0-9]+)-(-?[0-9]+)$ ]]; then
set -- "$@" $(eval echo "{${BASH_REMATCH[1]}..${BASH_REMATCH[2]}}")
else
set -- "$@" $element
fi
done
shift $n
echo "$@"
# to return a comma-separated value: echo "${*// /,}"
)
range_expand "-6,-3--1,3-5,7-11,14,15,17-20"
You may also check:How to resolve the algorithm Superpermutation minimisation step by step in the Elixir programming language
You may also check:How to resolve the algorithm Factorions step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Metered concurrency step by step in the Erlang programming language
You may also check:How to resolve the algorithm Power set step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Liberty BASIC programming language