title: What surrounds my Minesweeper tile?

text:
# Question
Title: What surrounds my Minesweeper tile?
Minesweeper is a puzzle game where mines are hidden around a board of nondescript tiles with the goal of identifying the location of all mines. Clicking on a mine loses the game, but clicking on any other tile will reveal a number from 0-8 which signifies how many mines directly surround it.

Given a number, you must display a random\* possible combination of empty tiles and mines surrounding it. This should be in the form of a 3x3 array. The center tile should be the number of mines taken as input.

\*Must have a non-zero chance for all combinations to occur.

---

## Examples

```
_ = blank square
X = mine

0

___
_0_
___

1

_X_
_1_
___

1

___
_1_
X__

___
_1_
__X

4

_X_
X4X
_X_

4

X_X
_4_
X_X

4

___
X4X
X_X

8

XXX
X8X
XXX

```

---

### Input

* The number of mines surrounding the center tile (0-8)

---

### Output

* Any reasonable form of output that displays the 3x3 array of tiles

---

## Other rules

* Each combination does not have to have an equal chance of happening. There just must be a non-zero chance of each combination to occur when executing your program.
* Any 2 characters can be chosen for the mine and empty tile.
* This is code golf, the program with fewest bytes wins.

# Answer
> 5 votes
# Jelly, 9 bytes

```
<Ɱ8Ẋs4js3

```

Try it online!

empty = `1`
mine = `0`

Note that `1` and `0` are integers.

Another note: this is somewhat similar to Jonathan Allan's 10-byte answer, but it's not really influenced by it in any way, and the mechanism, if you pay close attention, is actually more different than at first glance.

# Answer
> 9 votes
# APL (Dyalog Unicode), 28 15 bytes

-13 bytes thanks to ngn!

```
{3 3⍴5⌽⍵,⍵≥8?8}

```

## Explanation:

`{...}` A direct function (D-Fn),  is its right argument.

`8?8` deal 8 random numbers from the list 1..8:

```
      8?8
7 2 1 8 4 6 5 3

```

`⍵≥` is the argument greater or equal to each of them?:

```
      5 ≥ 7 2 1 8 4 6 5 3
0 1 1 0 1 0 1 1

```

`⍵,` prepend the argument to the boolean list:

```
      5 , 0 1 1 0 1 0 1 1
5 0 1 1 0 1 0 1 1

```

`5⌽` rotate the list 5 positions to the left, so that the argument is in the center:

```
      5 ⌽ 5 0 1 1 0 1 0 1 1
1 0 1 1 5 0 1 1 0

```

`3 3⍴` reshape the list to a 3x3 matrix:

```
      3 3 ⍴ 1 0 1 1 5 0 1 1 0
1 0 1
1 5 0
1 1 0

```

Try it online!

# , 15 bytes

Also many bytes thanks to ngn!

```
3 3$5|.],]>8?8:

```

Try it online!

# Answer
> 8 votes
# JavaScript (ES6), 67 bytes

*Shorter version suggested by @tsh*

Empty slots are `0`, mines are `1`.

```
n=>`___
_${t=9,n}_
___`.replace(/_/g,_=>n-(n-=Math.random()<n/--t))

```

Try it online!

---

# Original trial-and-error version, 78 bytes

Empty slots are `_`, mines are `7`.

```
f=(n,o=`___
_${k=n}_
___`.replace(/_/g,c=>Math.random()<.5?--k|7:c))=>k?f(n):o

```

Try it online!

### Commented

```
f = (                         // f = recursive function
  n,                          // n = input
  o = `___\n_${k = n}_\n___`  // o = minesweeper field / k = backup of n
    .replace(/_/g, c =>       // for each underscore character c in o:
      Math.random() < .5 ?    //   random action:
        --k | 7               //     either decrement k and yield 7
      :                       //   or:
        c                     //     let the underscore unchanged
    )                         // end of replace()
) =>                          //
  k ?                         // if k is not equal to 0:
    f(n)                      //   try again
  :                           // else:
    o                         //   stop recursion and return o

```

# Answer
> 6 votes
# Jelly,  ~~13~~  10 bytes

```
8Ẉ>RẊs4js3

```

The returned list of lists has the displayed integer in the centre surrounded by 0s and 1s representing mines and blanks respectively.

**Try it online!** (footer pretty prints the array)

### How?

```
8Ẉ>RẊs4js3 - Link: integer, n                   e.g.  3
8          - eight                                    8
 Ẉ         - length of each (implicit range of eight) [1,1,1,1,1,1,1,1]
   R       - range of n                               [1,2,3]
  >        - greater than? (vectorises)               [0,0,0,1,1,1,1,1]
    Ẋ      - shuffle                                  [1,1,1,0,0,1,1,0]
     s4    - split into chunks of 4                   [[1,1,1,0],[0,1,1,0]]
       j   - join (with n)                            [1,1,1,0,3,0,1,1,0]
        s3 - split into chunks of 3                   [[1,1,1],[0,3,0],[1,1,0]]

```

# Answer
> 6 votes
# Pyth, ~~16~~ 14 bytes

```
c3jQc2.S.[d8*N

```

Saved 2 bytes thanks to isaacg.
Uses spaces for safe spots and quotes for mines.
Try it here

### Explanation

```
c3jQc2.S.[d8*N
            *NQ     Get (implicit) input number of quotes...
        .[d8        ... and pad to length 8 with spaces.
      .S            Shuffle.
  jQc2              Stick the input in the middle.
c3                  Split into three.

```

# Answer
> 5 votes
# Oracle 18 SQL, 230 bytes

Not a golfing language but...

```
WITH v(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1))SELECT v.v||b.v||c.v||'
'||d.v||n||e.v||'
'||f.v||g.v||h.v
FROM n,v,v b,v c,v d,v e,v f,v g,v h
WHERE v.v+b.v+c.v+d.v+e.v+f.v+g.v+h.v=n
ORDER BY DBMS_RANDOM.VALUE
FETCH NEXT ROW ONLY

```

The input value is in a table `n` with column `n`:

```
CREATE TABLE n(n) AS
SELECT 7 FROM DUAL;

```

Try it online - log onto https://livesql.oracle.com and paste it into a worksheet.

Output:

```
V.V||B.V||C.V||''||D.V||N||E.V||''||F.V||G.V||H.V
-------------------------------------------------
101
171
111

```

To get all possible combinations (183 Bytes):

```
WITH v(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1))SELECT v.v||b.v||c.v||'
'||d.v||n||e.v||'
'||f.v||g.v||h.v
FROM n,v,v b,v c,v d,v e,v f,v g,v h
WHERE v.v+b.v+c.v+d.v+e.v+f.v+g.v+h.v=n

```

Output:

```
V.V||B.V||C.V||''||D.V||N||E.V||''||F.V||G.V||H.V
-------------------------------------------------
111
171
110

111
171
101

111
171
011

111
170
111

111
071
111

110
171
111

101
171
111

011
171
111

```

# Answer
> 3 votes
# Charcoal, 19 bytes

```
Ｗ¬⁼ΣＩＫＡ⁸«Ｅ³⭆³‽²↑↗↖θ

```

Try it online! Link is to verbose version of code. Uses `0` for a mine, `1` for an empty space. Explanation:

```
     ＫＡ             Peek the entire canvas
    Ｉ               Cast to integer
   Σ                Take the sum
       ⁸            Literal 8
  ⁼                 Equals
 ¬                  Logical Not
Ｗ       «           While
          ³ ³       Literal 3
         Ｅ          Map over implicit range
           ⭆        Map over implicit range and join
              ²     Literal 2
             ‽      Random element of implicit range
                    Implicitly print on separate lines
               ↑↗↖θ Print the original input in the middle

```
* `Peek` returns an array of strings, which `Sum` simply concatenates, which is why we have to cast to integer first. (`Sum(Sum(PeekAll()))` also works.)
* `Sum` returns `None` for an empty array (first loop), so the only safe comparison is `Not(Equals(...))`.
* Nilary `Random` always returns `0`, although its documentation says otherwise.

Alternative solution, was 19 bytes, now 17 bytes after a Charcoal bugfix:

```
θ←9Ｗ⁻ΣＩＫＡ⁸ＵＭＫＭＩ‽²

```

Try it online! Link is to verbose version of code. Uses `0` for a mine, `1` for an empty space. Explanation:

```
θ

```

Print the original input.

```
←9

```

Print a `9` leftwards. This moves the cursor back over the original input, and also forces at least one iteration of the while loop (otherwise an input of `8` would do nothing).

```
Ｗ⁻ΣＩＫＡ⁸

```

Repeat while the difference between the sum of all the digits on the canvas and 8 is nonzero:

```
ＵＭＫＭＩ‽²

```

Replace each of the surrounding characters randomly with `0` or `1`.

# Answer
> 3 votes
# Japt, 13 bytes

```
çÊú8 öÊi4U ò3

```

Try it

---

## Explanation

```
                   :Implicit input of integer U
 Ê                 :"l"
ç                  :Repeat U times
  ú8               :Pad right to length 8
    öÊ             :Random permutation
       i4U         :Insert U at index 4
            ò3     :Split at every 3rd character

```

# Answer
> 3 votes
# , ~~67 63 62~~ 59 bytes

```
matrix(c(sample(rep(1:0,c(n<-scan(),8-n))),n),6,5)[2:4,1:3]

```

Try it online!

Uses `1` and `0`. Build a `n* 1 +(8-n)* 0` vector, shuffles it, appends `n`, builds the bigger matrix shown below(where `a...i` stand for the elements of the original vector) and extracts the proper sub-matrix shown in uppercase:

```
     [,1] [,2] [,3] [,4] [,5]
[1,] "a"  "g"  "d"  "a"  "g"
[2,] "B"  "H"  "E"  "b"  "h"
[3,] "C"  "I"  "F"  "c"  "i"
[4,] "D"  "A"  "G"  "d"  "a"
[5,] "e"  "b"  "h"  "e"  "b"
[6,] "f"  "c"  "i"  "f"  "c"

```

# Answer
> 3 votes
# QBasic 1.1, ~~206~~ 186 bytes

```
RANDOMIZE TIMER
INPUT N
O=N
Z=8-N
FOR I=0TO 7
IF O*Z THEN
R=RND<.5
O=O+R
Z=Z-1-R
A(I)=-R
ELSEIF O THEN
O=O-1
A(I)=1
ELSE
Z=Z-1
A(I)=0
ENDIF
NEXT I
?A(0)A(1)A(2)
?A(3)N;A(4)
?A(5)A(6)A(7)

```

-20 thanks to DLosc (newly-posted golfing trick).

Empty = `0`
Mine = `1`

Note that `0` and `1` are integers, but I'm using STDOUT anyway, so...

Output appears like this:

```
 A  B  C
 D  x  E
 F  G  H

```

Where A-H are 0/1 and x is the input.

# Answer
> 2 votes
# Python 2, 93 bytes

```
from random import*
def f(n):a=[1]*n+[0]*(8-n);shuffle(a);a[4:4]=[n];return zip(*[iter(a)]*3)

```

Try it online!

`0` for empty; `1` for a mine; and `n` for one's self.

# Answer
> 2 votes
# Attache, 51 bytes

```
{[3,3]&Rotate[Sample[{Sum@_=_2}&_\BinBelow@8]'_,4]}

```

Try it online!

## Explanation

Similar to Galen's J/APL answer, the basic technique is to generate an array of 1s and 0s with the correct number of mines, inserting the input by appending it to the end, rotating the array such that the input lies in the center, then reshaping into a 3x3 matrix.

### Part 1: generating the binary array

There are many ways to go about doing this, but I mainly happened across two types: brute force and selection.

The primary brute force method looks like this:

```
NestWhile[{Random[8&2]},0,{Sum@_/=_2}&_]

```

This generates random arrays of 8 binary digits (`Random[8&2]`) while their sums do not equal the input `{Sum@_/=_2}&_`. This is a bit verbose, as the following emphasized portions of the code are there "just to make it work":

```
NestWhile[{Random[8&2]},0,{Sum@_/=_2}&_]
          ^           ^^^^^        ^^^^

```

And I discarded the idea.

Selection is more interesting. The main concept is to use the `BaseBelow[b, n]` builtin, which generates a list of all base-`b` integers of width `n` (as digit arrays), from `0` to `b^n-1`. For example, `BaseBelow[3, 2]` generates all ternary integers of width 2:

```
A> BaseBelow[3, 2]
 0 0
 0 1
 0 2
 1 0
 1 1
 1 2
 2 0
 2 1
 2 2

```

We specifically use `BaseBelow[2, 8]` for all binary integers of width 8. These represent all possible minefields of all lengths. This is the first step.

The second step is selecting all such arrays with only `N` 1s, where `N` is the input. My first idea was to translate this English statement directly into Attache:

```
Chunk[SortBy[Sum,BaseBelow[2,8]],Sum]@N@1

```

However, not only did this come out to be 1 byte longer than the aforementioned approach, it's also highly repetitive—and it isn't even randomized yet!. Sure, I could probably save 1 byte by reorganizing how `BaseBelow` is called, but it simply isn't worth using the approach.

So I decided to kill two birds with one stone and use a `Shuffle` based approach. The following gives all valid minefields of length `N` in random order:

```
{Sum@_=_2}&N\Shuffle[BaseBelow&8!2]

```

Then, all that needs to be done is to select the first. But I can do better—surely it'd be better to simply `Sample` the filtered array? This approach comes out to be something like this:

```
Sample[{Sum@_=_2}&_\BaseBelow[2,8]]

```

I had to revert the `BaseBelow&8!2` golf because `\`s precedence is too high. Otherwise satisfied, I proceeded to chop a byte off of that:

```
Sample[{Sum@_=_2}&_\2&BaseBelow@8]

```

(I discovered another way to succinctly call a dyadic function here: `x&f@y` is a high-precedence expression which evaluates to `f[x, y]`.)

However, despite this, I remembered that, all along, an alias for `2&BaseBelow` existed: `BinBelow`. So I used that:

```
Sample[{Sum@_=_2}&_\BinBelow@8]

