18
2
Related, but this only requires positive integers and does not have to be commutative
The Cantor Pairing Function is described in this Wikipedia article. Essentially, it is an operation such that when it is applied to two values X and Y, one can obtain the original values X and Y given the result.
Your task is to design two functions: one which performs X, Y -> Z
and the other which performs Z -> X, Y
. Here's the catch: X, Y -> Z
must be commutative. This means that Z -> X, Y
won't be able to determine if the input was X, Y
or Y, X
.
The formal definition of this challenge would be:
Choose an countable infinite set S of numbers.
Design two functions that perform the following tasks:
- Given an unordered pair of values in S, return a value in S
- Given a return value from the initial function, return the unordered pair of values which evaluates to the input integer when passed through the first function. I don't care about the behaviour of this inverse function if the input isn't a return value from the first function.
Requirements
- The result should be identical between runs.
{a, a}
is an unordered pair
Note: your answer is more likely to get an upvote from me if you provide a proof, but I will test answers when I get to it and upvote it once I'm fairly sure it works.
Doesn't this fit better for https://puzzling.stackexchange.com/?
– Jakube – 2017-09-11T13:13:10.0332@Jakube Not necessarily, as you are required to write code. – Mr. Xcoder – 2017-09-11T13:13:40.033
I'm assuming pairs are unique, but the numbers used in those pairs are not? So when
1,2
is one of the pairs,1,3
can also be a potential pair (both use1
)? – Kevin Cruijssen – 2017-09-11T13:38:39.657@KevinCruijssen I'm not sure what you mean – HyperNeutrino – 2017-09-11T13:48:39.573
@Giuseppe The inverse does not need to be able to return the correct order; it's just that for function
f
and its inverseg
,sorted((x, y))
should be the same assorted(g(f(x, y)))
– HyperNeutrino – 2017-09-11T13:49:28.883@HyperNeutrino gotcha. That makes sense now. – Giuseppe – 2017-09-11T13:54:54.803
Can we return a delimited string containing X & Y for the second function? – Shaggy – 2017-09-11T15:40:10.110
All answers so far appear to have assumed that {a, a} is an unordered pair, yet some definitions don't allow the members to be equal. Could you clarify? – Dennis – 2017-09-11T15:44:43.620
Can the two functions share code? – xnor – 2017-09-11T20:22:33.123
@xnor I'm going to say that the two programs must be independent; that is, they can run on their own. Shared code would then have to be counted twice – HyperNeutrino – 2017-09-11T20:24:55.307
does the function have to be a bijection? the title says so, but I don't see this requirement in the question, just injective. – proud haskeller – 2017-09-11T21:25:27.810
@proudhaskeller I should remove
bijection
sincecommutative bijection
is a contradiction. Yes, just injective. Thanks. – HyperNeutrino – 2017-09-11T21:29:07.143@Shaggy If that's approved by meta as a way of outputting a pair/tuple of numbers, then sure. If it's obvious what the two numbers are, that's fine. – HyperNeutrino – 2017-09-11T21:31:18.540
@Dennis Um yes I should have specified;
{a, a}
is an unordered pair – HyperNeutrino – 2017-09-11T21:31:44.433If the pair-to-value function isn't required to be surjective, maybe "Given a value in S" could say "Given a return value from the first function"? And maybe clarify whether it's okay for the value-to-pair function to output anything including pairs of elements of S, crash, etc. if its input is not a possible output of the pair-to-value function? – aschepler – 2017-09-12T01:45:30.887
@aschepler Sure, I'll add something and if you want more clarified I'll add more. Thanks! – HyperNeutrino – 2017-09-12T01:49:47.617