10
\$\begingroup\$

Inspired by Display a chain of little mountains with an odd number on the top of it! by @sygmei.

Why have a chain of mountains when you can have one massive one?

Make a program that takes an input number and prints a mountain with every odd number up to the input number.

(Where "south west" means directly below and to the left, and "south east" means directly below and to the right)

Every number will have a / south west of it, and a \ south east. It starts from 1 at the top, and the next number will go south west of a / or south east of a \. The next number will go in the line closest to the top and to the left most possible.

For a multidigit number, just the 1st digit needs to be in the right place with the other digits directly after, and only the first digit should have \ and / coming out from it.

The mountain up to 1 or 2 is just:

 1
/ \

A mountain up to 3 or 4 is just:

   1
  / \
 3
/ \

For 25 or 26:

         1
        / \
       3   5
      / \ / \
     7   9   11
    / \ / \ / \
   13  15  17  19
  / \ / \ / \ / \
 21  23  25
/ \ / \ / \

Last two lines where the input is 121:

 111 113 115 117 119 121
/ \ / \ / \ / \ / \ / \

And the last two lines where the input is 1019:

 993 995 997 999 1001100310051007100910111013101510171019
/ \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \

You may assume that the input will be greater than 0 and less than 10001 (exclusive).

Trailing spaces are OK, and extra leading spaces are alright as long as there is the same on all lines.

This is , so the shortest program in bytes wins.

An ungolfed answer to this can be found online here (In Python on repl.it) if you need more test cases.

\$\endgroup\$
  • \$\begingroup\$ If the input is less than 101, then why do you have a 121 testcase? \$\endgroup\$ – LegionMammal978 Nov 23 '16 at 20:05
  • \$\begingroup\$ @LegionMammal978 I meant 10001, fixed now. Thank you! \$\endgroup\$ – Artyer Nov 23 '16 at 20:12
  • 1
    \$\begingroup\$ Harder than the original one, I'll give it a try :D \$\endgroup\$ – Sygmei Nov 24 '16 at 10:58
5
\$\begingroup\$

05AB1E, 54 52 47 46 bytes

ÅɹL£D€g__ÏRv"/ \ "yg×N·©úˆyvy4yg-ð×}J®>úˆ}¯R»

Try it online!

Explanation

                                                # implicit input n
ÅÉ                                              # push a list of uneven numbers up to input
  ¹L£                                           # divide into sublists of length 1,2,3...n
     D€g__Ï                                     # remove empty sublists
           R                                    # reverse list
            v                                   # for each sublist
             "/ \ "                             # push the string "/ \ "
                   yg×                          # repeat it len(sublist) times
                      N·©ú                      # prepend (sublist index)*2 spaces
                          ˆ                     # add to global list
                           yv                   # for each number in sublist
                             y4yg-ð×            # prepend spaces until length is 4
                                    }           # end inner loop
                                     J          # join to string
                                      ®>ú       # prepend (sublist index)*2+1 spaces
                                         ˆ      # add to global list
                                          }     # end outer loop
                                           ¯    # push global list
                                            R   # reverse it
                                             »  # join rows by space and columns by newline
                                                # implicitly print

I could have saved 5 bytes with ÅɹL£D€g__ÏRv"/ \ "yg×N·©úˆy4jJðÛ®>úˆ}¯R» if only I'd pushed that bugfix for j I wrote several weeks ago :/

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

Batch, 335 bytes

@echo off
set i=
set/ac=w=0
:l
set i=  %i%
set/aw+=2,c+=w
if %c% lss %1 goto l
set s=
set t=
set/ac=w=1
for /l %%a in (1,2,%1) do call:c %%a
echo  %s%
echo%t%
exit/b
:c
if not %c%==0 goto g
echo%i%%s%
set i=%i:~2%
echo%i%%t%
set s=
set t=
set/aw+=1,c=w
:g
set n=%1   (three trailing spaces)
set s=%s%%n:~0,4%
set t=%t% / \
set/ac-=1
\$\endgroup\$
  • \$\begingroup\$ @Artyer I'm pretty sure cmd /Q /C file.bat counts 3 extra bytes, but it's still better than @echo off. You should post that to the Batch tips question. \$\endgroup\$ – Neil Nov 24 '16 at 17:05
