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 »
Sandbox

Comments on Bijection between natural numbers and finite sets

Post

Bijection between natural numbers and finite sets

+1
−0

Definitions

A natural number is either zero, or a successor of some other natural number.

A set is an unordered collection of zero or more elements. Each element is a set. Two sets that have same elements are equal. In this challenge we consider only hereditarily finite sets.

Cardinality of a set is the number of distinct elements of the set. For any set x, it is represented as |x|.

A set can be represented as a string by encapsulating space-separated string representations of its elements into curly braces. For example, {} is the empty set, {{}} is the set that contains only the empty set, {{}, {}} is still the set that contains only the empty set, {{}, {{}}} is the set that contains only the empty set and the set that contains only the empty set. Duplicate elements are ignored. The order of elements is not important.

Binary relation < on sets is defined in the following way. Given two sets A and B. If A is equal to B, then neither A < B nor B < A. If A is not equal to B, then let m = |A| and n = |B|. If m < n then A < B and if n < m then B < A (where < is the well-known binary relation on natural numbers). If m = n then let x be the array of distinct elements of A sorted in ascending order (for each two elements p and q in the array, p < q iff p precedes q) and let y be the array of distinct elements of B sorted in ascending order. Let i be the smallest index in the array such that the corresponding elements in x and y are not equal. The result of A < B is equal to the result of comparison of the corresponding elements with index i from x and y respectively.

Normalized string representation of a set is a string representation of the set such that each element appears only once, elements are sorted in ascending order and the string representation of each element is normalized.

Conversion from a natural number to a set

Let $S$ be the class of all hereditarily finite sets and let $\Bbb N$ be the set of all natural numbers. The conversion from a natural number to a set is denoted by function $f:\Bbb N\to S$. The conversion from a set to a natural number is denoted by function $g:S\to\Bbb N$. Since it is a bijection, $f(x) = g^{-1}(x)$ and $f^{-1}(x)=g(x)$.

