18
\$\begingroup\$

Given a strictly positive integer, N, produce an output satisfying the following:

  • Produce an array of length N.
  • Every string (i.e. "word") in the array is of length N.
  • Every letter in the word is unique.
  • Every first letter of the words are unique between each other.
  • The remaining items of each word are equal to each other.

Example output

For an input of e.g. 3:

cba
dba
eba

Specification

  • Trailing whitespace is totally allowed.
  • The "letters" don't have to be from the lowercase alphabet, as long as they aren't whitespace.
  • The maximum N you need to support is 13, since there are 26 letters in the lowercase alphabet.
  • The separator of your array can be anything, as long as you will never involve that character for every possible input from 1 to 13. You can also just output a literal array.
  • Letters have to be 1 character long.
  • Letters are case-sensitive, e.g. a and A can appear on the same line.
\$\endgroup\$
  • 4
    \$\begingroup\$ Can the "letters" be numbers? \$\endgroup\$ – Redwolf Programs May 17 at 14:53
  • \$\begingroup\$ Do the "letters" have to be only one character long? \$\endgroup\$ – JDL May 18 at 9:43
  • 5
    \$\begingroup\$ still better than vogon poetry \$\endgroup\$ – Kepotx May 18 at 14:29
  • 1
    \$\begingroup\$ Are upper and lower case letters considered to be different, so? e.g., can a and A appear on the same line? \$\endgroup\$ – Shaggy May 18 at 22:54

20 Answers 20

1
\$\begingroup\$

Japt, 9 8 bytes

;ÆîEhCgX

Try it

;ÆîEhCgX     :Implicit input of integer U
 Æ           :Map each X in the range [0,U)
  î          :  Slice to length U
;  E         :    Printable ASCII
    h        :    Replace first character (space) with
;    C       :      Lowercase alphabet
      gX     :      Character at index X
| improve this answer | |
\$\endgroup\$
7
\$\begingroup\$

Pyth, 9 bytes

V>QG+N<Gt

Try it online!

  • V>QG For each letter in the last Q (the input) elements of the lowercase alphabet:

  • +N>Gt Append that letter to the first Q-1 elements of the lowercase alphabet

For Q=13, the output looks like this:

nabcdefghijkl
oabcdefghijkl
pabcdefghijkl
qabcdefghijkl
rabcdefghijkl
sabcdefghijkl
tabcdefghijkl
uabcdefghijkl
vabcdefghijkl
wabcdefghijkl
xabcdefghijkl
yabcdefghijkl
zabcdefghijkl
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ The meaning behind this poem... so deep. \$\endgroup\$ – ihavenoidea May 19 at 5:24
5
\$\begingroup\$

Charcoal, 11 bytes

NθUOθ⮌β↓…βθ

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

Nθ

Input N.

UOθ⮌β

Print a square of size N filled with the reversed lowercase alphabet.

↓…βθ

Print the first N lowercase letters downwards.

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

C (gcc), 58 bytes

i,j;f(n){for(i=n;i;)putchar(j++?j>n?j=!i--,10:j+63:i+77);}

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Python 2, 53 bytes

lambda n:['%xopqrstuvwxyz'[:n+1]%i for i in range(n)]

Try it online!

Chooses a hex digit (0123456789abc) for the first character, and the last half of the alphabet for the rest.


Another 53-byter that does the same thing, using map:

lambda n:map('%xopqrstuvwxyz'[:n+1].__mod__,range(n))

Try it online!


Another 53-byter, this time using Python 3 f-string:

lambda n:[f'{i:x}copqrstuvwxyz'[:n]for i in range(n)]

Try it online!


If numeric characters are not allowed:

Python 2, 58 57 bytes

-1 byte thanks to @dingledooper !

lambda n:['%copqrstuvwxyz'[:n+1]%(i+65)for i in range(n)]

Try it online!

Choose the first letter from the first half of the alphabet, and the last letters from the last half of the alphabet.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 10 8 bytes

AÂSìδ£I£

Try it online!

Haha! With the expert help of Kevin, I beat Pyth once again!

