kdb programming challenge – Fibonacci Clock

AquaQ Admin kdb+ 4 Comments

Fibonacci is a nut shell!

Fibonacci in a nut shell!

We all know what the Fibonacci Sequence is. It is basically the summation of last 2 numbers of a series starting from 1 (i.e. 1 1 2 3 5 8 13 ….). It exists in nature and can be seen in a sunflower head or in a snail shell as shown above. It is also related to the famous golden ratio(phi)! But how obsessed are you with Fibonacci?? Apparently someone was so obsessed with the sequence that he went and made a clock out of it, Fibonacci Clock. Amazing isn’t it?? It’s hard enough to read a clock face when you are in a rush and late for work but with this clock, it is even more challenging! You need to do the math to get the time!

So as nerdy as I am, today we will have 2 challenges (wow 2 challenges in one day? so awsome!!). Well here are the challenges:

1) Tell the time of Fibonacci Clock

This is pretty simple, instead of using our brain to tell the time, why not write some q code that does that for you?? So write a function that takes a string input and return the time.
The string input is delimited by space, each items are digit follow by a colour code (R – Red, B – Blue, G – Green).
Results should be in 12-hour format.

fibo2


q)f"5R 3B 2G 1R"
09:25
q)f"5B 3G 1G"
05:45
q)f"5R 2R"
07:00

2) Create Fibonacci Time

So this is a bit tricky, create a function that takes time as input and return string form Fibonacci time.
Note that the author of the article said, there are different combinations of Fibonacci time for a certain time!
Your function must be able to return all those possible combinations. Since the clock can only display the time in 5 min intervals, you will need to round the time to its nearest 5 min.
Also, the clock’s time is 12-hour format instead of 24-hour format. So 01:00 is essentially the same as 13:00.


q)g 06:30
"5B 1B"
"5B 1R 1G"
"5R 3G 2G 1B"
"5R 3G 2G 1R 1G"
"5G 3R 2R 1B"
"5G 3R 2R 1R 1G"
"3B 2B 1B"
"3B 2B 1R 1G"

Personal Remark

I find this creation very intriguing and worth the praise. As a math and art lover, this clock is quite original and creative. I want to thank Philippe Chrétien for inspiring me to write this challenge and wish him all the best with the project!

AquaQ Adminkdb programming challenge – Fibonacci Clock

Comments 4

  1. Akash

    getTime:{[n]{(minute$60*x["R"]+x["B"])+minute$5*x[“G”]+x[“B”]} 0^”RBG”#sum each (“I”$’l[0]) group (l:flip ” ” vs n)[1]}

    createFibonacciTime:{{[l;h;m]list:5 3 2 1 1 0;" " sv/: distinct desc each {x where not x like "*W"}each raze each' string distinct -1_' list,'/: l where {[l;list;h;m](h=sum raze list a`R`B)& (m div 5)= sum raze list (a:(`R`B`G`W)#group l)`B`G}[;list;h;m] each l}[(cross/)6#enlist `R`B`G`W;] . "I"$'":" vs x}

    1. Akash

      issue with the first one.

      q)getTime:{[n]{(`minute$60*x["R"]+x["B"])+`minute$5*x["G"]+x["B"]} 0^"RBG"#sum each ("I"$'l[0]) group (l:flip " " vs n)[1]}

  2. Jonathan Wardell Avery

    f:{{g:{sum{-7h$x[0]}each y where{y[1] in x}[x]each y};(-1_.Q.s g[“RB”;x]),”:”,({((2-count x)#”0″),x}[-1_.Q.s 5*g[“BG”;x]])}” “vs x}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax