Cody

# Problem 29. Nearest Numbers

Solution 941518

Submitted on 24 Aug 2016 by steph26
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
A = [30 46 16 -46 35 44 18 26 25 -10]; correct = [8 9]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 30 46 16 -46 35 44 18 26 25 -10 distA = 0 16 14 76 5 14 12 4 5 40 16 0 30 92 11 2 28 20 21 56 14 30 0 62 19 28 2 10 9 26 76 92 62 0 81 90 64 72 71 36 5 11 19 81 0 9 17 9 10 45 14 2 28 90 9 0 26 18 19 54 12 28 2 64 17 26 0 8 7 28 4 20 10 72 9 18 8 0 1 36 5 21 9 71 10 19 7 1 0 35 40 56 26 36 45 54 28 36 35 0

2   Pass
A = [1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1949 -1829]; correct = [3 9]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = Columns 1 through 8 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1555 -3288 2061 -4681 -2230 -4538 -4028 3235 Columns 9 through 10 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 1949 -1829 distA = Columns 1 through 8 0 4843 506 6236 3785 6093 5583 1680 4843 0 5349 1393 1058 1250 740 6523 506 5349 0 6742 4291 6599 6089 1174 6236 1393 6742 0 2451 143 653 7916 3785 1058 4291 2451 0 2308 1798 5465 6093 1250 6599 143 2308 0 510 7773 5583 740 6089 653 1798 510 0 7263 1680 6523 1174 7916 5465 7773 7263 0 394 5237 112 6630 4179 6487 5977 1286 3384 1459 3890 2852 401 2709 2199 5064 Columns 9 through 10 394 3384 5237 1459 112 3890 6630 2852 4179 401 6487 2709 5977 2199 1286 5064 0 3778 3778 0

3   Pass
A = [-1 1 10 -10]; correct = [1 2]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = -1 1 10 -10 -1 1 10 -10 -1 1 10 -10 -1 1 10 -10 distA = 0 2 11 9 2 0 9 11 11 9 0 20 9 11 20 0

4   Pass
A = [0 1000 -2000 1001 0]; correct = [1 5]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = 0 1000 -2000 1001 0 0 1000 -2000 1001 0 0 1000 -2000 1001 0 0 1000 -2000 1001 0 0 1000 -2000 1001 0 distA = 0 1000 2000 1001 0 1000 0 3000 1 1000 2000 3000 0 3001 2000 1001 1 3001 0 1001 0 1000 2000 1001 0

5   Pass
A = [1:1000 0.5]; correct = [1 1001]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = 1.0e+03 * Columns 1 through 9 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0010 0.0020 0.0030 0.0...

6   Pass
% Area codes A = [847 217 508 312 212]; correct = [2 5]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = 847 217 508 312 212 847 217 508 312 212 847 217 508 312 212 847 217 508 312 212 847 217 508 312 212 distA = 0 630 339 535 635 630 0 291 95 5 339 291 0 196 296 535 95 196 0 100 635 5 296 100 0

7   Pass
% Zip codes A = [60048 61802 01702 60601 10001]; correct = [1 4]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

tmp = 60048 61802 1702 60601 10001 60048 61802 1702 60601 10001 60048 61802 1702 60601 10001 60048 61802 1702 60601 10001 60048 61802 1702 60601 10001 distA = 0 1754 58346 553 50047 1754 0 60100 1201 51801 58346 60100 0 58899 8299 553 1201 58899 0 50600 50047 51801 8299 50600 0