Explained (with Docs Descriptions)

A| 'abcdefghijklmnopqrstuvwxyz'
Â| Bifurcated a. Push a, reversed(a)
S| Cast a to a list of characters / digits.
ì| Merge b with a if both are lists, else prepend b to a. Push a.prepend(b)
δ| Outer Product. Get the next command and apply it double-vectorized.
£| Head. Push a[0:b]
I| Input
£| Head. Push a[0:b]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Your outputs are 1 character too long. The I should be I<. I do have the feeling 8 should be possible though, so I'll see if I can find something. \$\endgroup\$ – Kevin Cruijssen May 18 at 10:14
  • 1
    \$\begingroup\$ Yay, found an 8-byter after all: AÂSìδ£I£ \$\endgroup\$ – Kevin Cruijssen May 18 at 11:56
3
\$\begingroup\$

Java (JDK), 83 bytes

n->{for(int i=n,j=0;i>0;)System.out.printf("%c",j++>0?j>n?10+(j=--i-i):j+63:i+77);}

Try it online!

I was able to make a one-liner, but it's 100 bytes long and works only on Java 13+ if that can inspire anyone to golf further...

n->(" %sNOPQRSTUVWXY".substring(0,n+2)).repeat(n).formatted((Object[])"ABCDEFGHIJKLM".split("",n+1))

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

dc, 52 bytes

[nAP1-d0<M]sL?dsnCo[d96+POO^OO^Bd*/-ODln-^/d0<L]dsMx

Try it online!

Input is on stdin, and output is on stdout.

Output for 13:

mBA9876543210
lBA9876543210
kBA9876543210
jBA9876543210
iBA9876543210
hBA9876543210
gBA9876543210
fBA9876543210
eBA9876543210
dBA9876543210
cBA9876543210
bBA9876543210
aBA9876543210

How it works:

[                     Start a macro.
 n                       Pop a number and print it.
 AP                      Print a newline.
 1-                      Decrement top of stack by 1.
 d0<                     If top of stack > 0,
 M                         then continue by calling macro M.
]sL                   End macro and save it in register L.

?                     Input number and push it on stack.
dsn                   Store top of stack in n.
Co                    Change output radix to base 12.

[                     Start a macro.
 d96+P                   Print the character with
                           ASCII code 96 + (top of stack).
                           (This will be a lower-case letter,
                           since 97 = 'a'.)
 OO^OO^Bd*/-ODln-^/      Push (12^12 - (12^12)/(11*11)) / (12^(13-n)).
                           In base 12, this is the leftmost n-1 digits
                           of BA9876543210 (or 0 for n=1).
 d0<L                    If this number > 0, call macro L to print it,
                           decrement the value of n at the top of stack,
                           and go back to the top of the loop M.
]dsMx                   End macro, save it in register M, and execute it.
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Retina, 38 26 bytes

.+
*.
Y`.`l
L$`.
$=
Y`a`Rl

-12 bytes thanks to @Neil

Try it online!

This works by generating the beginning of 'abc...' length N, then repeating it and substituting the first letter for something from 'zyx...'

.+         This converts the number into unary, using dots
*.         ^
Y`.`l      A cyclic transliteration: replace all dots with something from a-m
L$`.       Repeat per N with a line break at the end
$=         ^
Y`a`Rl     Finally, transliterate each 'a' with something from z-n
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Oh, an actual use for the Y stage! An obvious saving is that all the lines will start with a, so there's no need to replace it with #, just transliterate the as directly. Then you can save a bit more by using the L$ stage to repeat rows by matching . and specifying a substitution of $=. (If not for that, the closing ) would have been unnecessary.) \$\endgroup\$ – Neil May 18 at 19:35
  • \$\begingroup\$ Thanks! I don't know how I didn't realise the a thing, and I thought there must be a better way to repeat a line \$\endgroup\$ – lolad May 19 at 8:49
2
\$\begingroup\$

B (asm2bf dialect), 136 bytes

p(c){asm("rclr1,r4");asm("outr1");}g(){asm("in r1");}n;c 65;d;i;main(){i=n=g();while(i--){p(c++);d=65+n;while(d-n-65<n-1)p(d++);p(10);}}

