Sparkline in unicode: Difference between revisions
(+ D entry) |
|||
Line 15: | Line 15: | ||
* A space is not part of the generated sparkline. |
* A space is not part of the generated sparkline. |
||
* The sparkline may be accompanied by simple statistics of the data such as its range. |
* The sparkline may be accompanied by simple statistics of the data such as its range. |
||
=={{header|D}}== |
|||
{{trans|Python}} |
|||
<lang d>void main() { |
|||
import std.stdio, std.range, std.algorithm, std.conv, |
|||
std.string, std.regex; |
|||
"Numbers please separated by space/commas: ".write; |
|||
/*immutable*/ auto numbers = readln |
|||
.strip |
|||
.splitter(r"[\s,]+".regex) |
|||
.array /**/ |
|||
.to!(real[]); |
|||
immutable mm = numbers.reduce!(min, max); |
|||
"min: %5f; max: %5f".writefln(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; |
|||
}</lang> |
|||
The output is the same as the Python entry (but it only accepts one series of values at a time). |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
Revision as of 17:09, 18 June 2013
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:
- 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
- 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
- (note the mix of separators in this second case)!
- Notes
- A space is not part of the generated sparkline.
- The sparkline may be accompanied by simple statistics of the data such as its range.
D
<lang d>void main() {
import std.stdio, std.range, std.algorithm, std.conv, std.string, std.regex;
"Numbers please separated by space/commas: ".write; /*immutable*/ auto numbers = readln .strip .splitter(r"[\s,]+".regex) .array /**/ .to!(real[]); immutable mm = numbers.reduce!(min, max); "min: %5f; max: %5f".writefln(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;
}</lang> The output is the same as the Python entry (but it only accepts one series of values at a time).
Perl 6
<lang perl6>constant @bars = '▁' ... '█'; while prompt 'Numbers separated by anything: ' -> $_ {
my @numbers = map +*, .comb(/ '-'? \d+ ['.' \d+]? /); my ($mn,$mx) = @numbers.minmax.bounds; say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')"; my $div = ($mx - $mn) / (@bars - 1); say @bars[ (@numbers X- $mn) X/ $div ].join;
}</lang>
- Output:
Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9 min: 9.000000; max: 72.000000 ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ Numbers separated by anything: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5 min: 0.500000; max: 7.500000 ▂▁▄▃▆▅█▇ Numbers separated by anything: 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3 min: -4.000000; max: 3.000000 █▇▆▅▄▃▂▁▂▃▄▅▆▇█ Numbers separated by anything: ^D
Python
<lang python>import re try: raw_input except: raw_input = input
- Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608
try: bar = list(u'▁▂▃▄▅▆▇█') except: bar = list('▁▂▃▄▅▆▇█') barcount = len(bar) - 1 while True:
line = raw_input('Numbers please separated by space/commas: ') numbers = [float(n) for n in re.split(r'[\s,]+', line.strip())] mn, mx = min(numbers), max(numbers) extent = mx - mn sparkline = .join(bar[int( (n - mn) / extent * barcount)] for n in numbers) print('min: %5f; max: %5f' % (mn, mx)) print(sparkline)</lang>
- Output:
Numbers separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 min: 1.000000; max: 7.000000 ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ Numbers separated by space/commas: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 min: 0.500000; max: 7.500000 ▂▁▄▃▆▅█▇