17
\$\begingroup\$

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]
\$\endgroup\$
7
  • \$\begingroup\$ This is kinda like counting binary in reverse \$\endgroup\$
    – ThreeFx
    Jul 18, 2016 at 23:06
  • \$\begingroup\$ @ThreeFx Except that, when adding 1 to 11, you get 000 instead of 100. ;P \$\endgroup\$ Jul 18, 2016 at 23:08
  • 1
    \$\begingroup\$ Can our answer be one-indexed? \$\endgroup\$
    – Downgoat
    Jul 18, 2016 at 23:57
  • \$\begingroup\$ 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... \$\endgroup\$
    – Dennis
    Jul 19, 2016 at 2:37
  • \$\begingroup\$ @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*. \$\endgroup\$ Jul 19, 2016 at 7:13

9 Answers 9

10
\$\begingroup\$

JavaScript ES6, 94 92 72 70 66 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.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Use >>1 instead of /2|0. Use (_,l)=> instead of .keys(). \$\endgroup\$
    – Neil
    Jul 19, 2016 at 0:01
  • \$\begingroup\$ @Neil thanks! That saved a quite a bit. By your last point, do you mean to use Array.from()?, fill, or something else? \$\endgroup\$
    – Downgoat
    Jul 19, 2016 at 0:06
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – Neil
    Jul 19, 2016 at 0:17
  • \$\begingroup\$ Oh, there's also a*-~a/2; I don't know why I didn't think of it before. \$\endgroup\$
    – Neil
    Jul 19, 2016 at 9:05
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ That use of the enum is clever. +1 \$\endgroup\$ Jul 19, 2016 at 22:47
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

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

APL (Dyalog Extended), 21 bytesSBCS

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

Try it online!

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.