19
\$\begingroup\$

Take an array of nonnegative integers, such as [1, 0, 0, 1, 2, 4, 2, 0]. Then, draw that as a mountain where the integers represent altitude:

     x  
        
    x x 
x  x    
 xx    x

You may use any printable non-whitespace character in place of x, and adding padding or whitespace after lines or the output is fine (but not required). You may optionally take one indexed inputs. Transposing the output is not allowed, but returning an array of lines is fine.

This is , so shortest answer (in bytes) per language wins.

\$\endgroup\$
14
  • \$\begingroup\$ May I print the answer with extra columns of spaces between each column? \$\endgroup\$
    – FZs
    Jun 26 at 9:40
  • 2
    \$\begingroup\$ Can we output a list of lines? \$\endgroup\$ Jun 26 at 9:59
  • \$\begingroup\$ May we take the length of the array as an additional input? \$\endgroup\$
    – chunes
    Jun 26 at 10:12
  • \$\begingroup\$ Can the output be transposed? (Rows instead of columns) \$\endgroup\$
    – Luis Mendo
    Jun 26 at 11:49
  • 1
    \$\begingroup\$ @JonathanAllan I don't think I mentioned anything either way on that in chat, but I think it'd fall under standard I/O in one way or another. It's very much an edge case though, so I think putting it in the challenge body is unnecessary. I did edit earlier to include the list of lines thing, though. Thanks! \$\endgroup\$ Jun 27 at 21:50

21 Answers 21

17
+100
\$\begingroup\$

Vyxal, 4 bytes

×꘍R§

Try it Online!

That's right, it's flagless.

Explained

×꘍    # [(n * " ") + "*" for n in input]
  R   # [x[::-1] for x in ^]
   §  # vertically join (rotate) ^

Using the L flag would make it 3 bytes.

\$\endgroup\$
11
\$\begingroup\$

Python 2, 66 bytes

a=input()
n=max(a)
while~n:print''.join(' x'[x==n]for x in a);n-=1

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ I'm new to golfing, could you please explain what the ~ in the while~n: stands for ? \$\endgroup\$
    – Ubikuity
    Jun 27 at 17:12
  • \$\begingroup\$ @Ubikuity It is the bitwise NOT operator. See docs.python.org/3/reference/expressions.html#index-96 \$\endgroup\$ Jun 27 at 17:42
  • 1
    \$\begingroup\$ See golfing tips for Python. ~ is the logical operator not. So while~n:<<…>>;n-=1 is a sort of for loop for n decreasing until it’s 0 (not n is True if n not equal to 0; for n=0, it’s False). \$\endgroup\$
    – agtoever
    Jun 27 at 17:45
  • 1
    \$\begingroup\$ @agtoever n actually decreases until it reaches -1. ideone.com/GEM9L1 \$\endgroup\$ Jun 29 at 13:53
  • \$\begingroup\$ @Unmitigated you’re completely right. \$\endgroup\$
    – agtoever
    Jun 29 at 13:55
9
\$\begingroup\$

05AB1E (legacy), 5 bytes

$úζR»

Try it online!

$        # push "1" and the input
 ú       # for each integer in the input,
         # pad "1" with this many spaces in the front
  ζ      # tranpose, padding with spaces
   R     # reverse, the mountains should not be upside down
    »    # join by newlines
\$\endgroup\$
1
  • 1
    \$\begingroup\$ » can be removed. \$\endgroup\$
    – Makonede
    Jun 30 at 2:49
7
\$\begingroup\$

Zsh, 66 63 bytes

