26
\$\begingroup\$

Inspired by this post. For those marking this question as a duplicate I urge you to actually read the question to see that mine is a modification of the one linked. The one linked does not ask for an input and is to just print the alphabet diagonally.

The Challenge

Given an input between 1-26 inclusively, print the alphabet diagonally, but begin printing vertically at the index of the given input.

Examples

Given the input:

16

Your program should output:

a
 b
  c
   d
    e
     f
      g
       h
        i
         j
          k
           l
            m
             n
              o
               p
               q
               r
               s
               t
               u
               v
               w
               x
               y
               z

Input:

4

Output:

a
 b
  c
   d
   e
   f
   g
   h
   i
   j
   k
   l
   m
   n
   o
   p
   q
   r
   s
   t
   v
   w
   x
   y
   z

Input:

1

Output:

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Input:

26

Output:

a
 b
  c
   d
    e
     f
      g
       h
        i
         j
          k
           l
            m
             n
              o
               p
                q
                 r
                  s
                   t
                    u
                     v
                      w
                       x
                        y
                         z

Scoring

This is , so the shortest answer in each language wins.

Good luck!

\$\endgroup\$
  • 6
    \$\begingroup\$ May we choose to use either 0- or 1-indexing? \$\endgroup\$ – notjagan Aug 19 '17 at 3:45
  • \$\begingroup\$ Is a consistent leading space acceptable? \$\endgroup\$ – Giuseppe Aug 19 '17 at 6:26
  • \$\begingroup\$ Are trailing spaces acceptable? \$\endgroup\$ – Dom Hastings Aug 19 '17 at 13:20
  • \$\begingroup\$ May we use uppercase? \$\endgroup\$ – Adám Aug 20 '17 at 22:29
  • \$\begingroup\$ May we return a list of strings? \$\endgroup\$ – Adám Aug 21 '17 at 11:15

45 Answers 45

1
\$\begingroup\$

APL (Dyalog), 12 11 bytes

-1 as OP has now clarified that returning a list of strings is fine.

Prompts for input.

⎕A↑¨⍨-⎕⌊⍳26

Try it online!

⍳26 first 26 strictly positive ɩntegers

⎕⌊ minimum of input and those

- negate those

⎕A↑⍨¨ for each letter of the Alphabet, take that many characters (from the rear, as all numbers are negative), padding with spaces as necessary


 convert list of strings into matrix (only in TIO link to enable readable output)

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 47 39 bytes

$l=<>;say$"x($#i+=$#i<$l-1).$_ for a..z

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 36 bytes

31 bytes code + 5 for -i# -l.