We describe the function $f$. Given a natural number n. Let s be the string {. Perform these steps iteratively (in each iteration apply the first step that can be applied):

  • If there is only one unmatched { in s and n is 0, then append } to s, interpret s as a set and return it as the result.
  • If there is only one unmatched { in s and n is not 0, then append { or ,{ (depending on whether the previous character was { or } respectively) and decrement n by 1.
  • If appending } to s would violate the normalized string representation definition, then append { or ,{ to s.
  • If n is even, then divide it by 2 and append } to s.
  • If n is odd, then subtract 1, divide it by 2 and append { or ,{ to s.

Function $g$ (the conversion from a set to a natural number) is the inverse of this algorithm.

Task

The task is to implement two functionalities (either in the same program, or two separate programs): one for conversion from natural numbers to normalized string representations of sets and the other for conversion from string representations (not necessarily normalized) of sets to natural numbers.

If your solution consists of two programs (or functions), the total byte count is the sum of byte counts of each program. You are also allowed to have a common header/footer for both programs and in that case the total byte count includes header and/or footer only once.

Input

Input is a string that either represents a natural number in base 10, or a string representation of a set.

Output

If the input is a natural number, then output the normalized string representation of the set corresponding to that number. If the input is a string representation of a set, then output the natural number corresponding to that set.

Spaces after commas are optional both in the input and in the output. That is, you may assume that the input contains spaces or you may assume that the input does not contain spaces (mention your decision in the answer). Similarly, the output that your program produces may or may not contain spaces.

Test cases

Number → Set

0   → {}
1   → {{}}
2   → {{{}}}
3   → {{}, {{}}}
4   → {{{{}}}}
5   → {{}, {{{}}}}
6   → {{{}, {{}}}}
7   → {{}, {{}, {{}}}}
8   → {{{{{}}}}}
9   → {{}, {{{{}}}}}
10  → {{{}}, {{}, {{}}}}
11  → {{}, {{}}, {{}, {{}}}}
12  → {{{{}, {{}}}}}
13  → {{}, {{{}, {{}}}}}
14  → {{{}, {{{}}}}}
15  → {{}, {{}, {{{}}}}}
16  → {{{{{{}}}}}}
17  → {{}, {{{{{}}}}}}
18  → {{{}}, {{{}}}}
19  → {{}, {{}}, {{{}}}}
20  → {{{{}}, {{}, {{}}}}}
21  → {{}, {{{}}, {{}, {{}}}}}
22  → {{{}, {{}, {{}}}}}
23  → {{}, {{}, {{}, {{}}}}}
24  → {{{{{}, {{}}}}}}
25  → {{}, {{{{}, {{}}}}}}
26  → {{{}}, {{}, {{{}}}}}
27  → {{}, {{}}, {{}, {{{}}}}}
28  → {{{{}, {{{}}}}}}
29  → {{}, {{{}, {{{}}}}}}
30  → {{{}, {{{{}}}}}}
31  → {{}, {{}, {{{{}}}}}}
32  → {{{{{{{}}}}}}}
33  → {{}, {{{{{{}}}}}}}
34  → {{{}}, {{{{}}}}}
35  → {{}, {{}}, {{{{}}}}}
36  → {{{{}}}, {{}, {{}}}}
37  → {{}, {{{}}}, {{}, {{}}}}
38  → {{{}, {{}}, {{}, {{}}}}}
39  → {{}, {{}, {{}}, {{}, {{}}}}}
40  → {{{{{}}, {{}, {{}}}}}}
41  → {{}, {{{{}}, {{}, {{}}}}}}
42  → {{{}}, {{}, {{}, {{}}}}}
43  → {{}, {{}}, {{}, {{}, {{}}}}}
44  → {{{{}, {{}, {{}}}}}}
45  → {{}, {{{}, {{}, {{}}}}}}
46  → {{{}, {{{}, {{}}}}}}
47  → {{}, {{}, {{{}, {{}}}}}}
48  → {{{{{{}, {{}}}}}}}
49  → {{}, {{{{{}, {{}}}}}}}
50  → {{{}}, {{{}, {{}}}}}
51  → {{}, {{}}, {{{}, {{}}}}}
52  → {{{{}}, {{{}}}}}
53  → {{}, {{{}}, {{{}}}}}
54  → {{{}, {{}, {{{}}}}}}
55  → {{}, {{}, {{}, {{{}}}}}}
56  → {{{{{}, {{{}}}}}}}
57  → {{}, {{{{}, {{{}}}}}}}
58  → {{{}}, {{}, {{{{}}}}}}
59  → {{}, {{}}, {{}, {{{{}}}}}}
60  → {{{{}, {{{{}}}}}}}
61  → {{}, {{{}, {{{{}}}}}}}
62  → {{{}, {{{{{}}}}}}}
63  → {{}, {{}, {{{{{}}}}}}}
64  → {{{{{{{{}}}}}}}}
65  → {{}, {{{{{{{}}}}}}}}
66  → {{{}}, {{{{{}}}}}}
67  → {{}, {{}}, {{{{{}}}}}}
68  → {{{{}}}, {{{}}, {{}, {{}}}}}
69  → {{}, {{{}}}, {{{}}, {{}, {{}}}}}
70  → {{{}, {{}}}, {{}, {{}}, {{}, {{}}}}}
71  → {{}, {{}, {{}}}, {{}, {{}}, {{}, {{}}}}}
72  → {{{{{}}}, {{}, {{}}}}}
73  → {{}, {{{{}}}, {{}, {{}}}}}
74  → {{{}}, {{}, {{}}, {{}, {{}}}}}
75  → {{}, {{}}, {{}, {{}}, {{}, {{}}}}}
76  → {{{{}, {{}}, {{}, {{}}}}}}
77  → {{}, {{{}, {{}}, {{}, {{}}}}}}
78  → {{{}, {{{}}, {{}, {{}}}}}}
79  → {{}, {{}, {{{}}, {{}, {{}}}}}}
80  → {{{{{{}}, {{}, {{}}}}}}}
81  → {{}, {{{{{}}, {{}, {{}}}}}}}
82  → {{{}}, {{{}}, {{}, {{}}}}}
83  → {{}, {{}}, {{{}}, {{}, {{}}}}}
84  → {{{{}}, {{}, {{{}}}}}}
85  → {{}, {{{}}, {{}, {{{}}}}}}
86  → {{{}, {{}, {{}, {{}}}}}}
87  → {{}, {{}, {{}, {{}, {{}}}}}}
88  → {{{{{}, {{}, {{}}}}}}}
89  → {{}, {{{{}, {{}, {{}}}}}}}
90  → {{{}}, {{}, {{{}, {{}}}}}}
91  → {{}, {{}}, {{}, {{{}, {{}}}}}}
92  → {{{{}, {{{}, {{}}}}}}}
93  → {{}, {{{}, {{{}, {{}}}}}}}
94  → {{{}, {{{{}, {{}}}}}}}
95  → {{}, {{}, {{{{}, {{}}}}}}}
96  → {{{{{{{}, {{}}}}}}}}
97  → {{}, {{{{{{}, {{}}}}}}}}
98  → {{{}}, {{{{}, {{}}}}}}
99  → {{}, {{}}, {{{{}, {{}}}}}}
100 → {{{{}}}, {{}, {{{}}}}}

Set → Number

{} → 0
{{}} → 1
{{}, {}} → 1
{{}, {}, {}} → 1
{{}, {}, {}, {}} → 1
{{}, {}, {}, {}, {}} → 1
{{}, {}, {}, {}, {}, {}} → 1
{{{}}} → 2
{{{}, {}}} → 2
{{{}}, {{}}} → 2
{{{}, {}, {}}} → 2
{{{}, {}}, {{}}} → 2
{{{}, {}, {}}, {{}}} → 2
{{{}, {}, {}, {}}, {{}}} → 2
{{{}}, {}} → 3
{{}, {{}}} → 3
{{{}, {}}, {}} → 3
{{{}}, {}, {}} → 3
{{}, {{}, {}}} → 3
{{}, {{}}, {}} → 3
{{}, {}, {{}}} → 3
{{{}, {}, {}}, {}} → 3
{{{}, {}}, {}, {}} → 3
{{}, {{}, {}, {}}} → 3
{{}, {{}, {}}, {}} → 3
{{}, {{}}, {}, {}} → 3
{{}, {}, {{}, {}}} → 3
{{}, {}, {{}}, {}} → 3
{{{}, {}}, {{}}, {}} → 3
{{{}}, {{}}, {}, {}} → 3
{{{}}, {}, {{}, {}}} → 3
{{}, {{}}, {{}, {}}} → 3
{{}, {}, {{}}, {{}}} → 3
{{}, {}, {}, {}, {}, {{}, {}}} → 3
{{{{}}}} → 4
{{{{}, {}}}} → 4
{{{{}}, {{}}}} → 4
{{{{}, {}, {}}}} → 4
{{{{}}, {{}, {}}}} → 4
{{{{}}, {{}, {}, {}}}} → 4
{{{{}, {}, {}, {}, {}}}, {{{}}}} → 4
{{{{}}}, {}} → 5
{{}, {{{}}}} → 5
{{}, {{{}, {}}}} → 5
{{}, {}, {{{}}}} → 5
{{{{}}, {{}}}, {}} → 5
{{}, {{{}}, {{}}}} → 5
{{{{}}}, {{{}}}, {}} → 5
{{}, {}, {{{}}}, {}} → 5
{{}, {}, {}, {{{}}}} → 5
{{}, {{{}, {}, {}, {}}}} → 5
{{}, {}, {{{}}}, {}, {}} → 5
{{{{}}, {}}} → 6
{{{}, {{}}}} → 6
{{{}, {{}, {}}}} → 6
{{{}, {{}}, {}}} → 6
{{{{}, {}, {}}, {}}} → 6
{{{{}}, {}, {{}}, {}}} → 6
{{}, {{}, {{}}}} → 7
{{}, {{{}}, {}, {}}, {}} → 7
{{}, {}, {{}, {{}, {}}}} → 7
{{{{}}, {}}, {}, {}, {}, {}} → 7
{{}, {}, {{{}, {}}, {{}}, {}}} → 7
{{{{{}}}}} → 8
{{}, {{{{}}}}} → 9
{{{}, {{}}}, {{}}} → 10
{{{}}, {{{}}, {}}} → 10
{{{}, {}}, {{{}}, {}}, {{}}} → 10
{{{}, {{}}}, {{}}, {}} → 11
{{{}, {{}}}, {}, {}, {}, {{}}} → 11
{{{}}, {{}, {{}}, {}, {}}, {}} → 11
{{}, {{}}, {{}, {{}, {}}}, {{}}} → 11
{{{{{}}, {}}}} → 12
{{{{}, {{}}}}} → 12
{{{{}, {}, {{}}}}} → 12
{{}, {{{{}}, {}}}} → 13
{{{}, {{{}}}}} → 14
{{{{{}}}, {}, {}}} → 14
{{{{{{}}}}}} → 16
{{{{{{}}}}}, {}} → 17
{{}, {{{{{}}}}}} → 17
{{{}}, {{{}}}} → 18
{{{}}, {}, {{{}}}} → 19
{{{{}}}, {}, {}, {{}}, {}} → 19
{{{}, {}, {{}, {{}, {}}, {}, {}}, {}, {}}} → 22
{{}, {{{{}}, {}}, {}}} → 23
{{{{{{}}, {}}}}} → 24
{{{{{{}}, {{}, {}}, {}, {}}}}} → 24
{{}, {{{}, {{{}}, {{}, {}}}}}} → 29
{{{{}, {{}, {{}}}}}} → 44
{{{{{}}, {}}}, {{}, {}}} → 50
{{{{}}, {{{}}}}} → 52
{{{}, {{{{}}, {}}, {}}, {}}} → 86
{{{}, {{{}}}, {{}}}} → 102
{{{{{}}, {{{}, {}}}}}} → 104
{{{{{}, {{{{}}}}}}}} → 120
{{{}}, {{{{{{}}, {}}}}}, {}} → 195
{{{}}, {{{}, {}, {}}, {{{}}}, {}}} → 202
{{{}, {{{{{{{{}, {}}}}, {{}}}}}}}} → 3710
{{{{{}}, {}, {{{}}, {{{}}}, {{}}}}, {{}}}} → 13076

Scoring

The shortest program in each language wins.

Clarifications

  • The challenge is to implement two programs (or functions). The first program takes a base-10 string representation of a natural number as argument and outputs the normalized string representation of the corresponding set. The second function takes a string representation of a set as argument and returns the base-10 string representation of the corresponding natural number. Your programs take a string as an input and produce a string as the output. Each natural number can be uniquely represented as a string, but that is not the case with sets. As we showed, a string representation of a set may contain duplicate elements and the order of elements may vary. That is why we introduce the normalized string representation - to represent a set in a unique way.

  • Functions $f$ and $g$ are described in the section "Conversion from a natural number to a set". The algorithm that is explained there is for converting from natural numbers to sets, so that represents function $f$. Since it is a bijection, we can uniquely implement function $g$ that does the opposite - converts a set to a natural number. Once again, the algorithm that is explained represents function $f$ and you need to figure out the algorithm for $g$ (it has been proven to exist).

  • Variable s that is used in the algorithm for explaining function $f$ is just a local variable. It is not explicitly stated that it is related to the normalized string representation of the resulting set that your program should output. Since the definition of $f$ states that $f$ takes a natural number and returns a set, it does not return a string. However, it can be proved that the s from that algorithm is indeed the valid normalized string representation of the resulting set that $f$ constructs. Noticing that s already contains the required final string output is not mandatory for solving the challenge, but can save you some bytes (instead of parsing s and then stringifying it again using normalized string representation, you can just return s). Once again, this is just an optimization detail that is not intended to be explicitly stated in the challenge, but now mentioning it according to the comments.

  • Your program for converting from natural numbers to sets takes a string as argument (in case of a function), or reads from stdin or in any other way you like. The string represents a natural number in base 10. Your program uses the definition of function $f$ as described above to output the normalized string representation of the corresponding set.

  • Your program for converting from sets to natural numbers takes a string as argument (in case of a function), or reads from stdin or in any other way you like. The string represents a set (you may expect a non-normalized string as input). Your program uses the definition of function $g$ as described above to output the corresponding natural number stringified in base 10.

  • The binary relation < on sets that is defined above is not related to the < binary relation on natural numbers. In other words, for any two natural numbers $x$ and $y$, statements $x < y$, and $f(x) < f(y)$ and $\{f(x)\} < \{f(y)\}$ are unrelated. That is, $x < y$ does not imply that $f(x) < f(y)$, and $f(x) < f(y)$ does not imply that $\{f(x)\} < \{f(y)\}$, or any other relation. You should not implicitly assume any relation that is not explicitly stated in this article, or that cannot be concluded from the definitions given here. If you look at the test cases, you can clearly see for example that $f(4) < f(3)$, despite the fact that $3 < 4$.

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 (17 comments)
General comments
Razetime‭ wrote almost 4 years ago

There must be some sort of recursive definition for this..

Hakerh400‭ wrote almost 4 years ago

@Razetime‭ Please let me know if any recursive definition is missing a base case.

celtschk‭ wrote almost 4 years ago · edited almost 4 years ago

Your depth is called rank (see here). Note that the definition you give only works for finite sets, as in infinite sets there may not be a maximum, see the link for a definition that always works. Also, what you call “finite” is properly called hereditary finite (“finite” alone only refers to cardinality). However sets of finite rank are already hereditary finite, so the additional requirement of finite cardinality is superfluous.

celtschk‭ wrote almost 4 years ago

I haven't completely gone through your algorithm, but I suspect what you define is the Ackermann bijection between the natural numbers and the hereditary finite sets.

Hakerh400‭ wrote almost 4 years ago · edited almost 4 years ago

@celtschk‭ The decision to avoid terms "rank" and "hereditary finite" was intentional, because they involve transfinite recursion and depend on set-theoretic axioms. The post is now edited, please let me know if the definitions are still misleading. Also, this is not the Ackermann bijection (6 is a counterexample: this algorithm gives {{{}, {{}}}}, while Ackermann gives {{{}}, {{{}}}}).

celtschk‭ wrote almost 4 years ago

Since all strings are of finite length, only hereditary finite sets can be represented as such strings anyway. And even if you consider strings of countably infinite length, you obviously cannot represent all sets as such strings. I think a better strategy would be to define the “set strings” by their form (maybe give their grammar), explain how to get a set from them, and then state that only sets that can be obtained from (finite) strings this way are considered.

celtschk‭ wrote almost 4 years ago · edited almost 4 years ago

BTW, your algorithm produces strings without spaces, but your examples contain spaces after the commas. This raises the question of (a) whether those spaces should be generated, and (b) whether the code should handle such spaces and/or the absence of them in the input.

celtschk‭ wrote almost 4 years ago · edited almost 4 years ago

Also, what number corresponds to the set {{{}, {{{}}}}} (that is, {5})? Up to n=4, the sets {n} seem to have ascending numbers ({0}=1, {1}=2, {2}=4, {3}=6, {4}=8), but then the next single-element set is {6}=12. Indeed, from those examples, it seems that {n}=2n, but 10={1,3}≠{5}.

Moshi‭ wrote almost 4 years ago

Is there a point to combining the two functionalities into one? I'm pretty sure that it basically means every answer will just have a input[0]=='{'?xxx:yyy (or equivalent) tacked to the front of it.

Hakerh400‭ wrote almost 4 years ago

@celtschk‭ Added link to "hereditarily finite" wiki article, hope all concerns are addressed now. Regarding the spaces, they are optional (mentioned that in the post, thanks). BTW, the algorithm does not produce a string, it produces a set (it says "interpret s as a set and return it as the result - s is just a local variable used in the algorithm). Regarding {n}=2n, it's not true in general (why do you think it should be true?). Set {{{}, {{{}}}}} corresponds to number 14.

Hakerh400‭ wrote almost 4 years ago

@Moshi‭ Two separate programs are allowed now.

celtschk‭ wrote almost 4 years ago

According to the task description, the task is to return a string, not a set. I took it that your algorithm gives that string. So what you are saying is that the algorithm only gives some representation of the set, and the program has to then calculate the set from that representation, and then to generate the normalised string representation from that set?

celtschk‭ wrote almost 4 years ago

On the {n}=2n: That's just an observation/extrapolation from the examples given. But the order you describe implies m<n => {m} < {n], which means that when {5} > {6} then your mapping does not respect your order. While there's of course not a requirement to respect the order, it is what I would expect if an order and a mapping between ordered sets is given. At the very minimum, explicitly mention that the mapping is not order preserving.

Hakerh400‭ wrote almost 4 years ago

@celtschk String s is indeed the normalized string representation, but figuring that out was intended to be a part of the challenge. Regarding the order of the mapping, what's the point of mentioning that the order is not preserved? If f stands for converting number to set and x,y are natural numbers, then neither x < y implies f(x) < f(y), nor f(x) < f(y) implies {f(x)} < {f(y)}. It is clear from the definition of < and from the algorithm itself.

celtschk‭ wrote almost 4 years ago

Figuring that out was intended to be part of the challenge? I would never have guessed that. The one single most important thing about a challenge is to be crystal clear on what is expected from a solution. Up to now, my comments were about things where people could be misled by reasonable assumptions that are not met, but now I see that even without such assumptions, the task is not well described. For now, I retracted my upvote.

Razetime‭ wrote almost 4 years ago

The problem with adding things in to be figured out is that once one person figures it out, everyone else just copies them. Hence, it's generally encouraged to have a full description of what you expect the programmer to do.

Hakerh400‭ wrote almost 4 years ago

The challenge has been updated.