Many times throughout the day can represent mathematical equations. In this problem, we focus on times that include the four basic operations (+,-,*,/). For example, 6:17 can be written as 6=1+7. Write a function that determines if the given time (restricted to three digits in 12-hour time, 1:00 to 9:59) is an equation time, and if so, which basic operation it uses. There are also four types of equations that are categorized here, and a given time can fit more than one category:
- equation written forward, "=" doesn't coincide with ":" --> add 1 to output (e.g., 2:35, 2+3=5)
- equation written forward, "=" does coincide with ":" -- > add 100 to output (e.g., 2:53, 2=5-3)
- equation written backward, "=" doesn't coincide with ":" --> add 10 to output (e.g., 3:26, 6=2*3)
- equation written backward, "=" does coincide with ":" --> add 1000 to output (e.g., 4:28, 8/2=4)
Note that some of these combinations are tied to each other due to the commutative nature of + and * and the inverse relation of +,- and ,/. The output should be a 4x2 matrix with 0s or 1s in the first column dependent on whether each operation (+,-,*,/) is applicable to a given time and the totals in the second column. Examples include:
4:22 | out = [1 1100; 1 1; 1 1100; 1 1]; since 4=2+2, 2+2=4; 4-2=2; 4=2*2, 2*2=4; 4/2=2.
5:15 | out = [0 0; 0 0; 1 1111; 1 1001]; since 5*1=5, 5=1*5, 5*1=5, 5=1*5; 5/1=5, 5/1=5.
This problem is related to Problem 2431 and Problem 2433.
3778 Solvers
1735 Solvers
The Hitchhiker's Guide to MATLAB
2656 Solvers
Back to basics 8 - Matrix Diagonals
692 Solvers
Split bread like the Pharaohs - Egyptian fractions and greedy algorithm
31 Solvers
Solution 554926
If my over-use of str2num has in any way caused offense, I hereby beg leave to apologize.
My hat's off to you for such a solution. I'm baffled as to how to decode it (pun intended). I know that there has been a lot of furor over the use of regexp and other string-based functions, conversions, etc. to lower solution sizes for problems in general, but this problem is string-based, so I see no problem in solving it via arcane string methods.