26
1
In set theory, the natural numbers \$\mathbb{N} = \{0, 1, 2, 3, ...\}\$ are usually encoded as pure sets, that is sets which only contain the empty set or other sets that are pure. However, not all pure sets represent natural numbers. This challenge is about deciding whether a given pure set represents an encoding of natural number or not.
The encoding of natural numbers works in the following way1:
- Zero is the empty set: \$ Set(0) = \{\} \$
- For a number \$n > 0\$: \$ Set(n) = Set(n-1) \cup \{Set(n-1)\}\$
Thus, the encodings of the first few natural numbers are
- \$ 0 \leadsto \{\}\$
- \$ 1 \leadsto \{0\} \leadsto \{\{\}\}\$
- \$ 2 \leadsto \{0,1\} \leadsto \{\{\},\{\{\}\}\}\$
- \$ 3 \leadsto \{0,1,2\} \leadsto \{\{\},\{\{\}\},\{\{\},\{\{\}\}\}\}\$
- \$ 4 \leadsto \{0,1,2,3\} \leadsto \{\{\},\{\{\}\},\{\{\},\{\{\}\}\},\{\{\},\{\{\}\},\{\{\},\{\{\}\}\}\}\}\$
The Task
- Given a string representing a pure set, determine whether this set encodes a natural number according to the above construction.
- Note, however, that the elements of a set are not ordered, so \$\{\{\},\{\{\}\},\{\{\},\{\{\}\}\}\}\$ is not the only valid representation of \$3\$ as e.g. \$\{\{\{\}\},\{\},\{\{\{\}\},\{\}\}\}\$ represents the same set.
- You may use
[]
,()
or<>
instead of{}
. - You may assume the sets are given without the
,
as separator. - You can assume there won't be any duplicate elements in the input, e.g.
{{},{}}
is not a valid input, and that the input is well-formed, e.g. no{{},
,{,{}}
or similar.
Test Cases
True:
{}
{{}}
{{},{{}}}
{{{}},{}}
{{},{{}},{{},{{}}}}
{{{},{{}}},{},{{}}}
{{{{}},{}},{{}},{}}
{{},{{}},{{},{{}}},{{},{{}},{{},{{}}}}}
{{{{{}},{}},{{}},{}},{{}},{},{{},{{}}}}
{{},{{}},{{},{{}},{{},{{}}},{{},{{}},{{},{{}}}}},{{{}},{}},{{},{{}},{{},{{}}}}}
{{{{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}}
False:
{{{}}}
{{{{}}}}
{{{{}},{}}}
{{},{{}},{{{}}}}
{{{},{{}}},{{}}}
{{{{{}}},{}},{{}},{}}
{{},{{}},{{},{{}}},{{},{{}},{{{}}}}}
{{{{{}},{}},{{{}}},{}},{{}},{},{{},{{}}}}
{{{{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}},{{{{{}},{}},{{}},{}},{{{}},{}},{{}}},{{{{}},{}},{{}},{}},{{{}},{}},{{}},{}}
Related: Natural Construction (Output the set encoding of a given natural number.)
1 See https://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers
13The test cases look like a program in a (yet) unimplemented esolang :) – Galen Ivanov – 2018-12-20T20:04:40.690
2can the input be a data structure (nested lists) instead of a string? – ngn – 2018-12-20T21:12:03.867
3
I thought it was Brain-flak for a moment.
– Belhenix – 2018-12-20T22:21:19.770Just for clarification, it seems to be implied, but the input will always be well formed? e.g. no
{{}
,{,{}}
or{}{{},{{}}}
– Post Rock Garf Hunter – 2018-12-21T03:59:21.8105@ngn No, input needs to be a string. – Laikoni – 2018-12-21T09:25:48.460
2@Laikoni, if only string input is acceptable, I think this basically invalidates current top answers. – Kirill L. – 2018-12-21T11:47:18.777
This reminds me of surreal numbers, I should make a challenge about them! – Quintec – 2018-12-21T15:12:12.810
@PostLeftGarfHunter Yes, the input will always be well-formed. – Laikoni – 2018-12-21T21:51:53.637
4@KirillL. Technically these answers weren't valid to begin with as the challenge always stated "Given a string representing a pure set", though I do see the point that allowing nested data structures allows for interesting golfing opportunities. However, I find it hard to decide where to draw the line on what is an allowed data structure and what isn't to avoid abuse of a too lenient input format, so I decided to restrict inputs to strings for the sake of simplicity and unambiguousness. – Laikoni – 2018-12-21T22:00:00.603
1@GalenIvanov If you go about inventing a language inspired by this question you could make it so that the task in this question could be implemented using the empty string. (That of course doesn't qualify as an answer as I believe answers have to be written in a language which existed before the question was asked.) – kasperd – 2018-12-22T10:48:25.287
@kasperd Languages created after a challenge was made are now competing. – Erik the Outgolfer – 2018-12-22T11:51:41.220
1
@GalenIvanov Reminds me of ObCode ... Guess I have to try solving the challenge in it!
– wastl – 2018-12-22T23:51:28.480@JonathanAllan Thanks for letting me know. I reformulated the first sentence, though I'm not sure whether it is much clearer now as for me it was clear from the beginning that all numbers are encoded as pure sets, but not all pure sets represent such an encoding. If you have a suggestion on how to aid understanding, please let me know. – Laikoni – 2018-12-23T14:43:28.350