Output assembly:

#!/usr/bin/env bfmake
    stk 16
    org 0
db_ 0
db_ 65
db_ 0
db_ 0
#PAGE_SIZE = 16
#MM_BASE = 5
#call("alloc")
    mov r4, r6
#call("_main")
    end
@alloc
#alloc("r6", "r5")
    ret
@_p
    rclr1,r4
    outr1
    ret
@_g
    in r1
    ret
@_main
    psh 3
    psh 0
#call("_g")
    mov r2, r1
    pop r1
    sto r1, r2
    pop r1
    sto r1, r2
@L1
    mov r2, 3
    rcl r1, r2
    dec r1
    sto r2, r1
    inc r1
    jz_ r1, %L2
    psh r4
#call("alloc")
    mov r2, 1
    rcl r1, r2
    inc r1
    sto r2, r1
    dec r1
    sto r6, r1
    mov r4, r6
#call("_p")
#free("r4")
    pop r4
    psh 2
    psh 65
    rcl r1, 0
    mov r2, r1
    pop r1
    add r1, r2
    mov r2, r1
    pop r1
    sto r1, r2
@L3
    rcl r1, 2
    psh r1
    rcl r1, 0
    mov r2, r1
    pop r1
    sub r1, r2
    mov r2, 65
    sub r1, r2
    psh r1
    rcl r1, 0
    mov r2, 1
    sub r1, r2
    mov r2, r1
    pop r1
    lt_ r1, r2
    jz_ r1, %L4
    psh r4
#call("alloc")
    mov r2, 2
    rcl r1, r2
    inc r1
    sto r2, r1
    dec r1
    sto r6, r1
    mov r4, r6
#call("_p")
#free("r4")
    pop r4
    jmp %L3
@L4
    psh r4
#call("alloc")
    sto r6, 10
    mov r4, r6
#call("_p")
#free("r4")
    pop r4
    jmp %L1
@L2
    ret
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 74 bytes

n=>(q=`opqrstuvwxyz,`.slice(13-n)).replace(/./g,t=>i.toString(++i)+q,i=10)

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Ruby, 47 bytes

