Monday Mini-Golf: A series of short code-golf challenges, posted (hopefully!) every Monday.
Sorry it's late; I realized 90% of the way through writing out a different idea that it was a duplicate.
My family is rather large, so we eat a lot of food. We usually need to double, triple, or even quadruple recipes to make enough food! But as multiplying the measurements can be a pain, it'd be nice to have a program to do this for us.
Challenge
Your challenge is to create a program or function that takes in a measurement as a number N and a letter L, and returns the same measurement, simplified as much as possible. Here's the required measurement units (all are American, like my family), and their corresponding letters:
1 cup (c) = 16 tablespoons (T) = 48 teaspoons (t)
1 pound (l) = 16 ounces (o)
1 gallon (g) = 4 quarts (q) = 8 pints (p) = 128 fluid ounces (f)
"simplified as much as possible" means:
- Using the largest measurement unit possible. Each unit can have a remainder of 1/4, 1/3, 1/2, 2/3, or 3/4.
- Turning the result into a mixed number, if necessary.
For example, 4 o
is four ounces, which becomes 1/4 l
, a quarter pound. 8 t
, 8 teaspoons, becomes 2 2/3 T
.
Details
- The input may be taken in any reasonable format; same with output. (
1 t
,1,"t"
,1\nt
, etc.) - Make sure any fractional part is dealt with properly. (
11/4
in place of1 1/4
is not allowed.) - The number will always be a mixed number, and will always have a denominator of
2
,3
, or4
(or none). (no1 1/8 T
, no1.5 T
, etc.) - As a result of the above, no downward conversions (e.g. cups to tablespoons) are ever needed.
- The letter will always be one of the letters listed above (
Tcfglopqt
).
Test-cases
Here's an large list, hopefully covering all types of cases:
Input | Output
--------+--------
1/2 t | 1/2 t
3/4 t | 1/4 T
1 t | 1/3 T
1 1/2 t | 1/2 T
2 t | 2/3 T
2 1/4 t | 3/4 T
2 1/2 t | 2 1/2 t
3 t | 1 T
10 t | 3 1/3 T
16 t | 1/3 c
5 1/3 T | 1/3 c
8 T | 1/2 c
16 T | 1 c
36 T | 2 1/4 c
1/4 c | 1/4 c
1024 c | 1024 c
1 o | 1 o
4 o | 1/4 l
5 1/3 o | 1/3 l
5 2/3 o | 5 2/3 o
8 o | 1/2 l
28 o | 1 3/4 l
28 l | 28 l
2 f | 2 f
4 f | 1/4 p
8 f | 1/4 q
16 f | 1/2 q
32 f | 1/4 g
64 f | 1/2 g
128 f | 1 g
2/3 p | 1/3 q
1 1/3 p | 2/3 q
2 p | 1/4 g
1 q | 1/4 g
Scoring
Our kitchen is very small, so the code should be as short as possible, so as not to make the kitchen more cramped. Shortest valid code in bytes wins; tiebreaker goes to submission that reached its final byte count first. The winner will be chosen next Monday, Nov 9. Good luck!
Please note that this challenge is similar to, but not a duplicate of, World Big Dosa.