Zeckendorf Cards Generator
Description:
#Source
This kata follows the application of the Zeckendorf magic trick presented here: http://www.codewars.com/kata/magic-zeckendorf. If you haven't completed the first one I strongly suggest you to do it first (it's pretty easy).
#Objective
In the previous kata, the cards used to find the number were given. These cards could find any number between 0 and 100. Your objective is to write a function cardZ(n)
that can generate the cards used to guess a number between 0 and n
. In coding term the function cardZ(n)
should return an array of array. The list of "card" (one card is an array) returned should be in ascending order (e.g. based on the first number of the "card").
##Example: The cards to find a number between 0 and 15 (with the correct index) are:
cardZ(15) ===
[
[1,4,6,9,12,14],
[2,7,10,15],
[3,4,11,12],
[5,6,7],
[8,9,10,11,12],
[13,14,15]
]
#Method It may exist many method to do it but one of the simpliest one is to use a Fibonnacy representation of the numbers. Any number can be represented in a Fibonnacy/Zeckendorf representation (http://en.wikipedia.org/wiki/Fibonacci_coding) :
To encode an integer N:
- Find the largest Fibonacci number equal to or less than N; subtract this number from N, keeping track of the remainder.
- If the number subtracted was the ith Fibonacci number F(i), put a 1 in place i in the code word (counting the right most digit as place 0).
- Repeat the previous steps, substituting the remainder for N, until a remainder of 0 is reached.
Example with 10:
Fib = [1,2,3,5,8,13]
|8|7|6|5|4|3|2|1|0|
First let's fill with zero: 10 ~= 0 0 0 0 0 0 0 0 0
Then closest Fibonacci number equal or smaller to 10 is 8. 8 === F(4) so
|9|8|7|6|5|4|3|2|1|
10 ~= 0 0 0 0 0 1 0 0 0
Now 10 - 8 === 2
2 is a Fibonacci number 2 === F(1) then
|8|7|6|5|4|3|2|1|0|
10 == 0 0 0 0 1 0 0 1 0
2 - 2 === 0 so we are finished:
|4|3|2|1|0|
10 === 1 0 0 1 0
Based on this representation we know that the number 10 would be on the card index number 1 and 4. You just need to apply this method to all numbers between 0 and n.
Good Luck, Don't forget to up vote if you like, thanks
IVBakker
Similar Kata:
Stats:
Created | Dec 19, 2014 |
Warriors Trained | 21 |
Total Skips | 0 |
Total Code Submissions | 34 |
Total Times Completed | 5 |
JavaScript Completions | 5 |
Total Stars | 0 |
% of votes with a positive feedback rating | 0% of 0 |
Total "Very Satisfied" Votes | 0 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 2 |