diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-08-23 11:01:46 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-08-26 15:51:26 +0200 |
| commit | fc1d2d045a1151eab74dfb69dea9fd2736c24023 (patch) | |
| tree | 43ccbdef0a86939325569ec1e50dc0b160d075cf | |
| parent | ea5e94b9fccd34813db40b04c286e76b30b47c59 (diff) | |
| download | perlweeklychallenge-club-fc1d2d045a1151eab74dfb69dea9fd2736c24023.tar.gz perlweeklychallenge-club-fc1d2d045a1151eab74dfb69dea9fd2736c24023.tar.bz2 perlweeklychallenge-club-fc1d2d045a1151eab74dfb69dea9fd2736c24023.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-179/jo-37/perl/ch-2.pl | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-179/jo-37/perl/ch-2.pl b/challenge-179/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..4b0f69da83 --- /dev/null +++ b/challenge-179/jo-37/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use v5.16; +use List::MoreUtils 'minmax'; +use Math::Round 'round'; + +die <<EOS unless @ARGV; +usage: $0 N1 N2 ... + +N1 N2 ... + Numbers to be represented in a sparkline-like bar chart. + +EOS + + +### Input and Output + +binmode STDOUT, ':utf8'; +say barchart(@ARGV); + + +### Implementation + +# A vague solution for a vague task: I didn't find any Unicode +# symbols that could be used to produce sparklines. Using +# ${SEARCH_ENGINE} revealed some implementations that produce small +# bar charts using Unicode symbols. Following this approach. +# +# Starting with U+2581 ("\N{LOWER ONE EIGHTH BLOCK}"), there are eight +# consecutive block symbols having a height of one to eight eights that +# will be used here. Returning a list of characters. + +sub barchart { + my ($min, $max) = minmax @_; + my $scale = 7 / (($max - $min) || 1); + map chr(0x2581 + round(($_ - $min) * $scale)), @_; +} |
