6
\$\begingroup\$

Are you feeling jealousy towards Mathematica, Matlab et al. for their intricate plotting capabilities? If you answered yes, this challenge is for you!

Challenge

Given, via parameters or standard input, a list of numbers list and a positive integer h for the height of the plot, write a program/function/snippet that prints a character array of length(list) columns and h rows, where each column contains exactly one special non-whitespace character, e.g. # and h-1 spaces. For the nth column the position of the special character corresponds to the value of the nth element in the list. Lower rows correspond to lower values, so if list[a]≥list[b], then in column a the special character must be at least as high than in column b.

The scaling from the value to the y-position of the special character must essentially be an affine mapping (x ↦ ax+b) plus any necessary rounding so for example integer division is allowed but clipping is not. The scaling must be done so that the full extent of the plot is used, i.e. the first and last row contain at least one special character.

The numbers can be of any numeral type that is at least 8 bits wide. Labels or axes aren't needed. Built-in plotting functions are allowed if the output conforms to the required format. The standard loopholes are disallowed.

Example

Input:

list = [0.0,0.325,0.614,0.837,0.969,0.997,0.916,0.736,0.476,0.165,-0.165,-0.476,-0.736,-0.916,-0.997,-0.969,-0.837,-0.614,-0.325,0.0]
h = 8

One possible output:

    ###             
  ##   #            
 #      #           
#        #         #
          #         
           #      # 
            #   ##  
             ###    

This is , so the shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ In your example, the first and last number is 0.0 but the special character is not on the same row \$\endgroup\$ – Karl Napf Nov 11 '16 at 12:01
  • \$\begingroup\$ @KarlNapf good catch, fixed \$\endgroup\$ – Angs Nov 11 '16 at 12:07
  • \$\begingroup\$ Is this output format acceptable? \$\endgroup\$ – Luis Mendo Nov 11 '16 at 12:11
  • \$\begingroup\$ @LuisMendo no, the output should 20 characters wide for that input and the height should be selectable, but I think it's worth putting in a non-competitive answer. \$\endgroup\$ – Angs Nov 11 '16 at 12:17
  • 2
    \$\begingroup\$ @Angs Thanks. I In fact it doesn't conform to the spec. It would be invalid rather than non-competitive \$\endgroup\$ – Luis Mendo Nov 11 '16 at 12:23
1
\$\begingroup\$

MATL, 26 bytes

tX<-tX>/iq*Yo"@Z"35h]Xhc!P

Try it online!

Explanation
tX<-    % Input array of values implicitly. Subtract minimum
tX>/    % Divide by maximum
iq      % Input height and subtract 1
*       % Multiply
Yo      % Round to nearest integer
"       % For each number in that array
  @     %   Push current number
  Z"    %   String with that number of spaces
  35h   %   Append a '#' character to that string
]       % End
Xh      % Collect all strings into a cell array
c       % Convert into 2D char array, right-padding with spaces
!P      % Transpose and flip vertically. Implicitly display
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 91 bytes

StringRiffle[(Join[" "~Table~#,{"#"},Table[" ",a-#]]&/@⌊(a=#2-1)Rescale@#⌋),"
",""]&

Anonymous function. The Unicode characters are respectively U+230A LEFT FLOOR for \[LeftFloor], U+230B RIGHT FLOOR for \[RightFloor], and U+F3C7 (private use) for \[Transpose]. Output for the test case:

     #              
   ## ##            
  #     #           
 #       #          
#                  #
          #       # 
           #     #  
            #####   
\$\endgroup\$
2
\$\begingroup\$

Python 2, 118 bytes

Saving 2 bytes thanks to Shebang

def f(n,L):
 for i in range(n):print''.join(' #'[i==j]for j in[n-1-int(~-n*(x-min(L))/(max(L)-min(L))+0.5)for x in L])
\$\endgroup\$
  • 1
    \$\begingroup\$ (n-1) -> ~-n saves two bytes by removing parenthesis :) \$\endgroup\$ – Kade Nov 11 '16 at 12:39
  • 1
    \$\begingroup\$ Switching to use n,L=input() taking the arguments as a tuple, dropping the 0 from 0.5, and saving min(L) in a variable saves 3 bytes more :) \$\endgroup\$ – Kade Nov 11 '16 at 12:49
1
\$\begingroup\$

JavaScript (ES6), 125 bytes

list=[0.0,0.325,0.614,0.837,0.969,0.997,0.916,0.736,0.476,0.165,-0.165,-0.476,-0.736,-0.916,-0.997,-0.969,-0.837,-0.614,-0.325,0.0];
h=8;
f=
(a,n,m=Math,u=m.max(...a),h=u-m.min(...a))=>[...Array(n--)].map((_,i)=>a.map(v=>m.round(i-(u-v)*n/h)?` `:`#`).join``).join`
`
;
console.log(f(list,h));

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.