Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Make my number a set

+7
−0

Natural to set

(set meaning an unordered collection with no duplicates, though answers may use and output lists instead)

Recently I was brainstorming what a language with only (arbitrarily nested) sets would be like. A number is represented with a set of that many elements. The elements need to be different, which leads to this definition:

Definition

  • The set representation of $0$ is the empty set.
  • The set representation of any other integer $n$ is the set of the set representations of all positive integers less than $n$, including zero.
  • Negative, complex, or rational numbers do not need to be handled.
  • Your full program or function is to convert from a positive integer to a set or list.

Examples

0 {}
1 {{}}
2 {{}, {{}}}
3 {{}, {{}}, {{}, {{}}}}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

10 answers

+5
−0

BQN, 7 6 bytesSBCS

≢↑∘⊢´↕

Run online!

The less-golfed version {↑⍟𝕩⟨⟩} is more readable: it applies Prefixes () the given number of times to the empty list ⟨⟩ and… that's it. (Suffixes also works, placing elements in the opposite order). The reason this works is that a prefix of a set-number is also a set-number, so that the values of the prefixes for n range from the empty prefix 0 to the complete prefix n. This list is defined to be n+1.

The golfed version uses Fold (´) and some other tricks to turn the computation into a 3-train: something that doesn't work well with Repeat () because the number needs to be an operand.

≢↑∘⊢´↕
     ↕  # Range: a list with length given by the argument
≢       # Shape: an empty list as the argument has no axes
    ´   # Fold over the range, starting with the shape
 ↑      #   Prefixes
  ∘⊢    #   Of the right argument
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+5
−0

JavaScript (V8), 32 bytes

f=n=>[...Array(n).keys()].map(f)

Try it online!

Somehow it doesn't recurse forever at n=0. Pretty nice.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+4
−0

Python 3, 34 bytes

f=lambda x:[f(y)for y in range(x)]

Try it online!

-9 bytes thanks to Moshi

Unfortunately, I cannot use Python sets, because sets are unhashable and thus cannot be put into sets.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

34 bytes (1 comment)
+4
−0

APL (ngn/apl), 6 bytes

{∇¨⍳⍵}

Try it online!

Dyalog APL and dzaima/APL appear to not give empty vectors for ⍳0, so I used ngn/APL. I don't know how to turn on boxing there, though, so there's extra processing code in the footer.

{∇¨⍳⍵}
    ⍵ ⍝ The input
   ⍳   ⍝ Make a range [1,⍵]
 ∇¨   ⍝ For each number in that range, run this function on it
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Japt, 3 bytes

ÆßX

Try it online! (don't mind the -Q

This is Shaggy's solution.

ÆßX
Æ   Make a range [0, input) and map each number X:
 ß  Run this program again on
  X

Japt, 11 8 bytes

Saved 3 bytes thanks to Shaggy! (Shaggy also has a 3-byter ready)

gA=_o mA

Try it online!

This is practically a port of Razetime's answer, which you should upvote!

gA=_o mA
g        Apply the following function to the input
 A=      It's a recursive function, so assign it to the variable A
   _o    Make a range [0, Z), where Z is the input
      mA And map every number in that range to A again
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Welcome (4 comments)
+3
−0

Haskell, 51 31 bytes

Saved 20 bytes thanks to Hakerh400!

data S=S[S]
f i=S$map f[0..i-1]

Try it online!

S is a recursive data structure, because sets of varying element types can't be represented with simple lists in Haskell.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+3
−0

Jelly, 3 bytes

Ḷ߀

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Ruby, 27 bytes

->n{x=[];n.times{x<<x*1};x}

->n{                      }  # lambda taking n
    x=[];                    # set x to empty array
         n.times{      };    # repeat n times
                 x<<x*1      # append x with a copy of itself       
                         x   # return x

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

alternative 27 bytes: `o=->n{(0...n).map{|x|o[x]}}` (3 comments)
+2
−0

Jq, 19 bytes

def s:[range(.)|s];
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 23 bytes

->n,*x{eval'x<<x*1;'*n}

Try this online!

There is also 22 bytes version, but it uses special $* global variables (so it can be run only once in single process):

->n{eval'$*<<$**1;'*n}

Try this online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »