22
2
When you convert a fraction to a decimal number and you want to store that number, you often have to round it, because you only want to use a certain amount of memory. Let's say you can only store 5 decimal digits, then 5/3 becomes 1.6667. If you can store only 2 decimal digits it will be 1.7 (now assuming that it is always between 0 and 9.99...).
If you now try to reverse that process with 1.7 and you want to get your fraction back, that can be difficult, since you know that 1.7 is only a rounded number. Of course you can try 17/10 but that is rather an 'ugly' fraction compared to the 'elegant' 5/3.
So the goal is now finding the fraction a/b with the least denominator b, that results in the rounded decimal number when correctly rounded.
Details
The input contains a string with a number of 1 up to 5 digits that is between 0 (including) and 10 (not including) with a '.' after the first digit. Let's say n
denotes the number of digits. The output must be a list/array of two integers [numerator, denominator]
or a rational datatype (you can create your own or use built-in) where the numerator is non-negative and the denominator is positive. The fraction numerator/denominator must be equal to the input when correctly rounded to n
digits (which means n-1
digits after the decimal point).
Restriction: only one loop statement allowed. This means that you can use only one single looping statement (like for
or while
or goto
etc. as well as functional loops like map
or fold
that apply code to every element of a list/array) in your whole code, but you are free to 'abuse' it or use recursion etc.
You should write a function. If your language does not have functions (or even if it does), you can alternatively assume that the input is stored in a variable (or input via stdin) and print the result or write it to a file. The lowest number of bytes wins.
Rounding
The rounding should follow the 'conventional' rounding rules, i.e. if the last digit that will be cut off is 5 or greater, you will round up, and you will round down for any other cases, so e.g:
4.5494 will result when rounding to
- 1 digit: 5
- 2 digits: 4.5
- 3 digits: 4.55
- 4 digits: 4.549
Examples
Please include following test cases and other 'interesting' ones:
Input 1.7 Output 5/3
Input 0. Output 0/1
Input 0.001 Output 1/667
Input 3.1416 Output 355/113
1Do implicit loops count? In array languages like J or APL, each verb introduces an implicit loop. If implicit loops counted, this would be impossible to do in APL. – FUZxxl – 2015-03-20T23:57:29.410
Thank you for these thougths: Since I am not used to both I ask you: What would you recommend? I think I have to allow functions like
map
etc but I'd disallow the output of a rational type. @ rounding: I will specify this, thanks. – flawr – 2014-09-23T12:49:14.143Thank you for the assistance, I think that should make sense now. Do you think that is an acceptable 'definition' I wrote down of functions like
map
? – flawr – 2014-09-23T13:01:22.600Yes, sounds good. Alternatively "... that apply a named or anonymous function repeatedly." E.g. Mathematica has
Nest
, which isn't applied to a list, but still creates a loop. – Martin Ender – 2014-09-23T13:02:09.8631But in functional languages there is no such a thing as a loop. Foe example in haskell
repeat
creates an infinite list of its argument. I t seems to loop but it actually has time complexity of O(1). But i guess sorting each case individually is better than not allowing functional languages. – proud haskeller – 2014-09-23T15:44:30.6733I don't like the current definition of "loop". In Python, for example,
for n in numbers: f(g(n))
is equivalent tomap(f, map(g, numbers))
. The functional version usesmap
twice, should that really be disallowed? – flornquake – 2014-09-23T16:19:29.153maybe have a bonus for no loops at all? – proud haskeller – 2014-09-23T16:20:54.480
@proudhaskeller As I read it, functional loops aren't disallowed, but are allowed exactly once like any other loop. – Martin Ender – 2014-09-23T16:36:06.130
1@MartinBüttner I talked about the case that functional languages would be disallowed because of ambiguity – proud haskeller – 2014-09-23T16:37:30.547
1I am sorry that I cannot really contribute to that discussion since my knowledge about functional programming is basically zero. If you have a solution of which you are unsure about whether it complies with the 'rules' please submit it anyway! In the end it is supposed to be a fun and educational challenge! – flawr – 2014-09-23T20:51:12.863
1I'm curious: Why does it have to be a function? Why can't we submit programs? – Dennis – 2014-09-24T02:58:39.257
2@Dennis No that was unfortunate wording, you can submit it in any form you like, the main idea behind that paragraph was that you should not have a disadvantage if your language takes more bytes for 'reading' the input number. – flawr – 2014-09-24T11:55:18.087
@proudhaskeller I even thought about that in the first place but was too lazy to figure out a way of making that bonus happen. I suggest that you can pat yourself on your shoulder twice if you make it without any loops? =) – flawr – 2014-09-24T11:57:00.593
@flawr I did infact – proud haskeller – 2014-09-24T12:13:05.777