21
0
This is inspired by a real world problem I had. I'm curious to see if there is any clever way to go about this.
You are given two unsorted arrays, A and B, each containing an arbitrary number of floats. A and B don't necessarily have the same lengths. Write a function that takes the elements of A sequentially and finds the nearest value in array B. The result has to be contained in a new array.
Win condition
Shortest code wins (as usual).
1Round to the nearest integer? – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2014-06-11T14:10:51.627
1@n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ I read that as "round each element of A to the nearest element of B" – John Dvorak – 2014-06-11T14:14:01.553
@JanDvorak: Well, I understand the part about rounding direction, but the problem didn't specify to how many digits. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2014-06-11T14:15:56.537
@n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Round to nearest float. The answer has to output floats from array/list B. – Orhym – 2014-06-11T14:16:28.973
For example, given A = [38.56] and B = [40.59], I can round it to 38.6 or 39 or 40 depending on the number of significant digits. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2014-06-11T14:19:16.063
@n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ it is possible to do exactly. I assume you have to. – John Dvorak – 2014-06-11T14:20:06.377
You have to round it to 40.59. It's not so much of a rounding operation as it is to find a closest match. – Orhym – 2014-06-11T14:20:24.323
Then just redefine the problem to "For each number in array A, find a number from array B that is nearest to the given number". It is much clearer than using the term "rounding". – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2014-06-11T14:21:20.457
1Will arrays A and B be sorted? – Level River St – 2014-06-11T14:21:56.840
@n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ "map", not "print" – John Dvorak – 2014-06-11T14:22:11.283
@steveverrill will it help you if they are? – John Dvorak – 2014-06-11T14:22:35.067
@JanDvorak I would certainly think it would, though I haven't analysed the problem fully. So I think it's a valid question. – Level River St – 2014-06-11T14:25:41.330
@steveverrill I'd be curious to see if it can lead to a shorter solution, because in the practical case, they are. For this problem, I'm not sure if it'd be fair to change it now. Let's assume they are not to stay more general. But it'd be very nice if you provided both solutions. – Orhym – 2014-06-11T14:25:53.627
@Orhym good decision. I put it in the question. I see you're new round here. With code golf it is very important to specify clearly, and you've done very well for a new user. You'd be amazed how many questions we get from new users that get closed due to vague specification. – Level River St – 2014-06-11T14:35:14.520
Not sure you'll get anything "clever" from code golf because most are going to go about it in the most straight-forward (less code) way possible (for each element in a, loop through b to find min difference and select). If B is sorted then finding that min difference can be improved, but at the expense of code length (so no one will post it). – DreamWarrior – 2014-06-11T14:48:27.787
Shortest code is not always the best code! I would recommend you post the problem statement and your way of solving the problem (with your code) on Code Review and ask for a review of your code. There has been some questions in the past about arrays there but I don't think there's been this one.
– Simon Forsberg – 2014-06-13T08:34:05.747