This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Intermediate Division 2021-2022 Contest 1 Programming Problem Fibonacci Clock.

Fibonacci Clock problem
The Fibonacci Clock problem was the programming problem in ACSL Intermediate Division 2021-2022 Contest 1.
First, read the Fibonacci Clock problem. Make sure you understand the problem description. Read it many times, if necessary. Then, write your algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a flowchart.
Breaking down the problem
From the problem description, the input contains 5 characters in sequence. The value of each character contains the corresponding fibonacci number starting with 1 and 1.
So, the 5 characters will have values 1,1, 2, 3, 5.
Looking at the first input string, we have:
| position index | 0 | 1 | 2 | 3 | 4 |
| characters | R | W | G | B | G |
| values | 1 | 1 | 2 | 3 | 5 |
We can represent the fibonacci sequence by saving them in an array [1, 1, 2, 3, 5].
For hours and minutes, we have: - Red and Blue squares represent Hours. - Green and Blue squares represent Minutes.
Loop through each character in the 5-character input.
For each character, find whether it is an hour or minute or both, and add to the appropriate value.
Finally, when you get the value for minutes, check if it is 60 or more. If it is, then, subtract 60 from the minutes value and add 1 to the hours value.
Return the value of hours and minutes in the format HH:MM with a zero padding if either of them has a single digit.
Sample Input
Save this sample input as a file fibonacci_clock_1.in
R W G B G
W B B G R
W G B R B
G G B B B
W R G G G
Sample Output
Save this sample output as a file fibonacci_clock_1.out
04:50
08:30
10:40
11:00
01:50
My Python solution
This is my working Python solution. Save it as a file fibonacci_clock.py.
'''
ACSL Senior Division 2021-2022 Contest 1 - FIBONACCI CLOCK
Blog Post: https://aruljohn.com/blog/acsl-intermediate-fibonacci-clock/
'''
def fibonacci_clock(chars=None):
time = None
# The squares will have the values of 1, 1, 2, 3, 5
square_values = [1, 1, 2, 3, 5]
# Initialize values of hours and minutes
hours = 0
minutes = 0
# Loop through the 5 input characters
for i, ch in enumerate(chars):
# If the current character is an R or B, add Fib value to hours
if 'R' in ch or 'B' in ch:
hours += square_values[i]
# If the current character is an G or B, add Fib value to minutes
if 'G' in ch or 'B' in ch:
minutes += square_values[i]
# Minutes value should be multiplied by 5
minutes *= 5
# If total value of minutes is equal to or greater than 60,
# increase hour by 1 and subtract 60 from minutes
if minutes >= 60:
minutes -= 60
hours += 1
# Format the return output as a string of hh:mm with 0 padding for single digits
time = f'{hours:02}:{minutes:02}'
return time
# Main function here
if __name__ == '__main__':
for _ in range(5):
chars = input().split(' ')
print(fibonacci_clock(chars))
Method 1: How to run the program with diff and cat
If you are running this program on Linux or macOS, you can use regular Unix/Linux commands.
$ diff <(cat fibonacci_clock_1.in | python fibonacci_clock.py) <(cat fibonacci_clock_1.out)
Method 2: How to run the program the regular way
The regular way to run this program is:
python fibonacci_clock.py
Copy/paste the input lines and press ENTER or return.
R W G B G W B B G R W G B R B G G B B B W R G G G
Screenshot
$ python fibonacci_clock.py
R W G B G
W B B G R
W G B R B
G G B B B
W R G G G
04:50
08:30
10:40
11:00
01:50
Method 3: How to run the program using cat on the input file
On my Mac, I personally prefer running cat on the input file and pipe the output to the Python program.
cat fibonacci_clock_1.in | python fibonacci_clock.py
Screenshot
$ cat fibonacci_clock_1.in | python fibonacci_clock.py
04:50
08:30
10:40
11:00
01:50
Compare this output with the contents of fibonacci_clock_1.out. They should be identical.
Decoding the program
In the fibonacci_clock(chars=None) function, we first create a list with the first 5 Fibonacci numbers with starting points 1 and 1. We know that only the first 5 Fibonacci numvbers are being used for the 5-character input, so we will hardcode them.
After initializing hours and minutes, we loop through each character in the input.
In the loop, we check if the character is R or B, and if yes, we add the value to hours.
In the loop, if the character is B or G, we add the value to minutes.
After the loop finishes, we update the value of minutes to 5 times itself.
If the value of minutes is equal to or greater than 60, subtract 60 from minutes, and add 1 to hours. That gives us the most accurate time.
At the end, create a string with the hours and minutes in the hh:mm format, making sure that single digits are padded with 0s using Python's f-string f'{x:02}
Use the Python shell
Feel free to test all your Python snippets in the Python shell.
Combine common code and DRY
Wherever possible, see if you can combine common code that repeats itself. DRY stands for don't repeat yourself. You can either use repeating code in a function with appropriate parameters. If you don't have time, or it's too late to make a clean fix, just leave it as it is, as long as it is working properly.
Conclusion
If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.