3
\$\begingroup\$

Python 2, 160 149 143 bytes

Thanks TFeld for saving 11 bytes and Artyer for saving 6 bytes

x=range(1,input()+1,2);y=[];i=1
while x:y+=[''.join('%-4d'%j for j in x[:i]),'/ \ '*len(x[:i])];x=x[i:];i+=1
while y:print' '*~-len(y)+y.pop(0)
\$\endgroup\$
  • \$\begingroup\$ You can do i=len(y) and for l in y:print' '*i+l;i-=1 instead of for i,l in enumerate(y):print(len(y)-i-1)*' '+l for -10 bytes \$\endgroup\$ – TFeld Nov 24 '16 at 15:38
  • \$\begingroup\$ Also I think you can change \\ to \ since it is before a space \$\endgroup\$ – TFeld Nov 24 '16 at 15:39
  • \$\begingroup\$ You can change the last 2 lines to while y:print' '*-~len(y)+y.pop(0). It saves 3 bytes. \$\endgroup\$ – Artyer Nov 24 '16 at 16:54
  • 1
    \$\begingroup\$ @Rod Oops... I meant %-4d to left justify it by 4. \$\endgroup\$ – Artyer Nov 24 '16 at 17:22
  • 1
    \$\begingroup\$ @Artyer woah, this negative value to left justify is new to me :D, nice trick \$\endgroup\$ – Rod Nov 24 '16 at 17:28
2
\$\begingroup\$

Perl, 134

133 bytes + 1 for -p option

 $l="/ \\ ";$t=" ";while($c++<$_) {$t.=sprintf'%-4d',$c++;$v.=$l;if ($i++>=$j){$t=~s/^/  /gm;$t.="
  $v
 ";$i=$v="";$j++}}$_="$t
$v";

Formatted version (original golfed version uses real "new lines" instead of \n)

$l="/ \\ ";
$t=" ";
while($c++<$_) {
   $t.=sprintf'%-4d',$c++;
   $v.=$l;
   if ($i++>=$j){
      $t=~s/^/  /gm;
      $t.="\n  $v\n ";
      $i=$v="";
      $j++
   }
}
$_="$t\n$v";
\$\endgroup\$
2
\$\begingroup\$

Bash, 425 bytes

f() { k=0; o=""; l=1; for (( c=1; c<=$1; c++ )); do o="$o$c "; let c++ k++; if [ $l -eq $k ]; then o=$o"\n"; k=0; let l++; fi; done; s=$[$(echo -e $o | wc -l)*2-1]; p() { for c in $(seq $1); do echo -n " "; done }; IFS=$'\n'; for n in $(echo -e $o); do unset IFS; p $s; let s--; for w in $n; do echo -n "$w"; p $[4-${#w}]; done; echo; p $s; for c in $(seq $(echo $n|wc -w)); do echo -n "/ \ "; done; echo; let s-- j++; done }

Formatted:

l=1
k=0
o=""
for (( c=1; c<=$1; c++ ))
do
        o="$o$c "
        let c++ k++
        if [ $l -eq $k ]
        then
                o=$o"\n"
                k=0
                let l++
        fi
done
s=$[$(echo -e $o | wc -l)*2-1]
p() {
        for c in $(seq $1)
        do
                echo -n " "
        done
}
IFS=$'\n'
for n in $(echo -e $o)
do
        unset IFS
        p $s
        let s--
        for w in $n
        do
                echo -n "$w"
                p $[4-${#w}]
        done
        echo
        p $s
        for c in $(seq $(echo $n|wc -w))
                do echo -n "/ \ "
        done
        echo
        let s-- j++
done
\$\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.