s=${(l/${${(On)@}[1]}*#/)}
for x;s[++i+$#s-#*x]=.
fold -$#<<<$s

Attempt This Online!

-3 thanks to JoKing, but still probably too long.

Explanation:

  • ${${(On)@}[1]}: get the maximum input
  • s=${(l/*#/)}: construct a string of spaces which is (number of inputs) × (maximum of inputs) in length
  • for x;s[++i+$#s-#*x]=.: set the correct index of the string to a . for each element
  • fold -$#<<<$s: line-wrap the string at (number of inputs) in length
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Only familiar with bash, but would something like sort -n|tail -1 save bytes for getting the max? \$\endgroup\$
    – Jonah
    Jun 26 at 15:05
  • \$\begingroup\$ @Jonah That method doesn't turn out to be shorter, but Zsh has its own builtin way to sort things which I should have considered, so thanks for the hint anyway! \$\endgroup\$
    – pxeger
    Jun 26 at 17:03
6
\$\begingroup\$

APL(Dyalog Unicode), 18 bytes SBCS

{⊖⍉⍕⍪'x',¨⍨⍵⍴¨' '}
{          ⍝ Open function
⊖⍉         ⍝ Flip diagonally and then vertically
⍕          ⍝ Make table into multiline string
⍪           ⍝ Display one element per row
'x',¨⍨      ⍝ Append an 'x' to each element
⍵⍴¨' '      ⍝ Duplicate each space 'input' times
}           ⍝ Close function

Try it on online!

\$\endgroup\$
8
  • 2
    \$\begingroup\$ ⊖⍉↑'x'↑¨⍨-1+⎕ 13 \$\endgroup\$
    – rak1507
    Jun 26 at 9:17
  • \$\begingroup\$ @rak1507 Thanks for the idea! Not sure I understand, though. It doesn't seem to work. Will play around with it more later! \$\endgroup\$
    – AviFS
    Jun 26 at 10:17
  • \$\begingroup\$ the 13 is the byte count \$\endgroup\$
    – rak1507
    Jun 26 at 11:00
  • \$\begingroup\$ rak's comment is a full program, here is a TIO link for it. I'm not sure how to use it on APLgolf \$\endgroup\$
    – ovs
    Jun 26 at 11:44
  • 1
    \$\begingroup\$ Sounds good, thanks a bunch @rak1507. I'm sure ovs is, yours was just a significant change so I wanted you to get the rep & credit if you wanted! \$\endgroup\$
    – AviFS
    Jun 27 at 20:02
6
\$\begingroup\$

Red, 91 bytes

func[a][i: last sort copy a until[forall a[prin either a/1 = i[1][sp]]print""0 > i: i - 1]]

Try it online!

-6 bytes from Galen Ivanov.

\$\endgroup\$
1
  • \$\begingroup\$ 91 bytes with some minor modifications. \$\endgroup\$ Jun 26 at 13:33
6
\$\begingroup\$

Jelly, 7 bytes

Ṭ€z0Ṛo⁶

Try it online!

Outputs a list of lines. +1 byte (append Y) if unacceptable.

Takes input 1 indexed, uses 1 as the mountain character

How it works

Ṭ€z0Ṛo⁶ - Main link. Takes a list L on the left
 €      - Over each element in L:
Ṭ       -   Untruthy; Map each n to [0, 0, ..., 1] with n-1 zeros
  z0    - Transpose, padding with zeros
    Ṛ   - Reverse
     o⁶ - Replace 0s with spaces
\$\endgroup\$
4
  • \$\begingroup\$ Strictly speaking, even if a list of lines is acceptable you would still need to be allowed to use an integer in place of a character to remove the Y ("You may use any printable non-whitespace character in place of x"). \$\endgroup\$ Jun 26 at 15:51
  • \$\begingroup\$ @JonathanAllan It appears as though Y doesn't actually coerce integers into strings: Try it online!. However, OP confirmed in chat that both a list of lines and mixing characters/digits was fine \$\endgroup\$ Jun 27 at 9:02
  • \$\begingroup\$ No it does not, but with it the code would work as a full program due to Jelly's implicit, smashing print. \$\endgroup\$ Jun 27 at 10:05
  • \$\begingroup\$ The post has not been updated yet to say non-charactrers are acceptable. It's best to stick to the current wording and give the potential save than the other way around. \$\endgroup\$ Jun 27 at 10:09
6
\$\begingroup\$

J, 17 bytes

[:|.@|:' x'#~,.&1

Try it online!

+6 thanks to Lynn for spotting a bug in the original solution: ' x'{~=\:~. This approach can be made to work but it's no longer golfy: ' x'{~]=/~[:i.1-@+>./

how

  • ,.&1 Zip input with 1:

    1 1
    0 1
    0 1
    1 1
    2 1
    4 1
    2 1
    0 1
    
  • ' x'#~ Duplicate ' x' according to these pairs

     x   
    x    
    x    
     x   
      x  
        x
      x  
    x
    
  • [:|.@|: Rotate left:

         x  
    
        x x 
    x  x    
     xx    x
    
\$\endgroup\$
2
  • \$\begingroup\$ Good spot, thanks. Fixed solution not as nice though :( \$\endgroup\$
    – Jonah
    Jun 26 at 22:54
  • \$\begingroup\$ @Lynn Original approach can be salvaged ' x'{~]=/~[:i.1-@+>./ but it's no longer shorter. \$\endgroup\$
    – Jonah
    Jun 26 at 23:15
6
\$\begingroup\$

Jelly, 6 bytes

=ⱮṀṚo⁶

A monadic Link that accepts a list of positive integers (the 1-indexed option) and yields a list of lists of characters and integers (1 being the choice for x).

Try it online! (The footer joins with newlines and then Jelly's implicit, smashing print produces the output.)

How?

=ⱮṀṚo⁶ - Link: list of integer heights, H
  Ṁ    - maximum (H) -> M
 Ɱ     - map across (h in [1..M]) with:
=      -   (H) equals (h) (vectorises)
   Ṛ   - reverse -> X
     ⁶ - space character
    o  - (X) logical OR (' ') (vectorises)
\$\endgroup\$
4
\$\begingroup\$

Charcoal, 6 bytes

↑EA◧xι

Try it online! Link is to verbose version of code. 1-indexed. Explanation:

  A     Input array
 E      Map over elements
    x   Literal string `x`
   ◧    Left-padded to width
     ι  Current element
↑       Print rotated
\$\endgroup\$
4
\$\begingroup\$

Japt -R, 7 bytes

1-indexed, using " for the peaks.

£QùXÃÕÔ

Try it

£QùXÃÕÔ     :Implicit input of array
£           :Map each X
 Q          :  Quotation mark
  ùX        :  Left pad with spaces to length X
    Ã       :End map
     Õ      :Transpose
      Ô     :Reverse
            :Implicit output joined with newlines
\$\endgroup\$
4
\$\begingroup\$

Ruby, 51 bytes

->a{(-a.max..0).map{|y|a.map{|x|x==-y ??x:' '}*''}}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 11 9 bytes

-2 bytes through auto-vectorizing

ð*×+ðÞṪṘ⁋
ð*            # vectorised n spaces
  ×+          # append "*"
     ðÞṪ      # transpose with space as filler
        Ṙ⁋    # reverse and join by newline

Try it Online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 77 74 73 70 bytes

-1 thanks to @RecursiveCo.
-3 thanks to @m90's idea

a=>{for(i=Math.max(...a);i;i--)console.log(a.map(e=>i^e&&' ').join``)}

Try it online!

Takes one-indexed input.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ -1 bytes for your first program: see this link \$\endgroup\$
    – user100690
    Jun 26 at 11:46
  • \$\begingroup\$ @RecursiveCo. Good idea, applies to both of my programs. Edited in. (+ thanks for tidying up my header & footer) \$\endgroup\$
    – FZs
    Jun 26 at 12:53
  • 1
    \$\begingroup\$ A further improvement is to replace 'x' with 1 (which gets automatically stringified by join), since any printable non-whitespace character is accepted. \$\endgroup\$
    – m90
    Jun 26 at 22:55
  • \$\begingroup\$ @m90 Good point! Thinking further, I can even get rid of the literal 1 by using the 0 returned by the XOR. Thanks for the idea, merged. \$\endgroup\$
    – FZs
    Jun 27 at 5:59
  • \$\begingroup\$ Switch to the V8 engine on TIO and you can use print. \$\endgroup\$
    – Shaggy
    Jun 27 at 20:05
3
\$\begingroup\$

R, 65 bytes

function(a)for(i in max(a):0)cat(c(' ','x')[1+(a==i)],'
',sep='')

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 3, 67 bytes

lambda x:[[v==i and'*'or' 'for v in x]for i in range(max(x),-1,-1)]

Try it online!

for i in range(max(x),-1,-1) -> identify how high the mountain will be, max value is first line (top), so start from max, iterate to 0

[v==i and'*' or' ' for v in x] -> for every integer in input set it as * if height matches current line, set it whitespace otherwise.

[EDIT] as per @hyper-neutrino suggestion to get 3 bytes less, and wrapped in lambda as per @Razetime comment

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to Code Golf! You can remove three spaces here: after '*', after ' ', and after the first ]. You may also be interested in our Tips for golfing in Python. \$\endgroup\$
    – hyper-neutrino
    Jun 29 at 13:37
  • \$\begingroup\$ hardcoding is not allowed as per standard i/o, so you need to wrap your code in a function. \$\endgroup\$
    – Razetime
    Jun 29 at 14:17
  • \$\begingroup\$ @Razetime could be like it's now, wrapped in lambda, so input is lambda arg? \$\endgroup\$
    – Grysik
    Jun 30 at 7:22
  • \$\begingroup\$ Yep, correct. You may want to read this meta post if you have any doubts. \$\endgroup\$
    – Razetime
    Jun 30 at 8:04
  • \$\begingroup\$ lambda a:[[' x'[i==j]for j in a]for i in range(max(a),-1,-1)] \$\endgroup\$
    – Jitse
    Jun 30 at 8:22
2
\$\begingroup\$

Wolfram Language (Mathematica), 44 bytes

Print@@@Table[" "Unitize[#+i],{i,-Max@#,0}]&

Try it online!

Prints the mountain, using 0 instead of x.

Remove the Print@@@ for -8 bytes if an array of characters and numbers is acceptable output.

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

Octave / MATLAB, 43 bytes

@(x)flip([accumarray([x find(x)],3)+32 ''])

Anonymous function that inputs a 1-based column vector and outputs a char matrix with # and

Try it online!

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

Pip -l, 10 bytes

R:sXg.0ZDs

Takes the integers as command-line arguments. Outputs using 0 in place of x. Try it here! Or, here's an 11-byte equivalent in Pip Classic: Try it online!

Explanation

            g is list of cmdline args; s is space (implicit)
  sXg       Convert each arg to a string of that many spaces
     .0     Append 0 to each string of spaces
       ZDs  Transpose, extending shorter strings with a default value of space
R:          Reverse the resulting list
            Autoprint, one row per line (-l flag)
\$\endgroup\$
2
\$\begingroup\$

Canvas, 7 bytes

{ ×x+]↶

Try it here!

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

Pyth, 7 bytes

_.tm+*;

Try it here!

\$\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.