15
\$\begingroup\$

Given an input number n from 1 to 26 (or 0 to 25), output the alphabet reading left-to-right up to and including that corresponding letter, with a=1, b=2, c=3, .... The twist is the letters must also be repeated vertically corresponding to their position in the alphabet. Odd numbers (when 1-indexed) should be balanced across the horizontal line, while even numbers should alternate between favoring the top or bottom (you can choose which direction to go first). If you're 0-indexing, then swap odd/even in the previous sentence.

Worded another way -- if the alphabetical value of a letter ? is #, then there should be # copies of that letter in the output, all of them in the #th column. These letters should be evenly balanced above and below the horizontal line that has the a. If the letters cannot be balanced evenly, then alternate having the "extra" letter above and below that line.

Here are the first six outputs (n = 1,2,3,4,5,6, 1-indexed, choosing to alternate to the bottom first), separated by newlines, so you can see the pattern. Comments explaining the pattern start with #.

a # On a line by itself
ab
 b # The "extra" letter is below the horizontal
 c
abc # The 'c' splits evenly
 bc
 d # Because the 'b' was below, the extra 'd' must be above
 cd
abcd
 bcd
 de
 cde
abcde # The 'e' balances
 bcde
 e
 def
 cdef
abcdef
 bcdef
 ef
 f # Since the 'd' was above, the extra 'f' must be below

(skip a few to n=26)

 xyz
 wxyz
 tuvwxyz
 stuvwxyz
 pqrstuvwxyz
 opqrstuvwxyz
 lmnopqrstuvwxyz
 klmnopqrstuvwxyz
 hijklmnopqrstuvwxyz
 ghijklmnopqrstuvwxyz
 defghijklmnopqrstuvwxyz
 cdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
 bcdefghijklmnopqrstuvwxyz
 efghijklmnopqrstuvwxyz
 fghijklmnopqrstuvwxyz
 ijklmnopqrstuvwxyz
 jklmnopqrstuvwxyz
 mnopqrstuvwxyz
 nopqrstuvwxyz
 qrstuvwxyz
 rstuvwxyz
 uvwxyz
 vwxyz
 yz
 z

Rules

  • You can choose to output in uppercase or lowercase, but it must be consistent.
  • The output cannot have extraneous whitespace, except for an optional trailing newline.
  • Either a full program or a function are acceptable.
  • The input number can be taken via any suitable format.
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
asked Aug 9, 2016 at 17:06
\$\endgroup\$

5 Answers 5

8
\$\begingroup\$

Python 2, (削除) 101 (削除ここまで) 99 bytes

r=range(input())
for x in sorted(r,key=lambda x:x*-(x&2)):print bytearray([97+i,32][i<x]for i in r)

xsot saved two bytes by realizing x*-(x&2) suffices as a sorting key — the bottom half of the resulting image is unaffected due to sorted guaranteeing a stable sort.

answered Aug 9, 2016 at 17:34
\$\endgroup\$
2
  • \$\begingroup\$ Can you not drop a - to output the lines in reverse order, which I believe is allowable? \$\endgroup\$ Commented Aug 10, 2016 at 0:27
  • \$\begingroup\$ I think x*-(x&2) works. \$\endgroup\$ Commented Aug 10, 2016 at 1:39
6
\$\begingroup\$

Pyth, 26 bytes

A perfect score for a challenge about alphabet.

j_C.e<u_G/k2.[.|1Q*hkbdQ<G

Try it online!

answered Aug 9, 2016 at 18:31
\$\endgroup\$
5
\$\begingroup\$

Jelly, (削除) 20 (削除ここまで) 18 bytes

Ḷ&×ばつḶỤØaḣ8¤ṫW€UGU

Port of my Python answer. Try it online.

EDIT: Dennis saved two bytes by using (grade up) instead of Þ (sort by)!

answered Aug 9, 2016 at 20:20
\$\endgroup\$
0
4
\$\begingroup\$

Jelly, 25 bytes

RØaxŒgz6W€;ṚL‘$¡\/Ṛ8H¤¡j7

Try it online! or verify all test cases.

answered Aug 9, 2016 at 19:01
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), (削除) 127 (削除ここまで) 126 bytes

n=>[...Array(n).keys()].sort((a,b)=>a*~-(a&2)-b*~-(b&2)).map(i=>` `.repeat(i)+`abcdefghijklmnopqrstuvwxyz`.slice(i,n)).join`\n`

Uses @Lynn's sort trick. Writing out the whole alphabet was two bytes cheaper than calculating it. Edit: Saved 1 byte thanks to @ETHproductions because I forgot to note that \n actually represents the literal newline character. (I don't like putting literal newlines in my answer when the line is so long.)

answered Aug 10, 2016 at 0:26
\$\endgroup\$
4
  • \$\begingroup\$ Save two bytes on the alphabet: btoa`...` where ... is replaced with the result of atob`abcdefghijklmnopqrstuvwxyzz` . (Also, you can replace \n with a literal newline.) \$\endgroup\$ Commented Sep 27, 2016 at 3:34
  • \$\begingroup\$ @ETHproductions I take it that would be using ISO encoding, rather than UTF? \$\endgroup\$ Commented Sep 27, 2016 at 18:53
  • \$\begingroup\$ Yes it would. Are we allowed to use ISO-8859-1 rather than UTF-8 in JS? \$\endgroup\$ Commented Sep 27, 2016 at 18:58
  • \$\begingroup\$ @ETHproductions Probably but I can't seem to get it to work, so I won't show it. \$\endgroup\$ Commented Sep 27, 2016 at 19:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.