13
1
In-between fractions
The challenge:
You will need to create code that takes atleast 3 inputs; 2 integers and "a fraction representation" - whichever type suits your language for representing the fraction increments) ie. If you choose string the input would be "1/4" or you could choose 2 extra integer inputs or a tuple or w/e.
Input can be anywhere reasonable (STDIN, function arguments, from a file, etc.), and so can output (STDOUT, function return value, to a file, etc.)
Rules:
- The input "fraction" will always be a valid fraction, less than 1; example "1/4"
- The second input integer will always have a higher value than the first integer. I.E the first input integer will always have a lower value than the second.
- The input integers can be negative.
- Outputted fractions should be reduced as much as possible (simplified)
The code will need to output every "fraction step" between the 2 numbers in increments of the input fraction.
The code should be a program or function as stated here
Example 1:
Input: -2,3,"1/2"
Output:
-2
-3/2
-1
-1/2
0
1/2
1
3/2
2
5/2
3
Example 2:
Input: 1,2,"2/3"
Output:
1
5/3
2
or
1
4/3
2
Note: Counting can start from either direction (thank you @Mego)
This is code-golf, so the shortest answer in bytes wins.
Could the fraction be taken as 2 integer inputs, making 4 total inputs? – Mego – 2016-04-15T07:40:54.380
I think ill keep the restraint of maximum of 3 inputs - I would like to see the code for 4 inputs aswell – Alex Carlsen – 2016-04-15T07:42:58.403
In that case, what about having a list/tuple/array/some other iterable type containing two integers for the third input? That's not fundamentally different than 4 integer inputs. You should also clarify that the fraction will not be equal to 0. – Mego – 2016-04-15T07:44:52.210
@Mego After thinking it through, I can't see why it shouldn't be allowed Changed to "code that takes atleast 3 inputs" – Alex Carlsen – 2016-04-15T07:48:57.500
It seems that the rule 4 is the only critical point (for languages that don't have a native fraction or rational type) – edc65 – 2016-04-15T08:25:12.597
@edc65 I agree... I thought it to be harder tbh. (next ill create a similar challenge, but restrict answers to not rely on internal fraction features of certain languages :) - on the other hand, it's nice not to see a plethora of cjam answers < 3 bytes :) – Alex Carlsen – 2016-04-15T08:27:46.813
Can you clarify what constitutes "a fraction representation"? I took this to mean two distinct values expressing the numerator and denominator, but would this include a numeric expression, such as
btw(-2, 3, 1/2)
where1/2
automatically evaluates to0.5
? (Even if this is the case I would still expect output to benum / den
.) – beaker – 2016-04-15T17:45:10.107Is
inbetween
supposed to be 2 words,in between
? I usually see it separated. – Rɪᴋᴇʀ – 2016-04-16T07:20:11.2071@beaker as long as the output is correct and the input comes from atleast 2 integers, the rest is up to you :) - i've kept the input part pretty openended, to see different answers – Alex Carlsen – 2016-04-18T19:25:14.073
You should change the first example input to be
-2,3,"3/6"
– mbomb007 – 2016-04-19T19:45:07.373Can
c
be asymbolic
variable? That is a data type used for symbolic computations – Luis Mendo – 2016-05-09T14:10:19.937