```

This generates the desired minefield. I'm convinced this is near optimal.

### Part 2: Forming the array

As said earlier, the forming technique I used is similar to the J/APL answer, so I won't go into too much detail. Suppose `MINEFIELD` is the result from the last section. The function then becomes:

```
{[3,3]&Rotate[MINEFIELD'_,4]}

```

`MINEFIELD'_` concatenates the minefield with the original input `_`, giving us something like this:

```
[1, 0, 0, 0, 1, 0, 0, 1, 3]

```

Then, `Rotate[MINEFIELD'_,4]` rotates this list `4` times to the left, placing the center:

```
[1, 0, 0, 1, 3, 1, 0, 0, 0]

```

The last step is using `[3,3]&` to reshape the list into a 3x3 matrix:

```
 1 0 0
 1 3 1
 0 0 0

```

# Answer
> 2 votes
# Java 10, ~~165~~ ~~157~~ 141 bytes

```
n->{var r="___\n_"+n+"_\n___";for(int i;n>0;r=r.charAt(i*=Math.random())>58?r.substring(0*n--,i)+(i>9?0:0+r.substring(i+1)):r)i=11;return r;}

```

Empty tiles are `_` (any character with a unicode value above 58 is fine) and mines are `0`.

Try it online.

**Explanation:**

```
n->{                           // Method with integer parameter and String return-type
  var r="___\n_"+n+"_\n___";   //  Result-String, starting at:
                               //   "___
                               //    _n_
                               //    ___"
  for(int i;n>0                //  Loop as long as `n` isn't 0 yet:
      ;                        //    After every iteration:
       r=r.charAt(i*=Math.random())
                               //     Select a random integer in the range [0,11)
         >58?                  //     And if the character at this random index is a '_':
          r.substring(0*n--,i) //      Set `r` to the first part excluding character `i`,
                               //      (and decrease `n` by 1 in the process)
          +(i>9?0:0+           //      appended with a '0',
           r.substring(i+1))   //      appended with the second part
         :                     //     Else:
          r)                   //      Leave `r` unchanged
     i=11;                     //   Set `i` to 11 so a new random integer can be chosen
  return r;}                   //  Return the result

```

# Answer
> 2 votes
# PowerShell, 77 bytes

```
param($n)-join(0..7|random -c 8|%{'X_'[$_-ge$n]
$n[++$i-ne4]
'
'*($i-in3,5)})

```

Try it online!

# Answer
> 1 votes
# PHP, ~~135~~ ~~134~~ ~~123~~ ~~117~~ ~~122~~ 121 bytes

Looping over str to print instead saves 1 byte

str\_split and implode to insert the centre number saves 11 bytes

~~Don't need to assign the string to $s anymore saving 6 bytes~~
Yes you do. Otherwise the string is shuffled after every echo...

Removing whitespace after echo saves 1 byte

Replacing "\n" with an ctual line break saves 1 byte

```
$n=$argv[1];$s=implode($n,str_split(str_shuffle(str_pad(str_repeat(m,$n),8,n)),4));for(;$i<9;)echo$s[$i].(++$i%3?"":"
");

```

Try it online!

# Answer
> 1 votes
# Alice, 24 bytes

```
a8E/be?SZ!@
~E-\ita*.o/a

```

Try it online!

Empty slots are `9`, and mines are `0`.

I also made a 27-byte program with `1` for mines and `0` for empty slots:

```
a8Ea8/be?SZ!@
-E-9:\ita*.o/

```

Try it online!

# Answer
> 1 votes
# C (gcc), 81 bytes

```
f(n,i,j,k){for(j=i=8,k=n;--i+5;putchar(i%4?i-2?rand()%j--<k?k--,88:95:48+n:10));}

```

Try it online!

# Answer
> 1 votes
# PowerShell, ~~91~~ 86 bytes

-5 bytes thanks to mazzy

```
param($n)$x=,'X'*$n+,'_'*(8-$n)|random -c 8
-join$x[0..2]
$x[3,4]-join$n
-join$x[5..7]

```

Try it online!

Shuffles a generated string ranging from `________` to `XXXXXXXX` (replacing from the left). It then slices through it multiple times, inserting `$n` in the middle, to build the output string. This last part can probably be greatly optimized because each index costs a minimum of 5 bytes.

# Answer
> 0 votes
# Perl 5 `-pa`, ~~105~~ 101 bytes

```
@o=grep!/^....1/&&y/1//=="@F",map{sprintf"%09b",$_}0..2**9-1;$_=$o[rand@o];s/....\K./@F/;s/...\K/\n/g

```

Try it online!

# Answer
> 0 votes
# Ruby, 59 bytes

Returns a list of strings (one for each line).

```
->n{a=([0]*(8-n)+[1]*n).shuffle;a[4,0]=n;a.join.scan /.../}

```

Try it online!

# Answer
> 0 votes
# 05AB1E, 12 bytes

```
$×8j.r2äIý3ô

```

Uses `0` for mines, spaces for blank squares. Outputs a list of lines, which is pretty printed in the TIOs below by joining with newline delimiter (`»`).

Try it online or verify a few more test cases at once.

**Explanation:**

```
$             # Push 0 and the input-digit
 ×            # Repeat the 0 the input-digit amount of times as string
              #  i.e. 4 → "0000"
  8j          # Prepend spaces to make the size 8
              #  → "    0000"
    .r        # Randomly shuffle the characters in this string
              #  i.e. "    0000" → " 00 0  0"
      2ä      # Split it into two equal halves (of 4 characters)
              #  → [" 00 ","0  0"]
        Iý    # Join it with the input-digit
              #  → " 00 40  0"
          3ô  # And then split it into (three) parts of 3 characters
              #  → [" 00"," 40","  0"]
              # (which is output implicitly as result)

```

**12 bytes alternative:**

```
8L@.rIš5._3ô

```

Uses `1` for mines, `0` for blank squares. Outputs a matrix of digits, which is pretty printed in the TIOs below by joining each row, and then these rows with newline delimiter ().

Try it online or verify a few more test cases at once.

**Explanation:**

```
8L            # Push the list [1,2,3,4,5,6,7,8]
  @           # Check for each if the (implicit) input-integer is >= it
              # (1 if truthy; 0 if falsey)
              #  i.e. 4 → [1,1,1,1,0,0,0,0]
   .r         # Randomly shuffle this list
              #  i.e. [1,1,1,1,0,0,0,0] → [0,1,1,0,1,0,0,1]
     Iš       # Prepend the input-digit to the list
              #  → [4,0,1,1,0,1,0,0,1]
       5._    # Rotate the list five times towards the left
              #  → [1,0,0,1,4,0,1,1,0]
          3ô  # And then split it into (three) parts of 3 digits each
              #  → [[1,0,0],[1,4,0],[1,1,0]]
              # (which is output implicitly as result)

```

# Answer
> 0 votes
# Vyxal `j`, 53 bitsv2, 6.625 bytes

```
IṅÞ℅½j²

```

Try it Online!

Bitstring:

```
01011100001000000101111111001100101100100000000011001

```

Spaces for mines, 0 for empty spaces

```
IṅÞ℅½j²
I        # Push n spaces
 ṅ       # pad with zeros to the nearest multiple of 8
  Þ℅     # shuffle
    ½j   # split in half and join by the input
      ²  # shape as a square
# j flag joins on newlines

```

Created with the help of Luminespire.

---
Tags: code-golf, random, minesweeper
---


# Question
Title: An Array of Challenges #1: Alternating Arrays
# Alternating Arrays

An *alternating array* is a list of any length in which two (not necessarily different) values are alternating. That is to say, all even-indexed items are equal, and all odd-indexed items are equal.

Your task is to write a program or function which, when given a list of positive integers, outputs/returns `truthy` if it is alternating and `falsy` otherwise.

This is , so the shortest code (in bytes) wins!

**Edge Cases:**

```
[]      ->  True
[1]     ->  True
[1,1]   ->  True
[1,2,1] ->  True

```

**Other Test Cases:**

```
[1,2,1,2]      -> True
[3,4,3]        -> True
[10,5,10,5,10] -> True
[10,11]        -> True
[9,9,9,9,9]    -> True

[5,4,3,5,4,3]   -> False
[3,2,1,2,1,2]   -> False
[1,2,1,2,1,1,2] -> False
[2,2,3,3]       -> False
[2,3,3,2]       -> False

```

# Example

Here is an example you can test your solution against, written in Python 3 (not golfed):

```
def is_alternating(array):
    for i in range(len(array)):
        if array[i] != array[i%2]:
            return False
    return True

```

# Answer
# Jelly, 4 bytes

```
ḣ2ṁ⁼

```

Try it online!

### How it works

```
ḣ2ṁ⁼  Main link. Argument: A (array)

ḣ2    Head 2; truncate A after its second element. If A has two or less elements,
      this returns A itself.
  ṁ   Mold; cyclically repeat the elements of the previous result to create an
      array that has the same shape/length as A.
   ⁼  Test the result for equality with A.

```
> 28 votes

# Answer
## brainfuck, 34 bytes

```
,>,>+>,
[
  [<+<<->>>-]
  +<[-<<]
  >[>]
  ,
]
<.

```

Takes the array as byte values in a string, and outputs `\x00` for false and `\x01` for true.

Try it online.

This maintains the structure

`a b 1 c`

on the tape, where `c` is the current character, `b` is the previous character, and `a` is the previous previous character, as long as the array is alternating. If a mismatch is found, the pointer is moved to the left such that `a`, `b`, and the `1` flag all become zero, and this situation will continue until all the input is consumed.
> 13 votes

# Answer
# R, ~~24~~ 23 bytes

```
all((a=scan())==a[1:2])

```

Reads a vector into STDIN, takes the first two elements of that vector, and checks equality. If the lengths of `a[1:2]` and a don't match, R will loop through `a[1:2]` to match the length of a. It will give a warning about doing so, but it will work.

Surprisingly this even works for empty input, not sure why, but I'll roll with it.

Saved 1 byte thanks to @MickyT
> 13 votes

# Answer
# MATL, ~~7~~ 6 bytes

```
2YCs&=

```

For alternating arrays this outputs a non-empty matrix of ones, which is truthy. For non-alternating arrays the matrix contains at least one zero, and is thus falsy (see here).

Try it online! Or verify all test cases.

### Explanation

Let's take `[1 2 1 2]` as example input.

```
2YC   % Implicit input. Build matrix whose columns are overlapping blocks of
      % length 2. If input has size less than 2 this gives an empty array
      % STACK: [1 2 1;
                2 1 2]
s     % Sum of each column. For an empty array this gives 0
      % STACK: [3 3 3]
&=    % Matrix of all pairwise equality comparisons. Implicit display
      % STACK: [1 1 1;
                1 1 1;
                1 1 1]

```
> 11 votes

# Answer
## JavaScript (ES6), 27 bytes

```
a=>!a.some((v,i)=>a[i&1]-v)

```

### Test cases

```
let f =

a=>!a.some((v,i)=>a[i&1]-v)

console.log(f([]));              // -> True
console.log(f([1]));             // -> True
console.log(f([1,1]));           // -> True
console.log(f([1,2,1]));         // -> True
console.log(f([1,2,1,2]));       // -> True
console.log(f([10,5,10,5,10]));  // -> True
console.log(f([10,11]));         // -> True
console.log(f([9,9,9,9,9]));     // -> True
console.log(f([5,4,3,5,4,3]));   // -> False
console.log(f([3,2,1,2,1,2]));   // -> False
console.log(f([1,2,1,2,1,1,2])); // -> False
console.log(f([2,2,3,3]));       // -> False
console.log(f([2,3,3,2]));       // -> False
```
> 9 votes

# Answer
## Retina, 25 bytes

```
M`\b(\d+),\d+,(?!\1\b)
^0

```

Try it online!

Instead of matching an input with alternating values (which leads to some annoying edge effects in a regex), I'm matching inputs that *aren't* valid and then negate the result afterwards.

The benefit of matching an invalid input is that this is a property can be checked locally, and it doesn't need to treat empty or short input specially: any input is invalid if it contains two distinct values that are one position apart.

So the first stage counts the number of matches of `\b(\d+),\d+,(?!\1\b)` which matches and captures one value, then matches the next value, and then asserts that the third value in sequence is different. This gives zero for valid inputs and something positive for invalid values.

The second stage simply counts the number of matches of `^0` which is `1` if the first stage returned `0` and `1` otherwise.
> 8 votes

# Answer
# Mathematica, 29 bytes

```
#=={}||Equal@@(Most@#+Rest@#)&

```

A port of Luis Mendo's MATL algorithm. Unnamed function taking a list of numbers (or even more general objects) and returning `True` or `False`. Tests whether sums of consecutive elements are all equal. Unfortunately `Most` and `Rest` choke on the empty list, so that has to be tested separately.

# Mathematica, 33 bytes

```
Differences[#,1,2]~MatchQ~{0...}&

```

Unnamed function taking a list of numbers (or even more general objects) and returning `True` or `False`. The function `Differences[#,1,2]` takes the differences, not of *consecutive* pairs of integers, but pairs of integers at distance two apart. Then we just check whether the resulting list has nothing other than zeros in it.

As a bonus, for one more byte (change the `2` to `#2`), we get a function that inputs a list of integers and another positive integer `#2`, and checks whether the input list is the result of interleaving `#2` constant sequences periodically with one another. For example,

```
Differences[#,1,#2]~MatchQ~{0...}&[{1,2,3,4,5,1,2,3,4,5,1,2},5]

```

evaluates to `True`.
> 7 votes

# Answer
# Retina, ~~39~~ ~~32~~ 28 bytes

```
^(\d*)((,\d+)(,\1(\3|$))*)?$

```

Try it online!

Saved 7 bytes thanks to Martin! Saved another 3 thanks to Kobi! And to Kritixi for an idea for another 1.

We optionally match a number that occupies the entire input, any pair of numbers, or any pair of numbers followed by the same pair any number of times and optionally not including the second number at the very end. Could save 2 bytes if the input was in unary.
> 7 votes

# Answer
## Haskell, ~~27~~ 26 bytes

```
and.(zipWith(==)=<<drop 2)

```

This evaluates to an anonymous function that solves the challenge. The idea is to drop the first two numbers from the list, zip with the original list using equality, and check that the result only contains `True`s. Try it online!

Thanks to nimi for 1 byte!
> 7 votes

# Answer
# Pyth, 9 bytes

```
q<*<Q2lQl

```

### Explanation

```
q<*<Q2lQlQQ   Implicitly add enough Qs to make the code run

   <Q2        Take the first two elements of the input
  *   lQ      Repeat those len(input) times
 <      lQ    Take the first len(input) elements
q         Q   Check if those are equal to the input

```
> 5 votes

# Answer
# Perl 6,  ~~49 43~~  42 bytes

```
{!grep {![==] @_},roundrobin |.rotor: 2,:partial}

```

Try it

```
{!.grep: ->\a,\b=$_[1] {sum .[0,1]Z!==a,b}}

```

Try it

```
{!.grep: ->\a,\b=.[1] {sum .[0,1]Z!==a,b}}

```

Try it

## Expanded:

```
{
  !              # invert

  .grep:         # find any mismatches

  ->
    \a,
    \b = .[1]   # optional second parameter with default of second value from input
  {
    sum          # count up the values that don't match

    .[ 0, 1 ]    # the first two values from the input
    Z[![==]]     # zip not equal
    a, b         # the current two values under test.
  }
}

```
> 5 votes

# Answer
# Java 8, 63 bytes

```
i->{int r=0,x=1;for(;++x<i.length;)r|=i[x]-i[x-2];return r==0;}

```

This is a lambda expression for a `Predicate< int[ ] >`

Explanation: initialize the result to 0. For each element, Biteise OR the result with the difference between the current element and the element 2 indicies earlier. return `true` if the result equals 0. Otherwise return `false`
> 5 votes

# Answer
# Brachylog, 15 bytes

```
:{~c#Tbh#Co}f#=

```

Try it online!

### Explanation

```
:{         }f       Find all results of the predicate below
             #=     They must all be equal

  ~c#T              Deconcatenate the input into three lists
      bh#C          The middle list has two elements
        #Co         Order that couple of elements as the output

```
> 4 votes

# Answer
# APL, 7 bytes

```
⊢≡⍴⍴2⍴⊢

```

Explanation:

* `2⍴⊢`: reshape the input array by 2
* `⍴⍴`: reshape the result by the original size of the input, repeating elements
* `⊢≡`: see if the result of that is equal to the original input

Test cases:

```
      true←(1 2 1 2)(10 5 10 5 10)(10 11)(9 9 9 9 9)
      false←(5 4 3 5 4 3)(3 2 1 2 1 2)(1 2 1 2 1 1 2)(2 2 3 3)(2 3 3 2)
      ( ⊢≡⍴⍴2⍴⊢ ) ¨ true
1 1 1 1
      ( ⊢≡⍴⍴2⍴⊢ ) ¨ false
0 0 0 0 0

```
> 4 votes

# Answer
# Python 2, 35 bytes

```
lambda x:(x[:2]*len(x))[:len(x)]==x

```

Try it online!
> 3 votes

# Answer
# Haskell, ~~33~~ 32 bytes

```
f(a:x@(_:b:_))=a==b&&f x
f a=1<3

```

Try it online! or Verify the testcases. -1 byte thanks to Zgarb.
> 3 votes

# Answer
## bash, ~~56~~ ~~54~~ 38 bytes

```
[ -z $3 ]||((($1==$3))&&(shift;$0 $*))

```

Save this as a script, and pass the list of numbers as arguments (for an n-element list, you'll pass n arguments). The output is the exit code: 0 (for true) if the list is alternating, and 1 (for false) otherwise.

(Returning output in the exit code is allowed in the PPCG standard I/O methods.)

This works recursively:

* If the list has fewer than 3 elements, then exit with return code 0;
* else if the 1st element != the 3rd element, then exit with return code 1;
* else run the program recursively on the list with the first element removed.
> 3 votes

# Answer
# Brachylog, 6 bytes

```
s₂ᶠoᵐ=

```

Try it online!

### Explanation

Inspired by Luis Mendo's MATL answer:

```
s₂ᶠ     Find all length-2 sublists
   oᵐ   Sort each
     =  Assert that all sorted sublists are equal

```
> 3 votes

# Answer
# J, 8 bytes

```
-:$$2&{.

```

## Explanation

```
-:$$2&{.  input: (y)
    2&{.  the first two elements of y
   $      shaped like
  $       the shape of y
-:        and check if they match

```

## Test cases

```
   f =: -:$$2&{.
   ]true =: '' ; 1 ; 1 1 ; 1 2 1 ; 1 2 1 2 ; 10 5 10 5 10 ; 10 11 ; 9 9 9 9 9
++-+---+-----+-------+------------+-----+---------+
||1|1 1|1 2 1|1 2 1 2|10 5 10 5 10|10 11|9 9 9 9 9|
++-+---+-----+-------+------------+-----+---------+
   f each true
+-+-+-+-+-+-+-+-+
|1|1|1|1|1|1|1|1|
+-+-+-+-+-+-+-+-+
   ]false =: 5 4 3 5 4 3 ; 3 2 1 2 1 2 ; 1 2 1 2 1 1 2 ; 2 2 3 3 ; 2 3 3 2
+-----------+-----------+-------------+-------+-------+
|5 4 3 5 4 3|3 2 1 2 1 2|1 2 1 2 1 1 2|2 2 3 3|2 3 3 2|
+-----------+-----------+-------------+-------+-------+
   f each false
+-+-+-+-+-+
|0|0|0|0|0|
+-+-+-+-+-+

```
> 2 votes

# Answer
# Jelly, 3 bytes

```
+ƝE

```

Try it online!

`Ɲ` was added after the challenge

## How it works

```
+ƝE - Main link. Takes a list L on the left
 Ɲ  - Over overlapping pairs [x, y] in L:
+   -   Yield x+y
  E - Are all sums equal?

```

The sums will only be all equal iff all overlapping pairs are the same, ignoring order. This is only the case if the array is alternating, or if all elements are equal (which is alternating)
> 2 votes

# Answer
**Python 2.7, 38 bytes**

```
>> i=lambda a:(a[:2]*len(a))[0:len(a)]==a

```

**Test Cases:**

```
>> print i([1,2,1,2])
>> True
>> print i([10,5,10,5,10]
>> True
>> print i([5,4,3,5,4,3])
>> False
>> print i([3,2,1,2,1,2])
>> False

```
> 1 votes

# Answer
# C,  ~~52~~   ~~50~~   ~~49~~  47 bytes

*Thanks to @ceilingcat for golfing two bytes!*

```
f(i,l)int*i;{return--l<2?1:*i-i[2]?0:f(++i,l);}

```

Outputs 1 if the array alternates, 0 otherwise.

Try it online!
> 1 votes

# Answer
## Pyke, 6 bytes, noncompeting

```
2<Ql{q

```

Try it here!

```
2<     -   inp[:2]
    {  -  reshape(^, v)
  Ql   -   len(inp)
     q - ^ == inp

```

Allow reshape node to take a list as well as a string
> 1 votes

# Answer
# Jelly, 6 bytes

My first Jelly program! I know I'll never beat Dennis, but I'm awfully proud of this regardless.

```
s2ZE€Ạ // Main link: Argument A (array)
s2     // Split A into chunks of 2
       // [1,2,1,2] -> [[1,2],[1,2]]
  Z    // Transpose A
       // [[1,2],[1,2]] -> [[1,1],[2,2]]
   E€  // Map an equality check to each sublist of A
       // [[1,1],[2,2]] -> [1,1]
     Ạ // Any: return 0 if A contains any falsy values, else return 1
       // [1,1] -> 1

```

Try it here!
> 1 votes

# Answer
# **Shenzen IO (Assembler) ,~~83~~ 76 bytes, noncompeting**

Shenzen io is a puzzle game where you can code you code in a special assembler-ish language.

Unfortunately, you can only use integers between -999 and 999 as inputs or outputs, and there is no way to tell if an array has ended. So i assumed that the array was written on a ROM that wraps around after reading the last cell. This means that only even arrays can be used, which is the reason for it being noncompeting.

Code:

```
@mov x0 dat
@mov x0 acc
teq x0 dat
+teq x0 acc
b:
+mov 1 p1
-mov 0 p1
-jmp b

```

Explanation:

```
  # calling for x0 will cause rom to move 1 cell forward

 @ mov x0 dat # Moves value to variable dat (only run once)
 @ mov x0 acc # Moves rom position forward and moves x0 to acc
  teq x0 dat  # See if dat equals x0
+ teq x0 acc  # If last expression was true, see x0 equals acc
b:            # Label for jumps (GOTO)
+ mov 1 p1    # Set output (p1) to 1 (same case as previous line)
- mov 0 p1    # if any expression was false, set output to 0
- jmp b       # jump to b: (same case as prev line)

```

Sorry if any of this is confusing, this is my first code-golf answer.

EDIT: removed 7 bytes by replacing loops by run-once code
> 1 votes

# Answer
# Ruby, ~~131~~ 119 bytes

```
a=->x{!(x.values_at(*x.each_index.select{|i|i.even?}).uniq)[1]&!(x.values_at(*x.each_index.select{|i|i.odd?}).uniq[1])}

```

Lambda `a` expects an array `x` and returns true if there are 0 or 1 unique values for the odd indexed elements and 0 or 1 unique values for the even indexed elements in the array.

**Notable byte-safers**

* use of lambda over `def`
* `!arr[1]` vs. `arr.length < 2`
* `&` vs `&&`

Test cases

```
p a[[]]
p a[[1]]
p a[[1,1]]
p a[[1,2,1]]
p a[[1,2,1,2]]
p a[[3,4,3]]
p a[[10,5,10,5,10]]
p a[[10,11]]
p a[[9,9,9,9,9]]

#false
p a[[5,4,3,5,4,3]]==false
p a[[3,2,1,2,1,2]]==false
p a[[1,2,1,2,1,1,2]]==false
p a[[2,2,3,3]]==false
p a[[2,3,3,2]]==false

```
> 1 votes

# Answer
## Ruby, 23 bytes

```
->a{a[2..-1]==a[0..-3]}

```
> 1 votes

# Answer
## Dart, 46 bytes

```
(l){var i=0;return l.every((x)=>x==l[i++%2]);}

```

Run with:

```
void main() {
  var f = (l){var i=0;return l.every((x)=>x==l[i++%2]);};
  print(f([1,2,1,2,1]));
}

```
> 1 votes

# Answer
# C#, 54 bytes

```
using System.Linq;p=>!p.Where((v,i)=>v!=p[i%2]).Any();

```

Filter array to show values that do not match the first value for evens and the 2nd value for odds. If there are not any results, return true.
> 1 votes

# Answer
# Japt, ~~7~~ 6 bytes

```
eUîU¯2

```

Try it or run all test cases

```
           :Implicit input of array U
   U¯2     :Get the first 2 elements of U
 Uî        :Repeat that array to the length of U
e          :Test for equality with the original U

```
> 1 votes

# Answer
# Common Lisp, 62 bytes

```
(defun f(l)(or(not #1=(caddr l))(and(=(car l)#1#)(f(cdr l)))))

```

Try it online!
> 1 votes

# Answer
# 05AB1E, 5 bytes

```
2∍s∍Q

```

Try it online!

Explanation:

```
2∍s∍Q   //Full Program
2∍      //extend/shorten input to length 2       e.g. [1,2,1,2,2] -> [1,2]
  s∍    //extend/shorten to length of input      e.g. [1,2] -> [1,2,1,2,1]
    Q   //is equal to input                      e.g. [1,2,1,2,1] != [1,2,1,2,2]

```
> 1 votes

# Answer
# Vyxal `ag`, ~~5~~ 4 bytes

*-1 byte because flag.*

```
yWv≈

```

Try it Online!

Explanation:

```
       # 'a' flag - treat newline separated inputs as a list
y      # Uninterleave
 W     # Wrap resulting lists together into a list
  v≈   # On each list: are all elements the same?
       # 'g' flag - Output minimum value of the top of the stack

```

Essentially, given `<a|b|a|b>`, separate into `<a|a>` and `<b|b>`, then check if `a==a` and `b==b`. If either of those return 0, print 0. Otherwise, print 1.
> 1 votes

# Answer
# Factor, 42 bytes

```
[ dup <evens> std swap <odds> std + 0. = ]

```

Try it online!

* `dup <evens> std` Take the standard deviation of the even indices.
* `swap <odds> std` Take the standard deviation of the odd indices.
* `+ 0. =` Is their sum zero?
> 1 votes

# Answer
# Python 3, Python 2 22 bytes

Simple and efficient ^^

```
lambda x:x[2:]==x[:-2]

```

Try it online! (python 3)

Try it online! (python 2)
> 1 votes

# Answer
# Thunno 2 `M`, 3 bytes

```
z€ạ

```

Try it online!

#### Explanation

```
z€ạ  # Implicit input
z    # Uninterleave the input
     #  - push [input[0::2], input[1::2]]
     #   (i.e. [even indices, odd indices])
 €   # For each inner list:
  ạ  #  Check if the elements are all equal
     #   (i.e. [even indices all equal,
     #           odd indices all equal]
     # Take the minimum of this list
     #  - check if both were true
     #   (i.e. the input was alternating)
     # Implicit output

```
> 1 votes

# Answer
# Octave, 51 bytes

```
@(L)numel(L)<3||(f=@(n)isequal(L{n:2:end}))(1)&f(2)

```

Input is a cell array of positive integers.

Try it online!
> 0 votes

# Answer
# C#, 66 bytes

```
a=>{int r=1,i=0;for(;i<a.Length;)if(a[i]!=a[i++%2])r=0;return r;};

```

Anonymous function which receives an integer array and returns 1 if array is alternating and 0 otherwise.

Full program with ungolfed function and test cases:

```
using System;

public class Program
{
    public static void Main()
    {
        Func<int[], int> f =
        a =>
        {
            int r = 1,  // return value. 1 is true, by default
                i = 0;  // iterator
            for ( ; i<a.Length ; )  // for each array element
                if ( a[i] != a[i++%2] ) // if the even (or odd) elements are not the same
                    r = 0;      // a falsy (0) value will be assigned to the return element
            return r;       // returning if the array is alternating or not
        };

        // test cases:
        Console.WriteLine("Edge cases (all TRUE):");
        Console.WriteLine(f(new int[]{}));      //  True
        Console.WriteLine(f(new int[]{1}));     //  True
        Console.WriteLine(f(new int[]{1,1}));   //  True
        Console.WriteLine(f(new int[]{1,2,1})); //  True

        Console.WriteLine("Some other TRUE test cases:");
        Console.WriteLine(f(new int[]{1,2,1,2}));      // True
        Console.WriteLine(f(new int[]{10,5,10,5,10})); // True
        Console.WriteLine(f(new int[]{10,11}));        // True
        Console.WriteLine(f(new int[]{9,9,9,9,9}));    // True

        Console.WriteLine("Some FALSE test cases:");
        Console.WriteLine(f(new int[]{5,4,3,5,4,3}));   // False
        Console.WriteLine(f(new int[]{3,2,1,2,1,2}));   // False
        Console.WriteLine(f(new int[]{1,2,1,2,1,1,2})); // False
        Console.WriteLine(f(new int[]{2,2,3,3}));       // False
        Console.WriteLine(f(new int[]{2,3,3,2}));       // False
    }
}

```
> 0 votes

# Answer
**Clojure, 70 bytes**

```
(fn[c](let[n #(max(count(set(take-nth 2 %)))1)](=(n c)(n(rest c))1))))

```

Checks that the distinct count of every 2nd item is 1, and handles empty collections as a special case. Also tried many approaches based on `reduce` and `group-by` but not much luck there.
> 0 votes

# Answer
Another option with R: 36 bytes.

```
all(rep_len(head(x,2),length(x))==x)

```

And I think I've found a much shorter version: 15 bytes

```
all(!diff(x,2))

```
> 0 votes

# Answer
# PHP, 52 bytes

Run with `-r`.

```
foreach($argv as$i=>$n)$i<3|$n==$argv[$i-2]?:die(1);

```

loops from 3rd to last argument. Continue while argument equals that two positions earlier, else exit with code `1` (error). Exit with `0` (ok) after loop finishes.

or for 53 bytes:

```
while(++$i<$argc-3&$f=$argv[$i]==$argv[2+$i]);echo$f;

```

loops through all arguments but the last two. Continue while current argument equals that two positions later. Print `1` for true, nothing for false (string representations of `true` and `false`).
> 0 votes

# Answer
# Oracle SQL, 73 Bytes

```
select count(*)from(select a,lag(a,2)over(order by 1)b from t)where a!=b;

```

## Output:

```
True  =  0
False != 0

```

### True Example:

```
create table t (a number);
truncate table t;
insert into t values (10);
insert into t values (5);
insert into t values (10);
insert into t values (5);
insert into t values (10);
commit;

select count(*)from(select a,lag(a,2)over(order by 1)b from t)where a!=b;

  COUNT(*)
----------
         0

```

### False Example:

```
create table t (a number);
truncate table t;
insert into t values (1);
insert into t values (2);
insert into t values (3);
insert into t values (3);
insert into t values (1);
insert into t values (2);
commit;

select count(*) from(select a,lag(a,2)over(order by 1)b from t)where a!=b;

  COUNT(*)
----------
         4

```
> 0 votes

# Answer
# Perl, 27 bytes

Includes `+1` for `p`

```
perl -pE '$_=!/\b(\d+) \d+ (?!\1\b)/' <<< "1 2 1 2"

```
> 0 votes

# Answer
# Add++ v5.1, 9 bytes

```
L~,Ñ+B]B=

```

Try it online!
> 0 votes

# Answer
# Perl 6, 30 bytes

```
2>*[(0,2...*;1,3...*)].all.Set

```

Try it online!

Anonymous Whatever lambda that takes a list and returns an all Junction that boolifies to True/False.

### Explanation:

```
  *[(       ;       )]          # Index from the list
     0,2...*                    # The even indexed elements
             1,3...*            # The odd indexed elements
                      .all      # Are both the lists
                          .Set  # When converted to a set
2>                              # The length is smaller than 2?

```
> 0 votes

# Answer
# Pip, 8 bytes

```
$&$=*UWg

```

Attempt This Online!

### Explanation

```
$&$=*UWg
       g  ; List of command-line arguments
     UW   ; Unweave into two alternating sublists
  $=*     ; Fold each sublist on equality
$&        ; Fold those two results on logical AND

```

Created with the help of Luminespire.
> 0 votes

---
Tags: code-golf, array, decision-problem, integer
---


# Question
Title: Find the Chess960 starting positions from its integer ID
Chess960, a.k.a. Fisher random chess, is a variation of the game of chess.

In classic chess the starting positions are always the same. In chess960 they vary.

Before each game of chess960 one random number from 0 to 959 is drawn. Both players then arrange their pieces in the same way according to this number from a lookup table:

https://chess960.net/wp-content/uploads/2018/02/chess960-starting-positions.pdf

There are 960 *valid* starting positions (SP). They are indexed from 0 to 959 and often prefixed with "SP-" or "SP". The starting position of classic chess for instance is SP-518 that is RNBQKBNR or ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜

**The challenge**

Create a program or a function that when given an integer from 0 to 959 it prints or returns the starting position as a string of length 8 consisting of the letters **R**, **N**, **B**, **Q**, **K**. Two R (rooks), two N (kNights), two B (bishops). Then one Q (queen) and one K (king). Ignore the pawns as they are placed just as in classic chess.

Various methods exists to calculate the starting positions from 0-959. Some might be undiscovered? A few simple methods are described at: https://en.wikipedia.org/wiki/Fischer\_random\_chess\_numbering\_scheme

The array of valid starting positions are:

```
 0  BBQNNRKR   1 BQNBNRKR   2 BQNNRBKR   3 BQNNRKRB   4 QBBNNRKR   5 QNBBNRKR   6 QNBNRBKR   7 QNBNRKRB   8 QBNNBRKR   9 QNNBBRKR
10  QNNRBBKR  11 QNNRBKRB  12 QBNNRKBR  13 QNNBRKBR  14 QNNRKBBR  15 QNNRKRBB  16 BBNQNRKR  17 BNQBNRKR  18 BNQNRBKR  19 BNQNRKRB
20  NBBQNRKR  21 NQBBNRKR  22 NQBNRBKR  23 NQBNRKRB  24 NBQNBRKR  25 NQNBBRKR  26 NQNRBBKR  27 NQNRBKRB  28 NBQNRKBR  29 NQNBRKBR
30  NQNRKBBR  31 NQNRKRBB  32 BBNNQRKR  33 BNNBQRKR  34 BNNQRBKR  35 BNNQRKRB  36 NBBNQRKR  37 NNBBQRKR  38 NNBQRBKR  39 NNBQRKRB
40  NBNQBRKR  41 NNQBBRKR  42 NNQRBBKR  43 NNQRBKRB  44 NBNQRKBR  45 NNQBRKBR  46 NNQRKBBR  47 NNQRKRBB  48 BBNNRQKR  49 BNNBRQKR
50  BNNRQBKR  51 BNNRQKRB  52 NBBNRQKR  53 NNBBRQKR  54 NNBRQBKR  55 NNBRQKRB  56 NBNRBQKR  57 NNRBBQKR  58 NNRQBBKR  59 NNRQBKRB
60  NBNRQKBR  61 NNRBQKBR  62 NNRQKBBR  63 NNRQKRBB  64 BBNNRKQR  65 BNNBRKQR  66 BNNRKBQR  67 BNNRKQRB  68 NBBNRKQR  69 NNBBRKQR
70  NNBRKBQR  71 NNBRKQRB  72 NBNRBKQR  73 NNRBBKQR  74 NNRKBBQR  75 NNRKBQRB  76 NBNRKQBR  77 NNRBKQBR  78 NNRKQBBR  79 NNRKQRBB
80  BBNNRKRQ  81 BNNBRKRQ  82 BNNRKBRQ  83 BNNRKRQB  84 NBBNRKRQ  85 NNBBRKRQ  86 NNBRKBRQ  87 NNBRKRQB  88 NBNRBKRQ  89 NNRBBKRQ
90  NNRKBBRQ  91 NNRKBRQB  92 NBNRKRBQ  93 NNRBKRBQ  94 NNRKRBBQ  95 NNRKRQBB  96 BBQNRNKR  97 BQNBRNKR  98 BQNRNBKR  99 BQNRNKRB
100 QBBNRNKR 101 QNBBRNKR 102 QNBRNBKR 103 QNBRNKRB 104 QBNRBNKR 105 QNRBBNKR 106 QNRNBBKR 107 QNRNBKRB 108 QBNRNKBR 109 QNRBNKBR
110 QNRNKBBR 111 QNRNKRBB 112 BBNQRNKR 113 BNQBRNKR 114 BNQRNBKR 115 BNQRNKRB 116 NBBQRNKR 117 NQBBRNKR 118 NQBRNBKR 119 NQBRNKRB
120 NBQRBNKR 121 NQRBBNKR 122 NQRNBBKR 123 NQRNBKRB 124 NBQRNKBR 125 NQRBNKBR 126 NQRNKBBR 127 NQRNKRBB 128 BBNRQNKR 129 BNRBQNKR
130 BNRQNBKR 131 BNRQNKRB 132 NBBRQNKR 133 NRBBQNKR 134 NRBQNBKR 135 NRBQNKRB 136 NBRQBNKR 137 NRQBBNKR 138 NRQNBBKR 139 NRQNBKRB
140 NBRQNKBR 141 NRQBNKBR 142 NRQNKBBR 143 NRQNKRBB 144 BBNRNQKR 145 BNRBNQKR 146 BNRNQBKR 147 BNRNQKRB 148 NBBRNQKR 149 NRBBNQKR
150 NRBNQBKR 151 NRBNQKRB 152 NBRNBQKR 153 NRNBBQKR 154 NRNQBBKR 155 NRNQBKRB 156 NBRNQKBR 157 NRNBQKBR 158 NRNQKBBR 159 NRNQKRBB
160 BBNRNKQR 161 BNRBNKQR 162 BNRNKBQR 163 BNRNKQRB 164 NBBRNKQR 165 NRBBNKQR 166 NRBNKBQR 167 NRBNKQRB 168 NBRNBKQR 169 NRNBBKQR
170 NRNKBBQR 171 NRNKBQRB 172 NBRNKQBR 173 NRNBKQBR 174 NRNKQBBR 175 NRNKQRBB 176 BBNRNKRQ 177 BNRBNKRQ 178 BNRNKBRQ 179 BNRNKRQB
180 NBBRNKRQ 181 NRBBNKRQ 182 NRBNKBRQ 183 NRBNKRQB 184 NBRNBKRQ 185 NRNBBKRQ 186 NRNKBBRQ 187 NRNKBRQB 188 NBRNKRBQ 189 NRNBKRBQ
190 NRNKRBBQ 191 NRNKRQBB 192 BBQNRKNR 193 BQNBRKNR 194 BQNRKBNR 195 BQNRKNRB 196 QBBNRKNR 197 QNBBRKNR 198 QNBRKBNR 199 QNBRKNRB
200 QBNRBKNR 201 QNRBBKNR 202 QNRKBBNR 203 QNRKBNRB 204 QBNRKNBR 205 QNRBKNBR 206 QNRKNBBR 207 QNRKNRBB 208 BBNQRKNR 209 BNQBRKNR
210 BNQRKBNR 211 BNQRKNRB 212 NBBQRKNR 213 NQBBRKNR 214 NQBRKBNR 215 NQBRKNRB 216 NBQRBKNR 217 NQRBBKNR 218 NQRKBBNR 219 NQRKBNRB
220 NBQRKNBR 221 NQRBKNBR 222 NQRKNBBR 223 NQRKNRBB 224 BBNRQKNR 225 BNRBQKNR 226 BNRQKBNR 227 BNRQKNRB 228 NBBRQKNR 229 NRBBQKNR
230 NRBQKBNR 231 NRBQKNRB 232 NBRQBKNR 233 NRQBBKNR 234 NRQKBBNR 235 NRQKBNRB 236 NBRQKNBR 237 NRQBKNBR 238 NRQKNBBR 239 NRQKNRBB
240 BBNRKQNR 241 BNRBKQNR 242 BNRKQBNR 243 BNRKQNRB 244 NBBRKQNR 245 NRBBKQNR 246 NRBKQBNR 247 NRBKQNRB 248 NBRKBQNR 249 NRKBBQNR
250 NRKQBBNR 251 NRKQBNRB 252 NBRKQNBR 253 NRKBQNBR 254 NRKQNBBR 255 NRKQNRBB 256 BBNRKNQR 257 BNRBKNQR 258 BNRKNBQR 259 BNRKNQRB
260 NBBRKNQR 261 NRBBKNQR 262 NRBKNBQR 263 NRBKNQRB 264 NBRKBNQR 265 NRKBBNQR 266 NRKNBBQR 267 NRKNBQRB 268 NBRKNQBR 269 NRKBNQBR
270 NRKNQBBR 271 NRKNQRBB 272 BBNRKNRQ 273 BNRBKNRQ 274 BNRKNBRQ 275 BNRKNRQB 276 NBBRKNRQ 277 NRBBKNRQ 278 NRBKNBRQ 279 NRBKNRQB
280 NBRKBNRQ 281 NRKBBNRQ 282 NRKNBBRQ 283 NRKNBRQB 284 NBRKNRBQ 285 NRKBNRBQ 286 NRKNRBBQ 287 NRKNRQBB 288 BBQNRKRN 289 BQNBRKRN
290 BQNRKBRN 291 BQNRKRNB 292 QBBNRKRN 293 QNBBRKRN 294 QNBRKBRN 295 QNBRKRNB 296 QBNRBKRN 297 QNRBBKRN 298 QNRKBBRN 299 QNRKBRNB
300 QBNRKRBN 301 QNRBKRBN 302 QNRKRBBN 303 QNRKRNBB 304 BBNQRKRN 305 BNQBRKRN 306 BNQRKBRN 307 BNQRKRNB 308 NBBQRKRN 309 NQBBRKRN
310 NQBRKBRN 311 NQBRKRNB 312 NBQRBKRN 313 NQRBBKRN 314 NQRKBBRN 315 NQRKBRNB 316 NBQRKRBN 317 NQRBKRBN 318 NQRKRBBN 319 NQRKRNBB
320 BBNRQKRN 321 BNRBQKRN 322 BNRQKBRN 323 BNRQKRNB 324 NBBRQKRN 325 NRBBQKRN 326 NRBQKBRN 327 NRBQKRNB 328 NBRQBKRN 329 NRQBBKRN
330 NRQKBBRN 331 NRQKBRNB 332 NBRQKRBN 333 NRQBKRBN 334 NRQKRBBN 335 NRQKRNBB 336 BBNRKQRN 337 BNRBKQRN 338 BNRKQBRN 339 BNRKQRNB
340 NBBRKQRN 341 NRBBKQRN 342 NRBKQBRN 343 NRBKQRNB 344 NBRKBQRN 345 NRKBBQRN 346 NRKQBBRN 347 NRKQBRNB 348 NBRKQRBN 349 NRKBQRBN
350 NRKQRBBN 351 NRKQRNBB 352 BBNRKRQN 353 BNRBKRQN 354 BNRKRBQN 355 BNRKRQNB 356 NBBRKRQN 357 NRBBKRQN 358 NRBKRBQN 359 NRBKRQNB
360 NBRKBRQN 361 NRKBBRQN 362 NRKRBBQN 363 NRKRBQNB 364 NBRKRQBN 365 NRKBRQBN 366 NRKRQBBN 367 NRKRQNBB 368 BBNRKRNQ 369 BNRBKRNQ
370 BNRKRBNQ 371 BNRKRNQB 372 NBBRKRNQ 373 NRBBKRNQ 374 NRBKRBNQ 375 NRBKRNQB 376 NBRKBRNQ 377 NRKBBRNQ 378 NRKRBBNQ 379 NRKRBNQB
380 NBRKRNBQ 381 NRKBRNBQ 382 NRKRNBBQ 383 NRKRNQBB 384 BBQRNNKR 385 BQRBNNKR 386 BQRNNBKR 387 BQRNNKRB 388 QBBRNNKR 389 QRBBNNKR
390 QRBNNBKR 391 QRBNNKRB 392 QBRNBNKR 393 QRNBBNKR 394 QRNNBBKR 395 QRNNBKRB 396 QBRNNKBR 397 QRNBNKBR 398 QRNNKBBR 399 QRNNKRBB
400 BBRQNNKR 401 BRQBNNKR 402 BRQNNBKR 403 BRQNNKRB 404 RBBQNNKR 405 RQBBNNKR 406 RQBNNBKR 407 RQBNNKRB 408 RBQNBNKR 409 RQNBBNKR
410 RQNNBBKR 411 RQNNBKRB 412 RBQNNKBR 413 RQNBNKBR 414 RQNNKBBR 415 RQNNKRBB 416 BBRNQNKR 417 BRNBQNKR 418 BRNQNBKR 419 BRNQNKRB
420 RBBNQNKR 421 RNBBQNKR 422 RNBQNBKR 423 RNBQNKRB 424 RBNQBNKR 425 RNQBBNKR 426 RNQNBBKR 427 RNQNBKRB 428 RBNQNKBR 429 RNQBNKBR
430 RNQNKBBR 431 RNQNKRBB 432 BBRNNQKR 433 BRNBNQKR 434 BRNNQBKR 435 BRNNQKRB 436 RBBNNQKR 437 RNBBNQKR 438 RNBNQBKR 439 RNBNQKRB
440 RBNNBQKR 441 RNNBBQKR 442 RNNQBBKR 443 RNNQBKRB 444 RBNNQKBR 445 RNNBQKBR 446 RNNQKBBR 447 RNNQKRBB 448 BBRNNKQR 449 BRNBNKQR
450 BRNNKBQR 451 BRNNKQRB 452 RBBNNKQR 453 RNBBNKQR 454 RNBNKBQR 455 RNBNKQRB 456 RBNNBKQR 457 RNNBBKQR 458 RNNKBBQR 459 RNNKBQRB
460 RBNNKQBR 461 RNNBKQBR 462 RNNKQBBR 463 RNNKQRBB 464 BBRNNKRQ 465 BRNBNKRQ 466 BRNNKBRQ 467 BRNNKRQB 468 RBBNNKRQ 469 RNBBNKRQ
470 RNBNKBRQ 471 RNBNKRQB 472 RBNNBKRQ 473 RNNBBKRQ 474 RNNKBBRQ 475 RNNKBRQB 476 RBNNKRBQ 477 RNNBKRBQ 478 RNNKRBBQ 479 RNNKRQBB
480 BBQRNKNR 481 BQRBNKNR 482 BQRNKBNR 483 BQRNKNRB 484 QBBRNKNR 485 QRBBNKNR 486 QRBNKBNR 487 QRBNKNRB 488 QBRNBKNR 489 QRNBBKNR
490 QRNKBBNR 491 QRNKBNRB 492 QBRNKNBR 493 QRNBKNBR 494 QRNKNBBR 495 QRNKNRBB 496 BBRQNKNR 497 BRQBNKNR 498 BRQNKBNR 499 BRQNKNRB
500 RBBQNKNR 501 RQBBNKNR 502 RQBNKBNR 503 RQBNKNRB 504 RBQNBKNR 505 RQNBBKNR 506 RQNKBBNR 507 RQNKBNRB 508 RBQNKNBR 509 RQNBKNBR
510 RQNKNBBR 511 RQNKNRBB 512 BBRNQKNR 513 BRNBQKNR 514 BRNQKBNR 515 BRNQKNRB 516 RBBNQKNR 517 RNBBQKNR 518 RNBQKBNR 519 RNBQKNRB
520 RBNQBKNR 521 RNQBBKNR 522 RNQKBBNR 523 RNQKBNRB 524 RBNQKNBR 525 RNQBKNBR 526 RNQKNBBR 527 RNQKNRBB 528 BBRNKQNR 529 BRNBKQNR
530 BRNKQBNR 531 BRNKQNRB 532 RBBNKQNR 533 RNBBKQNR 534 RNBKQBNR 535 RNBKQNRB 536 RBNKBQNR 537 RNKBBQNR 538 RNKQBBNR 539 RNKQBNRB
540 RBNKQNBR 541 RNKBQNBR 542 RNKQNBBR 543 RNKQNRBB 544 BBRNKNQR 545 BRNBKNQR 546 BRNKNBQR 547 BRNKNQRB 548 RBBNKNQR 549 RNBBKNQR
550 RNBKNBQR 551 RNBKNQRB 552 RBNKBNQR 553 RNKBBNQR 554 RNKNBBQR 555 RNKNBQRB 556 RBNKNQBR 557 RNKBNQBR 558 RNKNQBBR 559 RNKNQRBB
560 BBRNKNRQ 561 BRNBKNRQ 562 BRNKNBRQ 563 BRNKNRQB 564 RBBNKNRQ 565 RNBBKNRQ 566 RNBKNBRQ 567 RNBKNRQB 568 RBNKBNRQ 569 RNKBBNRQ
570 RNKNBBRQ 571 RNKNBRQB 572 RBNKNRBQ 573 RNKBNRBQ 574 RNKNRBBQ 575 RNKNRQBB 576 BBQRNKRN 577 BQRBNKRN 578 BQRNKBRN 579 BQRNKRNB
580 QBBRNKRN 581 QRBBNKRN 582 QRBNKBRN 583 QRBNKRNB 584 QBRNBKRN 585 QRNBBKRN 586 QRNKBBRN 587 QRNKBRNB 588 QBRNKRBN 589 QRNBKRBN
590 QRNKRBBN 591 QRNKRNBB 592 BBRQNKRN 593 BRQBNKRN 594 BRQNKBRN 595 BRQNKRNB 596 RBBQNKRN 597 RQBBNKRN 598 RQBNKBRN 599 RQBNKRNB
600 RBQNBKRN 601 RQNBBKRN 602 RQNKBBRN 603 RQNKBRNB 604 RBQNKRBN 605 RQNBKRBN 606 RQNKRBBN 607 RQNKRNBB 608 BBRNQKRN 609 BRNBQKRN
610 BRNQKBRN 611 BRNQKRNB 612 RBBNQKRN 613 RNBBQKRN 614 RNBQKBRN 615 RNBQKRNB 616 RBNQBKRN 617 RNQBBKRN 618 RNQKBBRN 619 RNQKBRNB
620 RBNQKRBN 621 RNQBKRBN 622 RNQKRBBN 623 RNQKRNBB 624 BBRNKQRN 625 BRNBKQRN 626 BRNKQBRN 627 BRNKQRNB 628 RBBNKQRN 629 RNBBKQRN
630 RNBKQBRN 631 RNBKQRNB 632 RBNKBQRN 633 RNKBBQRN 634 RNKQBBRN 635 RNKQBRNB 636 RBNKQRBN 637 RNKBQRBN 638 RNKQRBBN 639 RNKQRNBB
640 BBRNKRQN 641 BRNBKRQN 642 BRNKRBQN 643 BRNKRQNB 644 RBBNKRQN 645 RNBBKRQN 646 RNBKRBQN 647 RNBKRQNB 648 RBNKBRQN 649 RNKBBRQN
650 RNKRBBQN 651 RNKRBQNB 652 RBNKRQBN 653 RNKBRQBN 654 RNKRQBBN 655 RNKRQNBB 656 BBRNKRNQ 657 BRNBKRNQ 658 BRNKRBNQ 659 BRNKRNQB
660 RBBNKRNQ 661 RNBBKRNQ 662 RNBKRBNQ 663 RNBKRNQB 664 RBNKBRNQ 665 RNKBBRNQ 666 RNKRBBNQ 667 RNKRBNQB 668 RBNKRNBQ 669 RNKBRNBQ
670 RNKRNBBQ 671 RNKRNQBB 672 BBQRKNNR 673 BQRBKNNR 674 BQRKNBNR 675 BQRKNNRB 676 QBBRKNNR 677 QRBBKNNR 678 QRBKNBNR 679 QRBKNNRB
680 QBRKBNNR 681 QRKBBNNR 682 QRKNBBNR 683 QRKNBNRB 684 QBRKNNBR 685 QRKBNNBR 686 QRKNNBBR 687 QRKNNRBB 688 BBRQKNNR 689 BRQBKNNR
690 BRQKNBNR 691 BRQKNNRB 692 RBBQKNNR 693 RQBBKNNR 694 RQBKNBNR 695 RQBKNNRB 696 RBQKBNNR 697 RQKBBNNR 698 RQKNBBNR 699 RQKNBNRB
700 RBQKNNBR 701 RQKBNNBR 702 RQKNNBBR 703 RQKNNRBB 704 BBRKQNNR 705 BRKBQNNR 706 BRKQNBNR 707 BRKQNNRB 708 RBBKQNNR 709 RKBBQNNR
710 RKBQNBNR 711 RKBQNNRB 712 RBKQBNNR 713 RKQBBNNR 714 RKQNBBNR 715 RKQNBNRB 716 RBKQNNBR 717 RKQBNNBR 718 RKQNNBBR 719 RKQNNRBB
720 BBRKNQNR 721 BRKBNQNR 722 BRKNQBNR 723 BRKNQNRB 724 RBBKNQNR 725 RKBBNQNR 726 RKBNQBNR 727 RKBNQNRB 728 RBKNBQNR 729 RKNBBQNR
730 RKNQBBNR 731 RKNQBNRB 732 RBKNQNBR 733 RKNBQNBR 734 RKNQNBBR 735 RKNQNRBB 736 BBRKNNQR 737 BRKBNNQR 738 BRKNNBQR 739 BRKNNQRB
740 RBBKNNQR 741 RKBBNNQR 742 RKBNNBQR 743 RKBNNQRB 744 RBKNBNQR 745 RKNBBNQR 746 RKNNBBQR 747 RKNNBQRB 748 RBKNNQBR 749 RKNBNQBR
750 RKNNQBBR 751 RKNNQRBB 752 BBRKNNRQ 753 BRKBNNRQ 754 BRKNNBRQ 755 BRKNNRQB 756 RBBKNNRQ 757 RKBBNNRQ 758 RKBNNBRQ 759 RKBNNRQB
760 RBKNBNRQ 761 RKNBBNRQ 762 RKNNBBRQ 763 RKNNBRQB 764 RBKNNRBQ 765 RKNBNRBQ 766 RKNNRBBQ 767 RKNNRQBB 768 BBQRKNRN 769 BQRBKNRN
770 BQRKNBRN 771 BQRKNRNB 772 QBBRKNRN 773 QRBBKNRN 774 QRBKNBRN 775 QRBKNRNB 776 QBRKBNRN 777 QRKBBNRN 778 QRKNBBRN 779 QRKNBRNB
780 QBRKNRBN 781 QRKBNRBN 782 QRKNRBBN 783 QRKNRNBB 784 BBRQKNRN 785 BRQBKNRN 786 BRQKNBRN 787 BRQKNRNB 788 RBBQKNRN 789 RQBBKNRN
790 RQBKNBRN 791 RQBKNRNB 792 RBQKBNRN 793 RQKBBNRN 794 RQKNBBRN 795 RQKNBRNB 796 RBQKNRBN 797 RQKBNRBN 798 RQKNRBBN 799 RQKNRNBB
800 BBRKQNRN 801 BRKBQNRN 802 BRKQNBRN 803 BRKQNRNB 804 RBBKQNRN 805 RKBBQNRN 806 RKBQNBRN 807 RKBQNRNB 808 RBKQBNRN 809 RKQBBNRN
810 RKQNBBRN 811 RKQNBRNB 812 RBKQNRBN 813 RKQBNRBN 814 RKQNRBBN 815 RKQNRNBB 816 BBRKNQRN 817 BRKBNQRN 818 BRKNQBRN 819 BRKNQRNB
820 RBBKNQRN 821 RKBBNQRN 822 RKBNQBRN 823 RKBNQRNB 824 RBKNBQRN 825 RKNBBQRN 826 RKNQBBRN 827 RKNQBRNB 828 RBKNQRBN 829 RKNBQRBN
830 RKNQRBBN 831 RKNQRNBB 832 BBRKNRQN 833 BRKBNRQN 834 BRKNRBQN 835 BRKNRQNB 836 RBBKNRQN 837 RKBBNRQN 838 RKBNRBQN 839 RKBNRQNB
840 RBKNBRQN 841 RKNBBRQN 842 RKNRBBQN 843 RKNRBQNB 844 RBKNRQBN 845 RKNBRQBN 846 RKNRQBBN 847 RKNRQNBB 848 BBRKNRNQ 849 BRKBNRNQ
850 BRKNRBNQ 851 BRKNRNQB 852 RBBKNRNQ 853 RKBBNRNQ 854 RKBNRBNQ 855 RKBNRNQB 856 RBKNBRNQ 857 RKNBBRNQ 858 RKNRBBNQ 859 RKNRBNQB
860 RBKNRNBQ 861 RKNBRNBQ 862 RKNRNBBQ 863 RKNRNQBB 864 BBQRKRNN 865 BQRBKRNN 866 BQRKRBNN 867 BQRKRNNB 868 QBBRKRNN 869 QRBBKRNN
870 QRBKRBNN 871 QRBKRNNB 872 QBRKBRNN 873 QRKBBRNN 874 QRKRBBNN 875 QRKRBNNB 876 QBRKRNBN 877 QRKBRNBN 878 QRKRNBBN 879 QRKRNNBB
880 BBRQKRNN 881 BRQBKRNN 882 BRQKRBNN 883 BRQKRNNB 884 RBBQKRNN 885 RQBBKRNN 886 RQBKRBNN 887 RQBKRNNB 888 RBQKBRNN 889 RQKBBRNN
890 RQKRBBNN 891 RQKRBNNB 892 RBQKRNBN 893 RQKBRNBN 894 RQKRNBBN 895 RQKRNNBB 896 BBRKQRNN 897 BRKBQRNN 898 BRKQRBNN 899 BRKQRNNB
900 RBBKQRNN 901 RKBBQRNN 902 RKBQRBNN 903 RKBQRNNB 904 RBKQBRNN 905 RKQBBRNN 906 RKQRBBNN 907 RKQRBNNB 908 RBKQRNBN 909 RKQBRNBN
910 RKQRNBBN 911 RKQRNNBB 912 BBRKRQNN 913 BRKBRQNN 914 BRKRQBNN 915 BRKRQNNB 916 RBBKRQNN 917 RKBBRQNN 918 RKBRQBNN 919 RKBRQNNB
920 RBKRBQNN 921 RKRBBQNN 922 RKRQBBNN 923 RKRQBNNB 924 RBKRQNBN 925 RKRBQNBN 926 RKRQNBBN 927 RKRQNNBB 928 BBRKRNQN 929 BRKBRNQN
930 BRKRNBQN 931 BRKRNQNB 932 RBBKRNQN 933 RKBBRNQN 934 RKBRNBQN 935 RKBRNQNB 936 RBKRBNQN 937 RKRBBNQN 938 RKRNBBQN 939 RKRNBQNB
940 RBKRNQBN 941 RKRBNQBN 942 RKRNQBBN 943 RKRNQNBB 944 BBRKRNNQ 945 BRKBRNNQ 946 BRKRNBNQ 947 BRKRNNQB 948 RBBKRNNQ 949 RKBBRNNQ
950 RKBRNBNQ 951 RKBRNNQB 952 RBKRBNNQ 953 RKRBBNNQ 954 RKRNBBNQ 955 RKRNBNQB 956 RBKRNNBQ 957 RKRBNNBQ 958 RKRNNBBQ 959 RKRNNQBB

```

Valid arrangements are such that the two B's cannot both be on odd or even positions and the K must be somewhere between the two R's.

As a test of your program or function you may get the checksum 38292381401040 from summing all `(960+N) * SP(N)` where N B R Q K is transliterated into 1 2 3 4 5 accordingly to get a decimal number from all N's 0-959. The start and end of this summation looks like:

```
( 960 + 0 ) * 22411353 +
( 960 + 1 ) * 24121353 +
( 960 + 2 ) * 24113253 +
.
.
.
( 960 + 958 ) * 35311224 +
( 960 + 959 ) * 35311422 = 38292381401040

```

This is Code Golf, shortest code wins.

# Answer
> 7 votes
# JavaScript (ES6), 117 bytes

This is based on the *direct derivation* method described on Wikipedia.

```
f=(n,i=Q=R=N=-1)=>i<7?"RKRNQB"[i++^n%4*2&&i^n/2&6?n/16%6^++Q?~-(j=n/96)%3+1+j%9/7^N++&&~-j*3/8^N?++R:3:4:5]+f(n,i):""

```

Try it online!

Or verify all results, using a cleaner implementation.

### Knight positions

Given \\$j=\lfloor n/96\rfloor \in\[0\dots9\]\\$, the 0-indexed position of the first knight is:

$$\[0,0,0,0,1,1,1,2,2,3\]\[j\]$$

which can be obtained with:

```
~-j * 3 / 8 | 0

```

The 0-indexed position of the second knight is:

$$\[1,2,3,4,2,3,4,3,4,4\]\[j\]$$

which can be obtained with:

```
~-j % 3 + 2 + j % 9 / 7 | 0

```

---

# C (gcc), 127 bytes

```
f(n,i,j,Q,R,N){for(i=Q=N=-1,R=75;i<7;)putchar(i++^n%4*2&&i^n/2&6?n/16%6^++Q?(j=n/96-1)%3+1+j%8/6^N++&&j*3/8^N?R^=25:78:81:66);}

```

Try it online!

# Answer
> 3 votes
# Charcoal, ~~80~~ ~~76~~ 74 bytes

```
Ｎθ≔ＫＤ⁸→ηＦ²«§≔η⁻⊕⊗θιB≧÷⁴θ»§≔η§⌕ＡηωθQＦ⪪”)“∧⊟bＢ%⁰”χ§≔η§⌕ＡηωＩ§ι÷θ⁶NＦRKR§≔η⌕ηωι

```

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

```
Ｎθ

```

Input the position number.

```
≔ＫＤ⁸→η

```

Get a reference to `8` cells of the canvas. (Charcoal allows you to modify the canvas by writing to elements of this list, thus implicitly printing the result.)

```
Ｆ²«§≔η⁻⊕⊗θιB≧÷⁴θ»

```

Place the bishops. This also divides the input by `16`.

```
§≔η§⌕ＡηωθQ

```

Place the queen.

```
Ｆ⪪”...”χ§≔η§⌕ＡηωＩ§ι÷θ⁶N

```

Place the knights by looking up their positions in a compressed string, as that was sadly golfier than computing the position directly. (Note that the second half of the look-up table has to take into account that the first knight has already been placed, so there are only four empty cells remaining.)

```
ＦRKR§≔η⌕ηωι

```

Place the remaining pieces.

# Answer
> 3 votes
# APL+WIN, 146 bytes.

Prompts for layout number. Uses compressed versions of the bishops' and knights' tables.

```
p←~i←(8⍴2)⊤+⎕av⍳'?è{~⍨ÏÛ|+⊤≤÷+⍉○∪'[16|n←0]⋄p[(i/⍳8)[6|n←⌊n÷16]]←2⋄p[((5⍴2)⊤¯32+⎕av⍳'8421,*)&%#'[⌊n÷6])/(p=0)/⍳8]←3⋄p[((p=0)/⍳8)[0 2]]←4⋄'KBQNR'[p]

```

Unfortunately Dyalog Classic's ⎕av does not match that of my APL so no TIO available. Test cases:

```
n=0 BBQNNRKR, n=518 RNBQKBNR, n=959 RKRNNQBB

```

# Answer
> 1 votes
# Funge-98, 182 bytes

```
"RBUS"4(&:4%2*1+'B\9p4/:4%2*'B\9p4/:6%'Qa13C1g::7/'Na13C$:7%'Na13C00'Ra13C01'Ra13C00'Ka13C>::8-6j5+1,g9_@
'>1p"HTRF"4($ffp>3k0p>:9g' -3j7+1_O00g-9j7p00+1g00_'Q\9p$6/ffg1)1R

```

Try it online!

Uses the direct derivation method from Wikipedia.

Contains non-printable characters, so here is a hex dump:

```
00000000: 2252 4255 5322 3428 263a 3425 322a 312b  "RBUS"4(&:4%2*1+
00000010: 2742 5c39 7034 2f3a 3425 322a 2742 5c39  'B\9p4/:4%2*'B\9
00000020: 7034 2f3a 3625 2751 6131 3343 3167 3a3a  p4/:6%'Qa13C1g::
00000030: 372f 274e 6131 3343 243a 3725 274e 6131  7/'Na13C$:7%'Na1
00000040: 3343 3030 2752 6131 3343 3031 2752 6131  3C00'Ra13C01'Ra1
00000050: 3343 3030 274b 6131 3343 3e3a 3a38 2d36  3C00'Ka13C>::8-6
00000060: 6a35 2b31 2c67 395f 400a 070e 151c 0f16  j5+1,g9_@.......
00000070: 1d17 1e1f 273e 3170 2248 5452 4622 3428  ....'>1p"HTRF"4(
00000080: 2466 6670 3e33 6b30 703e 3a39 6727 202d  $ffp>3k0p>:9g' -
00000090: 336a 372b 315f 4f30 3067 2d39 6a37 7030  3j7+1_O00g-9j7p0
000000a0: 302b 3167 3030 5f27 515c 3970 2436 2f66  0+1g00_'Q\9p$6/f
000000b0: 6667 3129 3152                           fg1)1R

```

# Answer
> 1 votes
# Perl 5 `-Minteger -n`, ~~144~~ 152 bytes

```
@r[$_%4*2+1,$_/4%4*2]=(B,B);map{$q=$_;$_||=!$q--&&substr QNNRKR,$i++,1for@r[0..7]}$_/16%6,(($t=$_/96)>3)+($t>6)+($t>8),(0..3,1..3,2,3,3)[$t],0,0,0;say@r

```

Try it online!

---
Tags: code-golf, kolmogorov-complexity, chess
---


title: Print the Christmas alphabet

text:
# Question
Title: Print the Christmas alphabet
Write a program to print or return one of these strings verbatim:

```
abcdefghijkmnopqrstuvwxyz

ABCDEFGHIJKMNOPQRSTUVWXYZ

```

Notice that there is no L.

# Answer
# Charcoal, 3 bytes

```
⁻βl

```

Try it online! Link is to verbose version of code. Explanation: `β` is a Charcoal variable predefined to the lowercase alphabet.

```
⁻αL

```

Try it online! Link is to verbose version of code. Explanation: `α` is a Charcoal variable predefined to the uppercase alphabet.
> 9 votes

# Answer
# brainfuck, 49 bytes

```
+++++++++++++[->+>+++++>+<<<]>--[->.+<]>>+[-<+.>]

```

Try it online!

Starting with a cell containing `13` we set up 3 cells containing `13,65,13`. The 65 is the ASCII code of the letter to be printed and the other cells are down-counters (which are adjusted to 11 and 14 before use.) A nice feature is that the loop for printing the first 11 letters increments the ASCII code after printing and the loop printing the last 14 letters increments the ASCII code before printing, thereby skipping L naturally.
> 12 votes

# Answer
# Octave / MATLAB, ~~21~~ 18 bytes

Anonymous function that takes no inputs and outputs the string with uppercase letters.

```
@()['A':'K' 77:90]

```

Try it online!

### How it works

The code concatenates the two ranges of letters: before and after the 'L'. The second range is defined numerically to save two bytes. This can be done because concatenating chars and numbers implicitly converts each number to the char with the corresponding codepoint.
> 10 votes

# Answer
# , 22 bytes

```
!Map(cat,letters[-12])

```

Try it online!

Exits with an error; credit to ovs.

# , 23 bytes

```
x=Map(cat,letters[-12])

```

Try it online!

Also credit to ovs.

# , 24 bytes

```
cat(letters[-12],sep="")

```

Try it online!
> 8 votes

# Answer
# Bash, 23 bytes, original answer

```
printf %s {a..k} {m..z}

```

Try it online!

# Bash, 21 bytes, thanks to Nahuel Fouilleul

```
echo {a..z}|tr -d \ l

```

Try it online!
> 7 votes

# Answer
# Python, 39 bytes

```
print(25*"%c"%(*{*range(65,91)}-{76},))

```

Attempt This Online!

Only 5 bytes longer than hardcoding the entire thing ...

### How?

Unlike dictionaries, sets are in general not guaranteed to preserve insertion order, so why does this work?

C-Python implementation detail: (small) integers are their own hash values.
> 7 votes

# Answer
# Various Languages, 25 bytes

```
abcdefghijkmnopqrstuvwxyz

```

or

```
ABCDEFGHIJKMNOPQRSTUVWXYZ

```

In many languages, the shortest solution is also the least interesting.

Works in: ///, ARBLE, PHP, ReRegex, ...
> 6 votes

# Answer
# PowerShell Core, 25 bytes

```
-join$('a'..'k';'m'..'z')

```

Try it online!

I think solving that with 25 bytes has a certain *je ne sais quoi*.
Only works in PowerShell Core, Windows PowerShell can't enumerate characters.

# PowerShell, all flavors, 27 bytes

```
'abcdefghijkmnopqrstuvwxyz'

```

Try it online!

The boring/trivial solution with 27, which would end up as National Fruitcake Day. Somewhat related to CodeGolf, as golfed code and fruitcake have a similar density.

# PowerShell, all flavors, 31 bytes

```
-join([char[]]$(65..75;77..90))

```

Try it online!

So here's New Year's Eve.
Which is what we also get from the trivial solution in Batch:

# Windows Batch, 31 bytes

```
@echo abcdefghijkmnopqrstuvwxyz

```
> 6 votes

# Answer
# Vyxal 3,, 4 bytes

```
n'l-

```

Try it Online!

First Vyxal answer, so I’m sure there may be a more concise way, but I like that it almost reads ‘no el’!
> 6 votes

# Answer
# Retina, 14 bytes

```

26*#
Y`#`l
l

```

Try it online! Outputs in lowercase but if the program is uppercased then it will output in uppercase instead. Explanation:

```

26*#

```

Insert 26 `#`s. (I can't use the default `_` because that's a special transliteration character.)

```
Y`#`l

```

Translate each `#` in turn into the next character of the alphabet. (It's possible to skip the `l` here but it's longer overall.)

```
l

```

Remove the `l`.

The best I could do in Retina 0.8.2 without resorting to 26-byte hard-coding was 27 bytes, but with some inspiration from @FryAmTheEggman I got it down to ~~16~~ 15 bytes:

```
T`l`_l
}`$
z
l

```

Try it online! Outputs in lowercase but if the program is uppercased then it will output in uppercase instead. Explanation:

```
T`l`_l

```

Move all of the letters so far one step backwards in the alphabet, but delete any `a`.

```
$
z

```

Append a `z` to the current string.

```
}`

```

Repeat the above until the transformation becomes idempotent. This happens when the string is the entire alphabet; the `a` is deleted, the `b-z` get transliterated into `a-y`, and another `z` is appended.

```
l

```

Remove the `l`.
> 6 votes

# Answer
# x86-64 machine code, ~~17~~ 15 bytes

```
B0 E6 04 5B AA 2C 4B 3C 01 14 F1 75 F5 AA C3

```

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI a memory address at which to place the result, as a null-terminated byte string.

In assembly:

```
f:  mov al, -26     # Set AL to -26.
r:  add al, 'Z'+1   # Add this value to AL to produce a letter.
    stosb           # Write AL to the output string, advancing the pointer.
    sub al, 'K'     # Subtract 'K' from AL.
    cmp al, 1       # Set flags from calculating AL - 1. CF is 1 iff AL is 0.
    adc al, 'K'-'Z' # Add (this value + CF) to AL. The net change is +1 or +2.
    jnz r           # If the result is nonzero, repeat the loop.
    stosb           # Write AL (0) to the output string, advancing the pointer.
    ret             # Return.

```
> 6 votes

# Answer
# CP-1610 machine code, 14 DECLEs1 = 17.5 bytes

*1. CP-1610 instructions are encoded with 10-bit values (0x000 to 0x3FF), known as DECLEs. Although the Intellivision is also able to work on 16-bit data, programs were really stored in 10-bit ROM back then.*

A routine that writes the Christmas alphabet at **R4**.

The method used here would be more relevant if there were several letters to skip. With just one letter, using `SLLC` / `ADCR` only saves a few CPU cycles compared to `CMPI` / `BEQ`.

```
                            ROMW  10          ; use 10-bit ROM
                            ORG   $4800       ; map our code at $4800

4800  02BC 0200             MVII  #$200,  R4  ; R4 = BACKTAB pointer
4802  0004 0148 000D        CALL  alpha       ; invoke our routine

4805  02BD 0030             MVII  #$30,   R5  ; R5 = pointer into STIC registers
4807  01C0                  CLRR  R0
4808  0268                  MVO@  R0,     R5  ; no horizontal delay
4809  0268                  MVO@  R0,     R5  ; no vertical delay
480A  0268                  MVO@  R0,     R5  ; no border extension
480B  0002                  EIS               ; enable interrupts

480C  0017                  DECR  R7          ; loop forever

                            ;; our routine
                      alpha PROC

480D  02B8 010F             MVII  #$010F, R0  ; R0 = letter code, initialized to 'A'
480F  02B9 0010             MVII  #$0010, R1  ; R1 = mask used to skip 'L'

4811  0059            @loop SLLC  R1          ; left-shift the mask into the carry
4812  002F                  ADCR  R7          ; add the carry to the program counter
                                              ; (i.e. skip MVO@ if the carry is set)
4813  0260                  MVO@  R0,     R4  ; write the letter
4814  02F8 0008             ADDI  #8,     R0  ; update R0 to the next letter
4816  0378 01D7             CMPI  #$01D7, R0  ; compare with 'Z'
4818  0226 0008             BLE   @loop       ; loop if less than or equal

481A  00AF                  MOVR  R5,     R7  ; return

                            ENDP

```

## Output
> 6 votes

# Answer
# RProgN 2, 4 bytes

```
a`l-

```

Try it online!

Or for upper-case

```
A`L-

```

Try it online!

Just removes literal `L` from the alphabet builtin.
> 5 votes

# Answer
# V (vim), 14 bytes

```
:h<_
jjYZZPtmx

```

Try it online!

Yet another post uses the trick found by Lynn.
> 5 votes

# Answer
# Python 3, 71 bytes

```
import string as s
print(s.ascii_lowercase[:11]+s.ascii_lowercase[12:])

```

Try it online!
> 5 votes

# Answer
# Perl 5, 13 bytes

```
say A..K,M..Z

```

Try it online!
> 5 votes

# Answer
# Python 3, 51 50 bytes

```
print(''.join(chr(i+65)for i in range(26)if i-11))

```

Try it online!

Simple iteration combining `range` and `chr`.

-1 byte thank to @UndoneStudios's amazing hint of using `str.join`

---

# Python 3, 54 bytes

```
a=*range(65,91),
print(*map(chr,a[:11]+a[12:]),sep='')

```

Try it online!

Using tuple destructuring.
> 5 votes

# Answer
# Ruby, 19 bytes

```
([*?a..?z]-[?l])*''

```

Attempt This Online!

Since the task allows simply returning the string instead of printing, I believe it should be acceptable as this snippet. If not, it's +4 for `$><<` in front.
> 4 votes

# Answer
# morsecco: 112 bytes

Yes, that's 4.5 times the size of the pure input, but don't forget that we have only three symbols, resulting in a factor ~5, compared to the full character set.

The whole christmas alphabet in morse code already takes 102 characters; with the overhead of conversion and printing it would be 124 bytes, so this loop solution is shorter *(not counting the christmas tree formatting, which starts beautifully with `E`ntering 65, but doesn't really work out well, because whitespaces are sometimes meaningful in Morsecco)*:

```
   .
-.....-
 -- -
  - -
 -.- -
  . -
  .--
. .-..-.--
   .-
--.. --.-
 . .----
.- --.. --.
 . -.--.--
.- --. --.-
. -..--.- --.

```
* The first line initializes the stack with 65 (`A`)
* Line 2 sets a jump `M`ark to start a loop, `K`onvert the number to `T`ext and `W`rite it to stdout.
* Now comes the golfing trick: line 3 `A`dds –75 (`K`) and skips after `--.-` is zero (so the last line handles the `L` skipping by `E`ntering 77)
* The 4th line subtracts another 15 to test for the `Z` and skips after the `--.` that closes the loop for the other characters
* The 5th line `A`dds 91 (–75 + –15 + 91 = 1, so the value is incremented) and `G`oes to the mark that was set. This saves three bytes compared to saving a copy of the value, dropping the modified version and adding 1 to the copy.

**Cheating version: 47 bytes**

Yes, morsecco ignores all characters except for dot, dash and space, but to cheat, you can `R`ead the source code from the empty address, `C`ut it after the `Z` and `O`utput. Yes, such tricks are probably legal when golfing, but boring:

```
ABCDEFGHIJKMNOPQRSTUVWXYZ.   .-. -.-. --..- ---

```
> 4 votes

# Answer
# XPath 2: ~~42~~ ~~41~~ 27 bytes

First attempt: 42 bytes

```
codepoints-to-string(remove(97 to 122,12))

```

Explanation:

* `97 to 122` returns the sequence 97, 98, 99, ... 122
* `remove( ..., 12)` removes the 12th item
* `codepoints-to-string()` converts the numbers to a string

The result of the evaluation is the christmas string. Try it online.

---

Second attempt 41 bytes, thanks to @Philippos

```
codepoints-to-string(remove(65 to 90,12))

```

(upper case letters)

---

Third attempt (27 bytes), the "trivial" (but so far the shortest) solution (@jonathan-allan)

```
"abcdefghijkmnopqrstuvwxyz"

```

is a equally valid XPath expression.
> 4 votes

# Answer
# Haskell, 23 bytes

```
filter(/='L')['A'..'Z']

```

## Explanation

`['A'..'Z']` represents the entire uppercase alphabet, `(/='L')` is a shorthand for the lambda `\x -> x /= 'L'`. When both are passed to `filter`, this keeps all letters from the alphabet that are not `L`.

Alternatively, this can also be done with an extra byte using list comprehensions:

```
[x|x<-['A'..'Z'],x/='L']

```
> 4 votes

# Answer
# Python 3, 50 49 bytes

```
print(''.join(chr(x+x//76)for x in range(65,90)))

```

You could save two bytes by using Python 2, but nobody uses that anymore:

# Python 2, 47 bytes

```
print ''.join(chr(x+x/76)for x in range(65,90))

```
> 4 votes

# Answer
# Google Sheets, 45 bytes

`=sort(replace(join(,char(row(65:90))),12,1,))`

...or **47 bytes**:

`=join(,filter(char(row(65:90)),row(65:90)<>76))`

These formulas are longer than their output.
> 3 votes

# Answer
# Uiua, 11 bytes

```
▽≠@l.+@a⇡26

```

Try it!

```
▽≠@l.+@a⇡26
        ⇡26  # range from 0 to 25
     +@a     # add letter a
    .        # duplicate
 ≠@l         # where is it not equal to l?
▽            # keep

```
> 3 votes

# Answer
# APL (dzaima/APL), 6 bytes

```
⎕A~'L'

```

Try it online!
> 3 votes

# Answer
# sclin, 9 bytes

```
$abc"l"--

```

Try it on scline!

Very straightforward.
> 3 votes

# Answer
# Scala, ~~48~~ 30 bytes

Saved 18 bytes thanks to @corvus\_192

---

Golfed version. Try it online!

```
'A'to'Z'patch(11,"",1)mkString

```
> 3 votes

# Answer
# *Acc!!*, 41 bytes

```
Count i while 25-i {
Write (65+i)*77/76
}

```

Try it online!

### Explanation

Loop `i` from 0 to 24. Add 65 to convert to uppercase codepoints A through Y; then scale by a linear factor to skip over code point 76 (L). (Division in *Acc!!* is integer division, but the floating-point version would be \\$ 65.86, 66.87, \cdots, 74.97, 75.99, 77.00, 78.01, \cdots, 90.17 \\$)
> 2 votes

# Answer
# C (GCC), ~~48~~ ~~46~~ 42 bytes w/ help from @Neil and @ceilingcat

```
main(x){for(;x++<27;)x-13&&putchar(x+63);}

```

Attempt this online
> 2 votes

# Answer
# Kotlin, 40 35 bytes

```
{(('a'..'z')-'l').joinToString("")}

```

Try it online!

# Kotlin, 40 35 31 bytes

```
{(('a'..'z')-'l').map(::print)}

```

Try it online!

Lambdas `() -> String` and `() -> Unit`, respectively, that uses `minus` applied to a CharRange.

-5 bytes thank to @corvus\_192's wonderful reminder that `minus` can be replaced by `-`

-4 bytes on `() -> Unit` by replacing the `forEach` with a `map`.
> 2 votes

# Answer
# jq, 27 characters

```
[range(65;91)]-[76]|implode

```

Sample run:

```
bash-5.2$ jq -nr '[range(65;91)]-[76]|implode'
ABCDEFGHIJKMNOPQRSTUVWXYZ

```

Try it online!
> 2 votes

# Answer
# Japt, 4 bytes

```
;BkÊ

```

Test it here
> 2 votes

# Answer
# GolfScript, 10 bytes

```
91,65>'L'-

```

Try it online!

# GolfScript, 11 bytes

```
'alm{'{,^}/

```

Try it online!
> 2 votes

# Answer
# Pip, ~~6~~ 5 bytes

*-1 byte thanks to @DLosc*

```
zRM'l

```

~~Yea I think that's about as short as it can get, unless someone has some genius strategy.~~

Turns out I'm not genius enough :P

Try It Online!
> 2 votes

# Answer
# Knight (v2), 27 bytes

```
;=a 64W>91=a+1a|?76aO+Aa'\'

```

Try it online!

Well, at least it's shorter than this (28 bytes):

```
O"ABCDEFGHIJKMNOPQRSTUVWXYZ"

```
> 2 votes

# Answer
# Python, 65 bytes:

Uses the formula given by Stef in the comments, so I guess this could probably stay up for not being boring...

```
lambda:''.join(chr(int(i+((1.5*i)**.5)//1))for i in range(56,81))

```
> 2 votes

# Answer
# YASEPL, 26 bytes

algorithmic way of doing it

```
=a$97`1}3,108,2›`2+}2,123

```

# YASEPL, 28 bytes

lazy way of doing it

```
>"abcdefghijkmnopqrstuvwxyz"

```
> 2 votes

# Answer
# 05AB1E, ~~5~~ 4 bytes

-1 thanks to noodle man

```
A'lм

```

Try it online!

```
A'lм
A       The lowercase alphabet
 'lм    Minus the 'l'

```
> 2 votes

# Answer
# Pyth, 9 bytes

```
+<G11>G12

```

Try it online!

#### Explanation

**G** \- `Pyth`'s pre-initialised variable to the lowercase letters in the alphabet.

**\<** \- used to slice the string till 11.

**\>** \- used to slice the string from 12, thereby leaving the letter `l`.

# Pyth, 4 bytes | Thanks to @FryAmTheEggman

```
-G"l

```

Try it online!
> 1 votes

# Answer
# Commodore BASIC - 28 characters

Enter this in direct mode, the `?` denotes a `PRINT` command:

```
?"ABCDEFGHIJKMNOPQRSTUVWXYZ

```

Note that in Commodore BASIC, you do not necessarily require a closing quotation mark when printing a string literal.
> 1 votes

# Answer
# Deadfish~, 27 bytes

```
iisiiiis{ic}ici{ic}icicicic

```

Try it online!

My first Deadfish~ answer, I don't know if it could be improved.

**How does it work?**

```
ii                             accumulator: 2
  s                            accumulator: 4
   iiii                        accumulator: 8
       s                       accumulator: 64
        {ic}                   accumatator: [65, 75] ('A' -> 'J')
            ic                 accumulator: 76 ('K')
              i                accumulator: 77
               {ic}            accumulator: [78, 88] ('M' -> 'V')
                   icicicic    accumulator: [89, 92] ('X' -> 'Z')

```
> 1 votes

# Answer
# Python 3, ~~47~~ 46 bytes

```
for i in range(65,90):print(end=chr(i+(i>75)))

```

Try it online!

Yet another boring loop in Python 3 :)
> 1 votes

# Answer
# Nibbles, 17 nibbles (8.5 bytes)

```
-.,26ch+64$"L"

```

Attempt This Online!

## Explanation

```
  ,26          # for i in range [1-26]
       +64$    #  add 64 to i
     ch        #  convert i to char
-.         "L" # concat all and remove "L"
               # (implicitly print entire string)

```

Alternately, `-.,26ch+96$"l"` works for lowercase and has the same score.
> 1 votes

# Answer
# Lua, 139 bytes

```
p,c,l=io.write,string.char,{}for i=65,90 do if i~=76 then l[#l+1]=i end end for j=1,#l do p(c(l[j]))end p"\n"for k=1,#l do p(c(l[k]+32))end

```

Try it online!

## Explanation

Lua allows us to reference functions, similar to pointers in C and C++. This means we can rename the `io.write` and `string.char` functions to `p` and `c`. On the same line, we also create an empty table.

```
p, c, l = io.write, string.char, {}

```

Next we need to populate our `l` table with the ASCII codes of `A` through `Z`, making sure to skip ASCII code 76 that represents an L.

To insert an element into a Lua table, we could use `table.insert`, but that would be quite long. So we get the length of `l` using `#l`, add 1 to the length, and place the ASCII code into the new index.

```
for i=65,90 do
    if i ~= 76 then
        l[#l + 1] = i
    end
end

```

Once that's done, we then start looping over the table to print the characters. For this explanation, I'll expand `p` and `c` to show the function names.

The first loop will print the upper-case letters. Then we add a new line. Then we start another loop to print out the lower-case letters, that is +32 to our `l` ASCII table.

```
for j=1,#l do
    io.write(string.char(l[j]))
end
io.write"\n"
for k=1,#l do
    io.write(string.char(l[k] + 32))
end

```
> 0 votes

# Answer
# MathGolf, 4 bytes

```
▄'l-

```

Try it online.

**Explanation:**

```
▄     # Push the lowercase alphabet
 'l- '# Remove the "l"
      # (after which the entire stack is output implicitly as result)

```
> 0 votes

---
Tags: code-golf, kolmogorov-complexity
---


title: Write a Unicode Unpacker

text:
# Question
Title: Write a Unicode Unpacker
We all know scoring by characters is ripe for abuse. Let's prove this.

## The Challenge

Output the following:

```
"m8V}G=D@7G1lAI,v08`
#(hNb0A8T!g;==SVaG5~
g"jUF!bmRY(3I@na?2S{
fJVzo/GQYU%ybpfUq3aG
Yza[jc,WJ$bP^7r};Da}
V-!Z+Nk:`/poc}d/X:G\
sWX{dbAUv6,i]%RG$hRp
),bd+?/{U1tU[;<;u.Nk
ZFPIOzJ/HimL!nexc,ls
HM%k3D$n2|R,8L?'eI_n
qs.kzbil$UQy_#Z)l#i%
*G4gr2<R^y#/iQ9,<+p%
}BYnb3_&:!m;#~QL1C?t
f.U>wIkTz=P>bf!uwb!d
z()rG)>~:q4#\}~pQEvZ
=OT8T4<i50%/8%eX^"3E
#Ks(8}OzZ&]RQ(-BLy<7
p7B~GEsF$)>?a"dtGCC'
H;:n&p&Z($ukNd,Et.$O
F*Uq0$dm,m%ejPT~u,qL

```

The characters inside the box are selected uniformly at random between the codepoints `0x21` and `0x7e`. This means they should not be compressable much ordinarily. However, in this challenge, your code is scored by the number of *characters*, which can range in codepoint between `0x00` and `0x10ffff`. Since this gives about 21 bits of information per character, and the box contains about 7 bits of information per character, approaching a 3:1 packing should be possible.

Note that:

* As usual, you may output to STDOUT or return the string from a function;
* A trailing newline on the last line is optional. Apart from this, the output must be exactly as given.
* List representations are acceptable if returning from a function.

## Scoring

The score is the length of your code in *characters*, expressed in the encoding of your language's interpreter. For most languages, this will be UTF-8. Note that languages that use an SBCS, such as most golfing languages, are at a huge disadvantage here as the number of characters is equal to the number of bytes.

Note that characters are not the same as grapheme clusters. For example,  is actually 3 characters (`0x1f468 0x200d 0x1f9b2`).

# Answer
> 11 votes
# Python 3, ~~223~~ ~~221~~ ~~216~~ ~~214~~ 212 chars

```
ጸ=0
for  in"ጸꛯ裶":ጸ=ጸ<<20|ord()
while ጸ:print(end=chr(ጸ%128));ጸ>>=7

```

Converts from base 2²⁰ (1048576) to base 128. Just for fun, I used some of the letters from the encoded string as variable names.

---

**Edit 1**: (221) I saved two characters by using base 128 rather than base 127, allowing me to use the bit-shift operation `>>=7` rather than the integer division `//=127`.

**Edit 2**: (216) Thanks to ovs in the comments for suggesting a way to save a whopping five characters.

**Edit 3**: (214) Using base 2²⁰ allows us to save another two characters, because we can write the constant as `2**20` in the code, which only takes five characters to write rather than seven.

**Edit 4**: (212) We can write `ጸ<<20|ord()` instead of `ጸ*2**20+ord()`.

# Answer
> 6 votes
# Charcoal, 150 chars

```
⍘↨Ｅ¶涿燁⪆΅㓬萾℅ι×φφ⁺γ¶

```

Try it online! No verbose version (but see encoder below); the succinct code that the deverbosifier generates doesn't work and is suboptimal anyway. Note that Charcoal actually defaults to Unicode, so this is the only case where I don't really mean `charcoal -e` (to use SBCS encoding). Explanation:

```
   ...          Long string (including leading newline)
  Ｅ             Map over characters
       ι        Current character
      ℅         Take the ordinal
         φφ     Predefined variable 1,000
        ×       Multiply (i.e. 1,000,000)
 ↨              Decode array using that base
            γ   Printable ASCII
           ⁺    Concatenated with
             ¶  Literal newline
⍘               Encode integer using custom base
                Implicitly print

```

The encoded string was generated using the following program:

```
ＷＳ⊞υιＵＴ⭆↨⍘⪫υ¶⁺γ¶×φφ℅ι

```

Try it online! Link is to verbose version of code. I was somewhat lucky that the encoded string uses no Charcoal characters (which would need quoting) and contains no unprintables (not counting newline as an unprintable).

# Answer
> 5 votes
# JavaScript (ES6), ~~259~~ ~~253~~ 250 chars

```
f=
_=>"៘⋷㆐⇸຺⋄⡊ᙫᷢᰃἙ선ᴱꦁ蘀ȤȫὋ㵽᤽熀ᗜ嶼෎ࢥ魱ʼᅵ鍅Ⴅ".replace(/./gu,c=>String.fromCharCode((n=c.codePointAt())%95+32,n/95%95+32,n/9025+32)).replace(/ /g,"\n")
;document.write(`<xmp>${f()}</xmp>`);
```

Explanation: Simply encodes three ASCII characters using one Unicode character. Edit: Saved 4 bytes thanks to @Arnauld. Saved 2 bytes by dividing by 9025 instead of by 95 twice. Saved 3 bytes by subtracting 32 in the encoder:

```
f=s=>s.replace(/\n/g, " ").replace(/.../g, c=>String.fromCodePoint(c.charCodeAt()-32+(c[1].charCodeAt()-32)*95+(c[2].charCodeAt()-32)*95*95));
o.textContent=f(i.value);
```

```
<textarea rows=20 cols=20 id=i oninput=o.textContent=f(this.value);>
"m8V}G=D@7G1lAI,v08`
#(hNb0A8T!g;==SVaG5~
g"jUF!bmRY(3I@na?2S{
fJVzo/GQYU%ybpfUq3aG
Yza[jc,WJ$bP^7r};Da}
V-!Z+Nk:`/poc}d/X:G\
sWX{dbAUv6,i]%RG$hRp
),bd+?/{U1tU[;<;u.Nk
ZFPIOzJ/HimL!nexc,ls
HM%k3D$n2|R,8L?'eI_n
qs.kzbil$UQy_#Z)l#i%
*G4gr2<R^y#/iQ9,<+p%
}BYnb3_&:!m;#~QL1C?t
f.U>wIkTz=P>bf!uwb!d
z()rG)>~:q4#\}~pQEvZ
=OT8T4<i50%/8%eX^"3E
#Ks(8}OzZ&]RQ(-BLy<7
p7B~GEsF$)>?a"dtGCC'
H;:n&p&Z($ukNd,Et.$O
F*Uq0$dm,m%ejPT~u,qL
</textarea><pre id=o></pre>
```

# Answer
> 5 votes
# 05AB1E, 152 chars

Converts between base 94 and base 1125000. The highest byte in the program is `0x10cd84`.

```
"읋쵩䉸蠠殓綳"Ç•HтÄ•β94в33+ç20ôJ»

```

Try it online! For some reason I can't create a working TIO link for this, you will have to paste the code manually.

**Commented**:

```
"..."                    # long string
        Ç                   # convert to code points
         •HтÄ•β             # convert from base 1125000
               94в          # convert to base 94
                  33+       # add 33 to each digit
                     ç      # convert from code points
                      20ô   # split into groups of 20
                         J  # join each group into a string
                          » # join the groups by newlines

```

The *long string* is generated with this program. Note that this won't work for every base since some numbers like `0xdc00` can't be converted to a character.

# Answer
> 2 votes
# Julia 1.0, <sup>~~210~~</sup> 200 bytes

```
_->0:19 .|>i->join(Int(c)÷95^i%95+' ' for i=0:2,c="膛㚥愹鳑선謈츀㰖嶼瞑廀")[20i.+(1:20)]

```

Try it online!

output is a list of strings

# Answer
> 1 votes
# Vyxal `j`, 147 chars

```
`"疘暹ᠶ脭ﭮ蹝ੇ㈭첃`Ck4β94τ33+C∑²

```

Try it Online!

At least I think it's 130 chars - I had to count by hand...

# Answer
> 0 votes
# Deadfish~, 4347 bytes

```
{i}{i}{i}iiiic{{i}ddd}iiiiic{{d}iiiii}dddc{i}{i}{i}c{{i}dddddd}dc{{d}iiiii}ddddc{d}c{i}dddcddddc{d}ic{i}iiiiiic{d}{d}ddc{{i}dddd}dc{{d}iiiiii}dddc{i}ddc{d}{d}{d}ic{{i}ddd}iiiic{{d}iii}c{i}ddc{{i}dddddd}c{{d}ii}ddddddc{i}{i}iiiiiciiiiic{{i}dddd}iiiic{d}{d}ddddddc{i}{i}c{{d}iiiii}c{i}{i}dddc{d}ic{i}{i}{i}ddc{{d}iiiii}dc{{i}ddd}c{{d}iiiiii}ddddciicc{i}{i}iiciiic{i}ic{d}{d}ddddddc{d}{d}iic{{i}ddd}iiic{{d}}{d}ddddddc{{i}d}iiic{{d}iii}ic{{i}ddd}iic{d}{d}dc{d}dddddc{{d}iiiiii}iiic{{i}dddd}iiiiic{i}ic{d}{d}{d}iiic{i}dddc{{d}iiiii}ic{i}ic{i}{i}iic{d}ic{{i}dddddd}iiiiiic{d}dddc{d}{d}{d}ddddc{d}dddc{i}{i}{i}iiic{{i}dddddd}c{{d}}{d}dddc{{i}d}iic{d}{d}{d}iic{i}iic{i}{i}{i}iiiiiic{d}dc{{d}iiii}ddddc{i}{i}iiiic{i}c{i}ddcddddc{{d}iiiii}iic{{i}dd}iiiic{d}{d}dddc{i}iiiic{d}c{d}{d}iiic{i}{i}{i}ddc{{d}iiii}ddc{{i}dddddd}iiiiiic{d}{d}ddddddc{{d}iiii}dc{{i}dd}dc{i}{i}{i}iiic{d}{d}dddddcddddddc{i}iiiiic{d}iiic{{d}iiiii}dddddc{{i}dddddd}iiic{d}dddc{{d}iiiiii}iic{{i}dddd}iic{d}{d}iic{i}iiiic{{d}iiiiii}ic{{i}dddd}dc{i}ic{{d}iiii}ddddddc{i}dc{i}{i}{i}dc{i}{i}{i}ddc{{d}}{d}dddddc{{i}ddd}iiiiiic{{d}iiiiii}dc{d}ddc{{i}dddd}dddc{{d}iiiii}iiic{i}{i}{i}iiiiic{i}{i}{i}dc{{d}iiiii}ic{{i}dddddd}ddc{{d}iiiii}ic{{i}dddd}iiiiicdc{d}ddc{i}{i}iiiiiic{d}{d}dddddc{{d}iiiii}dddc{{i}dddddd}ic{d}{d}{d}c{i}iiic{i}{i}ic{{d}ii}ddc{{i}}iiiiic{d}{d}{d}iicic{i}{i}{i}iiiiic{d}{d}dddcddc{d}{d}{d}dddc{i}{i}c{i}{i}{i}iiic{{d}iiii}ddddc{d}c{{i}dddd}ic{d}ddc{{d}iiiii}ddddddc{{i}dddddd}iiiiic{d}dc{d}{d}{d}dddddc{{i}ddd}ddc{d}{d}ddc{i}{i}{i}c{{d}}ddc{i}{i}{i}iciiic{{i}ddddd}iiiiciic{{d}iiii}iiic{i}{i}c{d}ddddddc{{i}ddd}iiiiiic{{d}iiiiii}iic{d}{d}{d}ddddddc{{i}ddd}dddc{d}{d}{d}dciiiiiic{d}{d}{d}ddcicdc{{i}dddd}ddc{{d}iii}dc{i}{i}{i}iic{i}{i}{i}dc{{d}}iiic{{i}dd}c{d}{d}c{i}c{d}iiiciiiiiic{{i}dddddd}iiic{{d}iiiii}iic{d}{d}{d}iiic{i}{i}iiiiic{i}{i}{i}iiiciiiic{d}{d}{d}dddc{{d}iiiiii}dddc{{i}dd}dddc{d}ic{i}{i}dc{d}{d}dc{{d}iiiii}dddddc{{i}dddd}iiiic{i}dddc{{d}}dddddc{{i}dddd}iiciiiiic{{d}iiiiii}c{{i}ddd}c{{d}iiiii}ddddddc{i}{i}dddc{d}{d}{d}ddc{{i}ddd}iiiic{{d}iiii}c{{i}ddd}iiiic{{d}iiiiii}ddc{{d}iiiiii}iic{i}iic{i}{i}c{d}dddc{d}{d}ddddc{{i}dddd}iic{d}{d}{d}iic{i}{i}iic{i}iiiiic{{d}}c{{i}}iiiciic{{d}iii}ic{{i}dddd}ic{i}iiiiic{d}{d}ddddc{i}dddciiic{{d}iii}ddc{{i}ddddd}dcddddc{{i}dddddd}c{d}{d}ddddddc{{d}iiii}c{{i}ddddd}iiiiic{{d}iiiii}ic{{i}ddd}dddc{{d}iii}dddc{{i}ddd}c{{d}iii}iic{d}{d}{d}iiic{i}{i}{i}iic{i}{i}{i}dc{d}{d}ic{{i}ddddd}ic{i}ic{{d}iiii}ddddc{i}c{i}{i}iic{i}iic{i}{i}{i}dddc{{d}ii}ddddddc{i}iic{{i}dddd}ddc{d}{d}ddddc{d}{d}ddddc{d}dddc{i}iiiiiic{d}{d}iiic{{i}ddd}dc{{d}iii}dddddc{d}{d}{d}iiic{{i}}{i}iiiiic{{d}iiii}ic{i}{i}iiic{i}{i}ic{d}ddc{{d}iiiii}iiic{{i}dddddd}iiiic{{d}iiii}iiic{i}{i}c{d}{d}dddddc{{i}ddd}iiiiiic{{d}iiiii}c{d}{d}ddddc{{i}d}ic{{d}iiiiii}dddddcdddddc{d}{d}{d}iiic{i}{i}ddcddddc{{i}ddddd}iiic{{d}}ddddddc{{i}d}iic{{d}iiiii}ddddddc{{i}dddddd}dc{d}{d}dddc{{i}dddd}dddc{{d}iiiiii}ddddddc{i}{i}{i}iiiic{d}{d}dddc{{i}dddddd}ddc{{d}iiii}dc{i}{i}dc{d}{d}iic{i}{i}{i}iiiiiiciiiic{{d}iii}ic{{i}dd}iiiiciic{d}{d}dc{{d}iiii}dddddc{{i}ddd}dddc{{d}i}c{{i}}{i}iic{{d}ii}ddcic{{i}ddd}iiic{{d}iiiiii}dddc{d}{d}{d}c{i}{i}ic{{i}dddd}iiiic{{d}iii}iic{{i}ddddd}iiiiic{{d}iiii}dc{d}{d}iiic{{i}dddd}dddc{i}{i}{i}iiicic{d}ddddc{d}{d}{d}dc{d}ddc{{i}ddddd}dc{d}{d}{d}iic{{d}ii}c{{i}ddddd}ic{i}{i}ddciiiiic{d}{d}{d}iic{i}{i}{i}ddc{d}{d}{d}ddc{i}ddc{{i}dddddd}iiiiic{{d}iiiii}ddcdddddc{d}dc{i}c{i}dc{d}{d}ic{{i}dddd}iiiic{d}dddciiiiiic{{d}iiii}c{i}{i}dddc{i}{i}ddc{{d}iiii}ic{i}{i}iiiiic{{i}dddddd}c{{i}dddddd}c{{d}iii}dddddc{i}iiiiiic{{i}ddd}dc{{d}iiiiii}ddddddc{{i}dddddd}iiic{d}{d}{d}ddc{{d}iiiii}ddc{{i}ddddd}iiiiic{d}dcdc{{d}iiiiii}dciiiiic{i}{i}ic{i}c{{i}dddddd}iiiiic{{d}iiii}dcdddddc{{d}iiiiii}dddddc{{i}}iic{{d}iiii}iiic{i}ic{{i}dddd}c{{d}iiiii}dddddcddc{{i}dddddd}iiiiiic{{d}iiiiii}dddddc{d}{d}{d}ddddciiiiic{i}{i}icic{i}{i}{i}iiiic{{d}iiii}dddc{{i}dddd}iiiiiic{i}iiiiiic{{d}iiiiii}dddddcddddcc{d}{d}{d}iic{d}{d}{d}ic{{i}dddd}iic{d}dddcdc{{i}ddddd}iic{{d}iii}ddc{{i}ddd}iiiic{{d}iii}ddddc{{i}ddddd}iic{{d}iiiii}cddddc{{i}dd}ic{d}c{d}{d}{d}ic{i}{i}iic{{d}iiiii}ddddddc{i}{i}iiiiic{{i}ddddd}dddc{{d}iii}c{d}c{{i}dddddd}iiic{{d}iii}ic{{i}dddd}c{d}{d}{d}iic{{i}dddddd}iiic{i}{i}{i}ddc{{d}iiii}dddddc{d}ddc{{i}dddd}iiiic{i}dc{{d}iiii}dddddc{{i}dddd}iiiiic{{d}iii}ddc{{i}dddd}iiiiciiiiic{d}{d}ddddddciiiic{{i}dddddd}iic{d}ic{{d}iii}dddc{{i}ddd}dc{{d}iiiiii}iiic

```

# Answer
> 0 votes
# Brainfuck, 100 + 140 bytes

```
,[<+++[>>++>+<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>,<[-<----[>>-<<----]>>-<]>.[-]<<<-[->>>+<<<]>>>]>>>,]

```

Attempt This Online!

Explanation: every 3 ASCII characters are encoded in 4 bytes (1 character). Counting bytes from left to right:

* the least significant \\$6\\$ bits of each \\$n\\$-th ASCII char are stored in the least significant \\$6\\$ bits of the \\$n+1\\$-th byte of the Unicode char;
* the \\$6\\$-th least significant bit of each \\$n\\$-th ASCII char is stored in the \\$n\\$-th bit of the first byte of the Unicode char.

Visually:

```
0abcdefg 0hijklmn 0opqrstu => 11110oha 10bcdefg 10ijklmn 10pqrstu

```

---
Tags: code-golf, kolmogorov-complexity, unicode
---