14
2
Write a program or a function that takes two non-empty lists of the same length as input and does the following:
- uses elements of first list to get numerators,
- uses elements of the second list to get denominators,
- displays resulting fractions after simplification
(2/4=>1/2)
, separated by "+"s, - displays "=" and result of addition after last fraction.
Example:
Input
[1, 2, 3, 3, 6]
[2, 9, 3, 2, 4]
Output
1/2+2/9+1+3/2+3/2=85/18
About rules
- elements of lists will be positive integers,
- elements can be separated by spaces, eg:
1/2 + 2/9 + 1 + 3/2 + 3/2 = 85/18
is ok, - trailing newline is allowed,
- lists can be taken in other formats than above, eg.:
(1 2 3 3 6)
or{1;2;3;3;6}
, etc., 1
can be expressed as1/1
,- instead of printing you can return appropriate string,
- you do not need to handle wrong input,
- shortest code wins.
What range of values does it have to support? – Brad Gilbert b2gills – 2017-05-19T12:26:48.120
@BradGilbertb2gills I would say at least -30 000 to 30 000, but then I don't know whether it would be extra problem for some languages. So maybe just standard integer range of your language of choice. – None – 2017-05-19T12:36:19.370
@PrzemysławP saying "standard integer range of your language of choice" is not a good idea, some languages have standard integer as booleans – Felipe Nardi Batista – 2017-05-19T12:37:39.080
Thank you! @BradGilbertb2gills Then at least -30 000 to 30 000. – None – 2017-05-19T12:40:18.133
Can we get fractions as
[1, 2] [2, 9] [3, 3] ...
instead? – Olivier Grégoire – 2017-05-19T12:42:19.527@OlivierGrégoire No, sorry! – None – 2017-05-19T12:42:54.450
The standard integer range for Perl 6 is infinite, but the default range for denominators of Rationals is
uint64.Range.max
. So I was asking if I had to convert them all to FatRats instead. – Brad Gilbert b2gills – 2017-05-19T12:49:06.763@BradGilbertb2gills Sorry if I caused confusion - my second comment applies: -30 000 to 30 000. – None – 2017-05-19T13:02:43.610
can i concatenate the both lists as
[1, 2, 3, 3, 6, 2, 9, 3, 2, 4]
? – Felipe Nardi Batista – 2017-05-19T13:12:09.457@FelipeNardiBatista The input should be taken as two lists. After that you can obviously concatenate them. – None – 2017-05-19T15:11:03.130
Are the inputs going to be positive? – Dennis – 2017-05-19T19:58:05.517
@Dennis Not always. – None – 2017-05-19T20:15:12.420
That should be reflected in the test cases. – Dennis – 2017-05-19T20:17:21.273
@Dennis You are right. I'll add one more test case. – None – 2017-05-19T20:21:03.263
Can we assume the lists will always have at least two elements each? – Scott Milner – 2017-05-19T20:55:38.327
@ScottMilner Yes. – None – 2017-05-19T20:56:39.450
@Dennis It looks like pretty much every solution assumed it's always positive integers so I think it's better to say that input should be only positive integer. Sorry for confusion:/ – None – 2017-05-19T21:12:07.903
@BradGilbertb2gills Sorry again, but seeing that many people assumed input should be positive integers I think it's better to say that minimal supported range would be 1-30 000. Again, sorry for confusion:/ – None – 2017-05-19T21:15:45.247