diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-08-29 04:23:07 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-08-29 04:23:07 +0100 |
| commit | 912de14ab2434cf7f41dbd8479a3da5613a1622b (patch) | |
| tree | af6757e9fc67dd789251b5314a1d2af6661870b7 | |
| parent | 877d5097cf503a2fc82a81e0ac5396a8530fbc78 (diff) | |
| download | perlweeklychallenge-club-912de14ab2434cf7f41dbd8479a3da5613a1622b.tar.gz perlweeklychallenge-club-912de14ab2434cf7f41dbd8479a3da5613a1622b.tar.bz2 perlweeklychallenge-club-912de14ab2434cf7f41dbd8479a3da5613a1622b.zip | |
- Added guest contributions by Laurent Rosenfeld.
| -rw-r--r-- | challenge-179/laurent-rosenfeld/julia/ch-2.jl | 19 | ||||
| -rw-r--r-- | challenge-179/laurent-rosenfeld/python/ch-2.py | 17 | ||||
| -rw-r--r-- | challenge-179/laurent-rosenfeld/ruby/ch-2.rb | 19 | ||||
| -rw-r--r-- | challenge-179/laurent-rosenfeld/scala/ch-2.scala | 24 |
4 files changed, 79 insertions, 0 deletions
diff --git a/challenge-179/laurent-rosenfeld/julia/ch-2.jl b/challenge-179/laurent-rosenfeld/julia/ch-2.jl new file mode 100644 index 0000000000..fb24b765a9 --- /dev/null +++ b/challenge-179/laurent-rosenfeld/julia/ch-2.jl @@ -0,0 +1,19 @@ +function sparkline(test) + bars = '\u2581':'\u2588' + bar_count = length(bars) + min, max = extrema(test) + out = "" + for item in test + h = 1 + bar_count * (item - min) / (max - min) + h > bar_count && (h = bar_count) + out = out * string(bars[Int(floor(h))]) + end + return out * "\n" +end + +tests = [ [2, 4, 6, 8, 10, 12, 10, 8, 6, 4, 2], + [0, 1, 19, 20], [0, 999, 4000, 4999, 7000, 7999] ] +for test in tests + println(test, "\n") + println( sparkline(test)) +end diff --git a/challenge-179/laurent-rosenfeld/python/ch-2.py b/challenge-179/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..2f252f01f3 --- /dev/null +++ b/challenge-179/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,17 @@ +def sparkline(test): + bars = [chr(bar) for bar in range(9601, 9608+1)] + minim = min(test) + maxim = max(test) + scale = maxim - minim + length = len(bars) + line = "" + for item in test: + line += bars[min(int((item-minim) / scale * length), length - 1)] + return line + +tests = [ [2, 4, 6, 8, 10, 12, 10, 8, 6, 4, 2], \ + [0, 1, 19, 20], [0, 999, 4000, 4999, 7000, 7999] ] + +for test in tests: + print(test) + print(sparkline(test), "\n") diff --git a/challenge-179/laurent-rosenfeld/ruby/ch-2.rb b/challenge-179/laurent-rosenfeld/ruby/ch-2.rb new file mode 100644 index 0000000000..e53778cf2f --- /dev/null +++ b/challenge-179/laurent-rosenfeld/ruby/ch-2.rb @@ -0,0 +1,19 @@ +bars = ('▁'..'█').to_a +tests = [ [1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1], + [0, 1, 19, 20], [0, 999, 4000, 4999, 7000, 7999] ] +for test in tests + min, max = test.minmax + puts test.join(" ") + puts "min: %.2f; max: %.2f"% [min, max] + scale = (max - min) / (bars.size - 1) + line = "" + for item in test + h = bars.size * (item - min) / (max - min) + if h >= bars.size + h = bars.size - 1 + end + line += bars[h] + end + puts line + puts " " +end diff --git a/challenge-179/laurent-rosenfeld/scala/ch-2.scala b/challenge-179/laurent-rosenfeld/scala/ch-2.scala new file mode 100644 index 0000000000..c03b3c2fed --- /dev/null +++ b/challenge-179/laurent-rosenfeld/scala/ch-2.scala @@ -0,0 +1,24 @@ +object sparkLine extends App { + + def sparkline(test: Array[Int]): String = { + val bars = ('\u2581' to '\u2588') + var outl = "" + for (item <- test) { + var h = bars.length * (item - test.min) / (test.max - test.min) + if (h >= bars.length) { h = bars.length - 1 } + outl = outl + bars(h) + } + return outl + } + val tests = Array( + Array(1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1), + Array(0, 1, 19, 20), + Array(0, 999, 4000, 4999, 7000, 7999) + ) + for (test <- tests) { + println(test.mkString(" ")) + println("") + println(sparkline(test)) + println("") + } +} |
