From fa7cbaee4baf174ee416280d82a60fade6d3eaad Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Mon, 13 Feb 2017 22:00:06 +0700 Subject: Add /r/dailyprogrammer Challenge #302 [Hard] --- daily/302hard/README.md | 90 +++++++++++++++++++++++++++++++++++++++ daily/302hard/bargroup.pas | 104 +++++++++++++++++++++++++++++++++++++++++++++ daily/302hard/input | 43 +++++++++++++++++++ daily/302hard/output | 102 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 339 insertions(+) create mode 100644 daily/302hard/README.md create mode 100644 daily/302hard/bargroup.pas create mode 100644 daily/302hard/input create mode 100644 daily/302hard/output (limited to 'daily/302hard') diff --git a/daily/302hard/README.md b/daily/302hard/README.md new file mode 100644 index 0000000..3afdca3 --- /dev/null +++ b/daily/302hard/README.md @@ -0,0 +1,90 @@ +# [[2017-02-10] Challenge #302 [Hard] ASCII Histogram Maker: Part 2 - The Proper Histogram](https://www.reddit.com/r/dailyprogrammer/comments/5t7l07/20170210_challenge_302_hard_ascii_histogram_maker/) + +## Description + +Most of us are familiar with the histogram chart - a representation of a +frequency distribution by means of rectangles whose widths represent class +intervals and whose areas are proportional to the corresponding frequencies. It +is similar to a bar chart, but a histogram groups numbers into ranges. The area +of the bar is the total frequency of all of the covered values in the range. + +## Input Description + +You'll be given four numbers on the first line telling you the start and end of +the horizontal (X) axis and the vertical (Y) axis, respectively. The next line +tells you the interval for the X-axis to use (the width of the bar). Then +you'll have a number on a single line telling you how many records to read. +Then you'll be given the data as 2 numbers: the first is the variable, the +second number is the frequency of that variable. Example: + + 1 4 1 10 + 2 + 4 + 1 3 + 2 3 + 3 2 + 4 6 + +## Challenge Output + +Your program should emit an ASCII histogram plotting the data according to the +specification - the size of the chart and the frequency of the X-axis, +variables. Example: + + 10 + 9 + 8 + 7 + 6 + 5 + 4 *** + 3*** *** + 2*** *** + 1*** *** + 1 2 3 4 + +# Challenge Input + + 0 40 0 100 + 8 + 40 + 1 56 + 2 40 + 3 4 + 4 67 + 5 34 + 6 48 + 7 7 + 8 45 + 9 50 + 10 54 + 11 20 + 12 24 + 13 44 + 14 44 + 15 49 + 16 28 + 17 94 + 18 37 + 19 46 + 20 64 + 21 100 + 22 43 + 23 23 + 24 100 + 25 15 + 26 81 + 27 19 + 28 92 + 29 9 + 30 21 + 31 88 + 32 31 + 33 55 + 34 87 + 35 63 + 36 88 + 37 76 + 38 41 + 39 100 + 40 6 diff --git a/daily/302hard/bargroup.pas b/daily/302hard/bargroup.pas new file mode 100644 index 0000000..295c458 --- /dev/null +++ b/daily/302hard/bargroup.pas @@ -0,0 +1,104 @@ +uses math, sysutils; + +var + minx, maxx, miny, maxy, stepx, stepy, i, x0, f: integer; + n, j, lenf: byte; + raw_freq, freq: array of integer; + xtrue, xfalse: array of string; + + +function gcd(a, b: integer): integer; + var + c: integer; + + begin + while b > 0 do + begin + c := b; + b := a mod b; + a := c + end; + gcd := a + end; + + +function rjust( + s: string; + width: byte; + fill: char +): string; + begin + while length(s) < width do + s := fill + s; + rjust := s + end; + + +begin + read(minx, maxx, miny, maxy, stepx, n); + setlength(raw_freq, maxx - minx + 1); + for i := 0 to length(raw_freq) - 1 do + raw_freq[i] := 0; + for i := 1 to n do + begin + readln(x0, f); + raw_freq[x0 - minx] := f + end; + + setlength(freq, length(raw_freq) div stepx); + for i := 0 to length(freq) - 1 do + begin + freq[i] := 0; + for j := 0 to stepx - 1 do + inc(freq[i], raw_freq[i * stepx + j]); + freq[i] := freq[i] div stepx + end; + + stepy := maxy - miny + 1; + for i in freq do + stepy := gcd(stepy, i - miny + 1); + + lenf := 0; + i := miny; + repeat + lenf := max(length(inttostr(i)), lenf); + inc(i, stepy) + until i > maxy; + + setlength(xfalse, length(freq)); + setlength(xtrue, length(freq)); + for i := 0 to length(freq) - 1 do + begin + xtrue[i] := rjust('', length(inttostr(minx + stepx * i)), '*'); + xfalse[i] := rjust('', length(inttostr(minx + stepx * i)), ' '); + for j := 1 to stepx - 1 do + begin + xtrue[i] := xtrue[i] + + rjust('', length(inttostr(minx + stepx*i + j)) + 1, '*'); + xfalse[i] := xfalse[i] + + rjust('', length(inttostr(minx + stepx*i + j)) + 1, ' ') + end + end; + + repeat + write(rjust(inttostr(maxy), lenf, ' ')); + for i := 0 to length(freq) - 2 do + begin + if freq[i] >= maxy then + write(xtrue[i]) + else + write(xfalse[i]); + write(' ') + end; + if freq[length(freq) - 1] >= maxy then + writeln(xtrue[length(freq) - 1]) + else + writeln(xfalse[length(freq) - 1]); + dec(maxy, stepy) + until maxy < miny; + + write(rjust('', lenf, ' ')); + for i := minx to maxx - 1 do + write(i, ' '); + writeln(maxx) +end. diff --git a/daily/302hard/input b/daily/302hard/input new file mode 100644 index 0000000..75dd6f8 --- /dev/null +++ b/daily/302hard/input @@ -0,0 +1,43 @@ +1 40 0 100 +8 +40 +1 56 +2 40 +3 4 +4 67 +5 34 +6 48 +7 7 +8 45 +9 50 +10 54 +11 20 +12 24 +13 44 +14 44 +15 49 +16 28 +17 94 +18 37 +19 46 +20 64 +21 100 +22 43 +23 23 +24 100 +25 15 +26 81 +27 19 +28 92 +29 9 +30 21 +31 88 +32 31 +33 55 +34 87 +35 63 +36 88 +37 76 +38 41 +39 100 +40 6 diff --git a/daily/302hard/output b/daily/302hard/output new file mode 100644 index 0000000..e0ccaf4 --- /dev/null +++ b/daily/302hard/output @@ -0,0 +1,102 @@ +100 + 99 + 98 + 97 + 96 + 95 + 94 + 93 + 92 + 91 + 90 + 89 + 88 + 87 + 86 + 85 + 84 + 83 + 82 + 81 + 80 + 79 + 78 + 77 + 76 + 75 + 74 + 73 + 72 + 71 + 70 + 69 + 68 + 67 + 66 + 65 + 64 *********************** + 63 *********************** *********************** + 62 *********************** *********************** + 61 *********************** *********************** + 60 *********************** *********************** + 59 *********************** *********************** + 58 *********************** *********************** + 57 *********************** *********************** + 56 *********************** *********************** + 55 *********************** *********************** + 54 *********************** *********************** + 53 *********************** *********************** + 52 *********************** *********************** + 51 *********************** *********************** + 50 *********************** *********************** + 49 *********************** *********************** + 48 *********************** *********************** + 47 *********************** *********************** + 46 *********************** *********************** + 45 *********************** *********************** + 44 *********************** *********************** *********************** + 43 *********************** *********************** *********************** + 42 *********************** *********************** *********************** + 41 *********************** *********************** *********************** + 40 *********************** *********************** *********************** + 39 ********************** *********************** *********************** *********************** + 38 ********************** *********************** *********************** *********************** + 37*************** ********************** *********************** *********************** *********************** + 36*************** ********************** *********************** *********************** *********************** + 35*************** ********************** *********************** *********************** *********************** + 34*************** ********************** *********************** *********************** *********************** + 33*************** ********************** *********************** *********************** *********************** + 32*************** ********************** *********************** *********************** *********************** + 31*************** ********************** *********************** *********************** *********************** + 30*************** ********************** *********************** *********************** *********************** + 29*************** ********************** *********************** *********************** *********************** + 28*************** ********************** *********************** *********************** *********************** + 27*************** ********************** *********************** *********************** *********************** + 26*************** ********************** *********************** *********************** *********************** + 25*************** ********************** *********************** *********************** *********************** + 24*************** ********************** *********************** *********************** *********************** + 23*************** ********************** *********************** *********************** *********************** + 22*************** ********************** *********************** *********************** *********************** + 21*************** ********************** *********************** *********************** *********************** + 20*************** ********************** *********************** *********************** *********************** + 19*************** ********************** *********************** *********************** *********************** + 18*************** ********************** *********************** *********************** *********************** + 17*************** ********************** *********************** *********************** *********************** + 16*************** ********************** *********************** *********************** *********************** + 15*************** ********************** *********************** *********************** *********************** + 14*************** ********************** *********************** *********************** *********************** + 13*************** ********************** *********************** *********************** *********************** + 12*************** ********************** *********************** *********************** *********************** + 11*************** ********************** *********************** *********************** *********************** + 10*************** ********************** *********************** *********************** *********************** + 9*************** ********************** *********************** *********************** *********************** + 8*************** ********************** *********************** *********************** *********************** + 7*************** ********************** *********************** *********************** *********************** + 6*************** ********************** *********************** *********************** *********************** + 5*************** ********************** *********************** *********************** *********************** + 4*************** ********************** *********************** *********************** *********************** + 3*************** ********************** *********************** *********************** *********************** + 2*************** ********************** *********************** *********************** *********************** + 1*************** ********************** *********************** *********************** *********************** + 0*************** ********************** *********************** *********************** *********************** + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 -- cgit 1.4.1