18
1
In this challenge you will be asked to implement any function (or full program) that fulfills two properties. Those properties are:
Your function must be an injective (reversible) function from the polynomials with non-negative integer coeffecients to the non-negative integers. This means no two unequal inputs can map to an equal output.
Your function must preserve the total number of "on bits" from its input to its output. This means if you count the 1 bits of each coefficient of the polynomial, their sum should be the same as the number of 1 bits in the binary representation of the output. For example
9
is1001
in binary so it has 21
bits.
IO
A non-negative integer polynomial is the same as a infinite list of non-negative integers such that after a certain point all the integers are zero. Thus, polynomials may be represented either by infinite lists (although this is probably undesirable) or by finite lists with implicit zeros after the end of the list.
The key distinction between polynomials and finite lists is that adding a zero to the end of a list will change the list:
While adding a zero to the end of a polynomial does not change its value:
Thus if your function takes a finite list representing a polynomial as input, adding a zero must not change its result.
When representing polynomials as lists, you may represent them either with the first or last entry representing the constant term. For example you could have either of the following possibilities:
In the first case, adding zeros to the end of the list should not change the result; in the second case, adding zeros to the front of the list should not change the result.
Of course if your language supports polynomials you may take those as inputs.
Output should be a non-negative integer output via any standard methods.
This is code-golf so answers will be scored in bytes, with fewer bytes being better.
Is
[]
or[0]
a valid input? – JungHwan Min – 2017-12-25T23:43:28.5301@JungHwanMin Yes, both are, they are the zero polynomial. – Post Rock Garf Hunter – 2017-12-25T23:44:52.677
I know you mean to put 1 to split zeros, but some ways may work and seem not that good... – l4m2 – 2017-12-26T07:02:33.617
Can I require the leading zeros not written? – l4m2 – 2017-12-26T07:03:46.777
1@l4m2 I'm sorry, but I don't understand either of your comments. As far as your question goes, leading zeros on what? The polynomial, the coefficients? I'm not sure what you mean by "zeros not written" either. – Post Rock Garf Hunter – 2017-12-26T07:06:26.977
@WheatWizard I think it's the implicit ones. – wizzwizz4 – 2017-12-26T12:47:46.790
More specifically, can we assume that the null polynomial will always be represented as, e.g.,
[0]
and not as[]
or[0, 0]
. – Dennis – 2017-12-26T13:05:31.4501Are the images really necessary (i.e. they cannot be represented using rich text)??? Because people without the ability to see images cannot see your challenge in its entirety. – Mindwin – 2017-12-26T14:18:55.903
@Mindwin Well for one the images are only examples and are not strictly necessary for the challenges. Secondly I rich text looks bad. – Post Rock Garf Hunter – 2017-12-26T19:12:22.603
@Dennis I'm going to say you may assume there is at least one zero in the null polynomial. – Post Rock Garf Hunter – 2017-12-26T19:14:52.800