# Generate the Rummy Sequence

Your task is to take an input n and output element n of the Rummy Sequence, a sequence which I made (looking on OEIS will not help you).

# Definition

Each element of the Rummy Sequence is a set of truthy or falsey values. Ex.: [true, false].

The steps to producing a member of the Rummy Sequence are quite simple:

1. Start out with the first index, [] (this is element 0).
2. Set the leftmost falsey to truthy. If there are no falseys to change, then increase the length of the list by 1 and set all members of the new list to falsey.
3. Repeat step 2 until reaching element n.

# Example

Let's define our function as rummy(int n) (stuff in {} is a step taken to get to the answer):

>>> rummy(5)
{[]}
{[false]}
{[true]}
{[false, false]}
{[true, false]}
[true, true]


# Rules

• Standard loopholes apply.
• Must work for inputs 0 through your language's upper numerical bound.
• You may output in any way you see fit, provided that it is clear that the output is a set of truthy/falseys.

# Trivia

I call this the "Rummy Sequence" because, starting at index 2, it defines the sets you would need to lay down in each round of Progressive Rummy, where falsey is a book and truthy is a run.

# Test Cases

>>> rummy(0)
[]

>>> rummy(1)
[false]

>>> rummy(6)
[false, false, false]

>>> rummy(20)
[true, true, true, true, true]

>>> rummy(1000)
[true, true, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]

• This is kinda like counting binary in reverse Jul 18, 2016 at 23:06
• @ThreeFx Except that, when adding 1 to 11, you get 000 instead of 100. ;P Jul 18, 2016 at 23:08
• Can our answer be one-indexed? Jul 18, 2016 at 23:57
• I think you should include a few more test cases, even if the outputs are implicitly mentioned in the example. My first revision broke with the corner case 1... Jul 19, 2016 at 2:37
• @VTCAKAVSMoACE That would make it bijective binary (which we also have a challenge for), but there are more differences in that every number is always of the form 1*0*. Jul 19, 2016 at 7:13

# JavaScript ES6, 9492727066 64 bytes

Saved 6 bytes thanks to Neil!

n=>[...Array(a=Math.sqrt(8*n+1)-1>>1)].map((_,l)=>l<n-a*(a+1)/2)


I don't think this can be golfed more. At least with the equations.

## Explanation

They are two main equations (n is input):

(Math.sqrt(8*n+1)-1)/2


This will give the total size the output array will need to be. In my program I used >>1 instead of (...)/2 these are the same as the first bit in binary has a value of 2. Shifting it will result in in floor(.../2)

n-a*(a+1)/2


This is the amount of trues there will be. a is the result of the previous expression.

This is what the syntax does:

[...Array(n)]


This code generates an array with range [0, n) in this answer n is the first equation.

.map((_,l)=>l<n) this will loop through the above range, l is the variable containing the current item in the range. If the item is less than the amount of trues they are (determined by second equation), then it will return true, else false.

• Use >>1 instead of /2|0. Use (_,l)=> instead of .keys().
– Neil
Jul 19, 2016 at 0:01
• @Neil thanks! That saved a quite a bit. By your last point, do you mean to use Array.from()?, fill, or something else? Jul 19, 2016 at 0:06
• No, I was thinking of [...Array(a)].map((_,l)=>) which I believe is slightly shorter, but good catch on removing some of the ()s when switching to >>1, I hadn't spotted that!
– Neil
Jul 19, 2016 at 0:17
• Oh, there's also a*-~a/2; I don't know why I didn't think of it before.
– Neil
Jul 19, 2016 at 9:05

## Python, 51 bytes

f=lambda n,i=0:n>i and f(n+~i,i+1)or[1]*n+[0]*(i-n)


Outputs a list of 1's and 0's.

# Pyth, 8 bytes

_@{y/RQy


Try it online: Demonstration or Test Suite

This is exponentially slow.

### Explanation:

_@{y/RQyQQ    implicit Qs at the end, (Q = input)
yQ     2*Q
/RQ       divide each number in [0, 1, ..., 2*Q-1] by Q
this results in a list of Q zeros and Q ones
y          take all subsets
{           remove duplicates
@       Q    take the Qth element
_             print it reversed


# Jelly, 13 11 bytes

Ḷṗ2SÞ⁸ị1,0x


The code does not work in the latest version of Jelly before the challenge was posted, but it did work in this version, which predates the challenge.

Indices are 1-based. Try it online! (takes a few seconds) or verify multiple inputs at once.

### How it works

Ḷṗ2SÞ⁸ị1,0x  Main link. Argument: n (integer)

Ḷ            Unlength; yield [0, ..., n - 1].
ṗ2          Take the second Cartesian power, i.e., generate the array of all
pairs of elements of [0, ..., n - 1].
SÞ        Sort the pairs by their sum. The sort is stable, so ties are broken
by lexicographical order.
⁸ị      Retrieve the pair at index n.
1,0x  Map [a, b] to a copies of 1 and b copies of 0.


## 05AB1E, 27 bytes

8*>t<;ïÐ>*;¹s-ïD1s×Š-0s×JSï


Will see if I can golf it some more and add an explanation in the morning.

Try it online

# Java, 117 110 bytes

enum B{T,F};B[]r(int n){int i=0,c=0,j=0;while(n>=i)i+=++c;B[]a=new B[c-1];for(;j<n-i+c;)a[j++]=B.T;return a;}


created my own boolean type, which allowed me to save 7bytes

• That use of the enum is clever. +1 Jul 19, 2016 at 22:47

# Python 2, 69 63 bytes

a=b=0
exec'a,b=[a-1,b+1,0][a<1:][:2];'*input()
print[1]*b+[0]*a


Test it on Ideone.

## Python 2, 61 bytes

j=(2*input()+.25)**.5-.5
print[i/j<j%1for i in range(int(j))]


Solves for n = j·(j+1)/2. Input is taken from stdin.

Sample Usage

$echo 20 | python rummy-seq.py [True, True, True, True, True]$ echo 50 | python rummy-seq.py
[True, True, True, True, True, False, False, False, False]


Demo.

# APL (Dyalog Extended), 21 bytesSBCS

{⍵⌷↑,/(↓0 1↓⍳≥\⍳)¨⍳⍵}


Try it online!