How to resolve the algorithm Sparkline in unicode step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Sparkline in unicode step by step in the D programming language

Table of Contents

Problem Statement

A sparkline is a graph of successive values laid out horizontally where the height of the line is proportional to the values in succession.

Use the following series of Unicode characters to create a program that takes a series of numbers separated by one or more whitespace or comma characters and generates a sparkline-type bar graph of the values on a single line of output. The eight characters: '▁▂▃▄▅▆▇█' (Unicode values U+2581 through U+2588). Use your program to show sparklines for the following input, here on this page:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sparkline in unicode step by step in the D programming language

Source code in the d programming language

void main() {
    import std.stdio, std.range, std.algorithm, std.conv,
           std.string, std.regex;

    "Numbers please separated by space/commas: ".write;
    immutable numbers = readln
                        .strip
                        .splitter(r"[\s,]+".regex)
                        .array /**/
                        .to!(real[]);
    immutable mm = numbers.reduce!(min, max);
    writefln("min: %4f, max: %4f", mm[]);
    immutable bars = iota(9601, 9609).map!(i => i.to!dchar).dtext;
    immutable div = (mm[1] - mm[0]) / (bars.length - 1);
    numbers.map!(n => bars[cast(int)((n - mm[0]) / div)]).writeln;
}


  

You may also check:How to resolve the algorithm Closest-pair problem step by step in the F# programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Singleton step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Smalltalk programming language