Note: this takes input via -i, if that is unacceptable (since it's non-standard) I can remove.

$\.=$"x(--$^I>0),print for a..z

Try it online!

Explanation

For this I'm abusing the special variable $\ which is automatically printed after each call to print, to store the next line's indentation. Using the flag -l (which enables line ending processing), $\ is pre-initialised to "\n" so we append a space each time we process an element in the range a..z, as long as --$^I isn't less than 0. Using $^I (via the -i commandline flag) means I don't need to store input separately, but it's bit of a stretch as its not the usual way to accept input in Perl. Since the postfix for loop stores the current item in $_, we don't need any arguments to print ($_ is automatically printed and $\ is automatically appended) so we just call it each iteration, appending spaces as we go to get the desired output. This does append trailing spaces though, which @SpookyGengar has allowed.

Thanks to @Xcali for pointing out an oversight!

\$\endgroup\$
  • \$\begingroup\$ Are you off by one here? Based on the examples, it looks like an input of 4 should have d and e vertically aligned. \$\endgroup\$ – Xcali Aug 22 '17 at 22:53
  • \$\begingroup\$ @Xcali Oops! Yeah! Change to pre-decrement instead, thank you! \$\endgroup\$ – Dom Hastings Aug 23 '17 at 4:41
1
\$\begingroup\$

PowerShell, 66 60 bytes

param($a)65..90|%{" "*(($b=$_-65),($a-1))[$b-ge$a]+[char]$_}

Try it online!

Loops through the alphabet, each iteration using string multiplication to prepend the appropriate number of spaces (chosen using a pesudo-ternary (,)[]), then string-concatenates with the current character.

\$\endgroup\$
1
\$\begingroup\$

Pyke, 6 bytes

G\xb1oh->

Try it here!

          - o = 0
G         -  alphabet
 \xb1     - for i in ^: (1 byte, pretty prints)
     oh   -    (o++)+1
       -  -   " "*^ + i
        > -  ^[input:]
\$\endgroup\$
1
\$\begingroup\$

SimpleTemplate, 79 70 bytes

The code outputs the required text and a trailing newline.

{@forfrom"a"to"z"}{@echols,_}{@inca}{@ifa is lowerargv.0}{@sets s," "}

Ungolfed:

{@for letter from "a" to "z"}
    {@echo spaces, letter, EOL} {@// same as echol}
    {@inc by 1 index} {@// sets to 1 if it doesn't exist}
    {@if index is lower than argv.0}
        {@// creates an array like [[[...], " "], " "]}
        {@set spaces spaces, " "}
    {@/} {@//not required}
{@/} {@// not required}

You can try it on http://sandbox.onlinephpfunctions.com/code/a0ee99464f463d23072ff5d5be7dbd3a532f9c7c

(Old version: http://sandbox.onlinephpfunctions.com/code/a60e11b9dd2dcd54a84c11937f1918f26e0adcfa)

\$\endgroup\$
0
\$\begingroup\$

><> (-v), 51+2 Bytes

'a'$1-0\   /' 'o1-81.
'z')?;}>::?/~$:@@:@)+{:oao1+:

Try it online

equivalent pseudo code

char = 'a'
input = read_number
input -= 1
tabCount = 0

while true
    for i from 0 to tabCount
        print ' '
    if tabCount < input
        ++tabCount
    print char
    print '\n'
    ++char
    if char > 'z'
        exit
\$\endgroup\$
0
\$\begingroup\$

Brainbash, 98 bytes

+[-[---<]>>-]<-~++++++++[>++++>+++>+<<<-]>>++>++>#-<<[>>>[-<<<<.>>>>>+<]>[-<+>]<<{->+<}<<-~.+~>.<]

Try it online!

Explanation

The code can be split into two parts:

+[-[---<]>>-]<-~++++++++[>++++>+++>+<<<-]>>++>++>#
-<<[>>>[-<<<<.>>>>>+<]>[-<+>]<<{->+<}<<-~.+~>.<]

Basically, generate the constants 97, 32, and 26. Then, takes a number N as input. After input (#) the tape looks like:

[ ]   0      0     >97     0
[*]   0      32     26     10    >0

The second part executes 26 times, each time decrementing the input. Every time the input is decremented, the prefix length is incremented. After the input reaches zero, the prefix length remain constant. After each loop, the spaces, the letter, and a newline are printed.

\$\endgroup\$
0
\$\begingroup\$

Röda, 48 bytes

f n{seq 1,26|min([n,_])<>chr 96+_1|print" "*_,_}

Try it online!

Each output line has one leading space.

Explanation:

f n{
  seq 1,26|    /* push numbers 1..26 to the stream (inclusive) */
               /* for each number in the stream: */
  min([n,_])<> /*  push min(n, _1) */
  chr 96+_1|   /*  push string containing _1 as a letter */
               /* for each number _1 and letter _2 in the stream: */
  print" "*_,_ /*  print _1 spaces and _2 */
}
\$\endgroup\$
0
\$\begingroup\$

Bash, 50 bytes

for c in {A..Z};{ printf "%$[++i<$1?i:$1]s\n" $c;}

Try it online!

Takes input as a command-line argument.

\$\endgroup\$
0
\$\begingroup\$

Elixir, 145 bytes

{n,_}=IO.gets("")|>Integer.parse;for x<-?a..?z,do: if x-97<n,do: IO.puts String.pad_leading(<<x>>,x-96),else: IO.puts String.pad_leading(<<x>>,n)

Try online

\$\endgroup\$
0
\$\begingroup\$

PHP, 67 bytes

<?for($l=a;$l!=aa;)echo str_pad($l++,min($argv[1],++$x)," ",0)."
";

Try it online!

\$\endgroup\$
0
\$\begingroup\$

VBA Excel, 51 bytes

using Immediate Window and [a1] as input

for x=1to 26:?spc(iif(x<[a1],x,[a1]))chr(96+x):next
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 53 50 Bytes

Anonymous VBE immediate window function that takes input of expected type Integer in the domain of [1,26] from range [A1] and outputs to the VBE immediate window

For i=0To 25:[B1]=i:?Spc([Min(1:1)])Chr(97+i):Next
\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 107 bytes

	N =INPUT
I	&LCASE POS(R) LEN(1) . L
	OUTPUT =DUPL(' ',X) L
	R =R + 1
	X =LT(R,N) X + 1
	LT(R,26)	:S(I)
END

Try it online!

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