->n{a=*?`..?z;(1..n).map{|i|a[i]+a[14,n-1]*''}}

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -na, 42 bytes

say$_,(A..Z)[0..$F[0]-2]for(N..Z)[0..$_-1]

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice! Had basically the same! :) You can save a few bytes using "@F" instead of $F[0] and reorganising the letters a bit (to avoid arithmetic): Try it online! \$\endgroup\$ – Dom Hastings May 18 at 11:29
2
\$\begingroup\$

Bash + Unix utilities, 62 59 bytes

dc -e'[nAP1-d0<M]sL?dsnCo[d96+POO^OO^Bd*/-ODln-^/d0<L]dsMx'

Try it online!

Input is on stdin, and output is on stdout.



Here's the original, longer answer:

echo {a..m}`echo {o..z}|tr -d \ `|fold -14|cut -b 1-$1|sed $1q

Try it online (62 bytes)

Input is passed as an argument, and output is on stdout.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Befunge-93, 53 51 bytes

&:v
< <>::      v:::\,+*77:
|  ^-1,+*88<_$$\1-0.:
@

Try it online!

0 is my two character delimiter. Outer loop outputs N'+(7*7) in ascii, sets M to N (this requires a swap) then enters inner loop. Inner loop outputs M+(8*8) in ascii and decrements M. On exiting inner loop outputs 0 and decrements N' (this requires a swap). | and _ are the loop condition instructions respectively. : is often used to make copies since most operations - from arithmetic to conditional check, destroy the value they operate on by popping it out of the stack

Befunge is stack based with a single instruction pointer that points to a character in the code. It has a travelling direction that can be changed via arrows <>^v

Befunge-98 submission by ovs, 43 bytes

&:>:77*+,\:>:: v
.:|;-1,+*8;^;8<_$$\1-0
  @

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ There's wasteful spaces that a better golfer could probably get rid of \$\endgroup\$ – ghosts_in_the_code May 18 at 9:27
  • \$\begingroup\$ 43 bytes by removing most of the spaces from the 53 byte version: tio.run/##S0pNK81LT9W1tPj/… \$\endgroup\$ – ovs May 19 at 8:00
  • \$\begingroup\$ You can use ' to push integers like 64 and 49 with just two bytes. \$\endgroup\$ – ovs May 19 at 8:04
  • \$\begingroup\$ @ovs Oh, I was looking at the specification on wikipedia which was for Befunge-93 (when writing my answer), didn't notice the difference. Should I edit your answer in or change mine to 93? Also I didn't understand what ' does in befunge-98 \$\endgroup\$ – ghosts_in_the_code May 19 at 8:23
  • 1
    \$\begingroup\$ ' pushes the character value of the next cell on the stack. An example: '@ pushes 64 on the stack. (A complete funge-98 specification can be found at quadium.net/funge/spec98.html) \$\endgroup\$ – ovs May 19 at 8:33
1
\$\begingroup\$

C (gcc), 98 88 bytes

-10 thanks to @ceilingcat

c,d,i,j;f(n){for(c=65,i=n,d=c+n;i--;puts(""))for(j=!putchar(c++);j<n-1;)putchar(d+j++);}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 86 bytes by removing the (as far as I think they are called in this terrible version of C) forward declarations of c,d,i: Try it online! (I'm also not sure why you are compiling with -Dp=putchar) \$\endgroup\$ – my pronoun is monicareinstate May 17 at 15:39
  • \$\begingroup\$ it's a leftover; I probably forgot to remove it \$\endgroup\$ – Szewczyk May 17 at 15:46
  • \$\begingroup\$ Why not d=78? \$\endgroup\$ – l4m2 May 17 at 16:17
  • \$\begingroup\$ Suggest ~j+n instead of j<n-1 \$\endgroup\$ – ceilingcat May 17 at 16:18
  • \$\begingroup\$ 73 bytes (I changed almost everything though, not sure whether I should post this as my own answer): Try it online! \$\endgroup\$ – my pronoun is monicareinstate May 17 at 16:30
1
\$\begingroup\$

Jelly, 9 bytes

Øa;€Ṛ$ḣḣ€

A monadic Link accepting an integer in \$[1,13]\$ which yields a list of lists of characters.

Try it online!

How?

Øa;€Ṛ$ḣḣ€ - Link: integer, N
Øa        - lower-case alphabet
     $    - last two links as a monad:
    Ṛ     -   reverse (the alphabet)
  ;€      -   concatenate that to each of (the alphabet)
      ḣ   - head to index (N)
       ḣ€ - head each to index (N)
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js),  66  60 bytes

f=(n,k=n*n)=>k?Buffer(k--%n?[97+k%n]:[10,123-k/n])+f(n,k):''

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 76 bytes

=ArrayFormula(Char(Row(Offset(78:78,,,A1)))&Join(,Char(Row(Offset(65:65,,,A1

When you exit the cell, Sheets will automatically add the 5 trailing parentheses. Input is in cell A1. Output is wherever you put the formula and the N-1 cells below it.

Row(Offset(78:78,,,A1)) gives us an array from 78 to 78+N-1.
Char(Row(~)) turns that array into their ASCII equivalent (capital letters).
Char(Row(Offset(65:65,,,A1))) does the same thing for the range 65 to 65+N-1.
Join(Char(~)) combines that second array into a single string.
ArrayFormula(~) makes these functions input and output arrays instead of a single value.

enter image description here

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

[MATLAB/Octave], 35 bytes

char([N+[1:N]',ones(N,1)*[1:N]]+64)

First create a column vector ranging from N+1 to 2N with N+[1:N]. Make a column vector with all values equal to one and length N, and multiply by a row vector containing values 1 to N to make a matrix of N columns with all rows equal to 1:N. Concatenate the first vector with your matrix, add 64 to all digits and use char to turn every row into a string.

| improve this answer | |
\$\endgroup\$

Your Answer

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