Integer to Nested Structure
Description:
Taken from: https://codegolf.stackexchange.com/questions/139034/encode-an-integer
−--
We shall encode an integer in the following way:
Given positive integer n > 1
. We convert it to an array as follows:
- First, create an array of all
n
's prime factors sorted ascending - Then, for every element: if it is equal to
2
, return an empty array. Otherwise replace the number with an array with its index in the prime numbers sequence, and then convert this number.
For example, lets convert number 46
to array. Firstly, convert it to array of its prime factors:
[2, 23]
Number 23
is 9
th prime, so replace 2
with empty array and 23
with [9]
. Array now becomes:
[[], [9]]
Prime factors of 9
are 3
and 3
, so:
[[], [3, 3]]
Do the same for both 3
:
[[], [[2], [2]]]
And finally:
[[], [[[]], [[]]]]
Now, to encode it, we simply replace each open bracket with 1
and each closing bracket with 0
, then remove all ending zeros and drop one 1
from the end. This is our binary number. Using the above example:
[ ] [ [ [ ] ] [ [ ] ] ]
| | | | | | | | | | | |
| | | | | | | | | | | |
V V V V V V V V V V V V
1 0 1 1 1 0 0 1 1 0 0 0
Now simply drop the last three zeros and the last 1
. The number becomes 10111001
which is 185
in decimal. That is the expected output. Notice that in array to binary conversion brackets of the main array are not included.
Your task is to write a pair of functions, encode
and decode
. encode
should encode a number according the above procedure, and decode
will return the original number given an encoded number.
Similar Kata:
Stats:
Created | Aug 15, 2017 |
Published | Aug 15, 2017 |
Warriors Trained | 1252 |
Total Skips | 376 |
Total Code Submissions | 1451 |
Total Times Completed | 172 |
JavaScript Completions | 134 |
Haskell Completions | 39 |
Total Stars | 89 |
% of votes with a positive feedback rating | 93% of 41 |
Total "Very Satisfied" Votes | 36 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 5 |
Average Assessed Rank | 3 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |