Cody

# Problem 44952. Find MPG of Lightest Cars

Solution 2226515

Submitted on 21 Apr 2020
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   Fail
N = 5 load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat')); Model = strtrim(string(Model)); cars = table(Model, MPG, Horsepower, Weight, Acceleration); save cars.mat cars assert(isequal(sort_cars(N),[35; 31; 39.1; 35.1; 31]));

N = 5 cars = 406x1 table <strong>cars</strong> <strong>Model</strong> <strong>MPG</strong> <strong>Horsepower</strong> <strong>Weight</strong> <strong>Acceleration</strong> <strong>______________________________________________________________________________________</strong> "chevrolet chevelle malibu" 18 130 3504 12 "buick skylark 320" 15 165 3693 11.5 "plymouth satellite" 18 150 3436 11 "amc rebel sst" 16 150 3433 12 "ford torino" 17 140 3449 10.5 "ford galaxie 500" 15 198 4341 10 "chevrolet impala" 14 220 4354 9 "plymouth fury iii" 14 215 4312 8.5 "pontiac catalina" 14 225 4425 10 "amc ambassador dpl" 15 190 3850 8.5 "citroen ds-21 pallas" NaN 115 3090 17.5 "chevrolet chevelle concours (sw)" NaN 165 4142 11.5 "ford torino (sw)" NaN 153 4034 11 "plymouth satellite (sw)" NaN 175 4166 10.5 "amc rebel sst (sw)" NaN 175 3850 11 "dodge challenger se" 15 170 3563 10 "plymouth 'cuda 340" 14 160 3609 8 "ford mustang boss 302" NaN 140 3353 8 "chevrolet monte carlo" 15 150 3761 9.5 "buick estate wagon (sw)" 14 225 3086 10 "toyota corona mark ii" 24 95 2372 15 "plymouth duster" 22 95 2833 15.5 "amc hornet" 18 97 2774 15.5 "ford maverick" 21 85 2587 16 "datsun pl510" 27 88 2130 14.5 "volkswagen 1131 deluxe sedan" 26 46 1835 20.5 "peugeot 504" 25 87 2672 17.5 "audi 100 ls" 24 90 2430 14.5 "saab 99e" 25 95 2375 17.5 "bmw 2002" 26 113 2234 12.5 "amc gremlin" 21 90 2648 15 "ford f250" 10 215 4615 14 "chevrolet c20" 10 200 4376 15 "dodge d200" 11 210 4382 13.5 "ih 1200d" 9 193 4732 18.5 "datsun pl510" 27 88 2130 14.5 "chevrolet vega 2300" 28 90 2264 15.5 "toyota corona" 25 95 2228 14 "ford pinto" 25 NaN 2046 19 "volkswagen super beetle 117" NaN 48 1978 20 "amc gremlin" 19 100 2634 13 "plymouth satellite custom" 16 105 3439 15.5 "chevrolet chevelle malibu" 17 100 3329 15.5 "ford torino 500" 19 88 3302 15.5 "amc matador" 18 100 3288 15.5 "chevrolet impala" 14 165 4209 12 "pontiac catalina brougham" 14 175 4464 11.5 "ford galaxie 500" 14 153 4154 13.5 "plymouth fury iii" 14 150 4096 13 "dodge monaco (sw)" 12 180 4955 11.5 "ford country squire (sw)" 13 170 4746 12 "pontiac safari (sw)" 13 175 5140 12 "amc hornet sportabout (sw)" 18 110 2962 13.5 "chevrolet vega (sw)" 22 72 2408 19 "pontiac firebird" 19 100 3282 15 "ford mustang" 18 88 3139 14.5 "mercury capri 2000" 23 86 2220 14 "opel 1900" 28 90 2123 14 "peugeot 304" 30 70 2074 19.5 "fiat 124b" 30 76 2065 14.5 "toyota corolla 1200" 31 65 1773 19 "datsun 1200" 35 69 1613 18 "volkswagen model 111" 27 60 1834 19 "plymouth cricket" 26 70 1955 20.5 "toyota corona hardtop" 24 95 2278 15.5 "dodge colt hardtop" 25 80 2126 17 "volkswagen type 3" 23 54 2254 23.5 "chevrolet vega" 20 90 2408 19.5 "ford pinto runabout" 21 86 2226 16.5 "chevrolet impala" 13 165 4274 12 "pontiac catalina" 14 175 4385 12 "plymouth fury iii" 15 150 4135 13.5 "ford galaxie 500" 14 153 4129 13 "amc ambassador sst" 17 150 3672 11.5 "mercury marquis" 11 208 4633 11 "buick lesabre custom" 13 155 4502 13.5 "oldsmobile delta 88 royale" 12 160 4456 13.5 "chrysler newport royal" 13 190 4422 12.5 "mazda rx2 coupe" 19 97 2330 13.5 "amc matador (sw)" 15 150 3892 12.5 "chevrolet chevelle concours (sw)" 13 130 4098 14 "ford gran torino (sw)" 13 140 4294 16 "plymouth satellite custom (sw)" 14 150 4077 14 "volvo 145e (sw)" 18 112 2933 14.5 "volkswagen 411 (sw)" 22 76 2511 18 "peugeot 504 (sw)" 21 87 2979 19.5 "renault 12 (sw)" 26 69 2189 18 "ford pinto (sw)" 22 86 2395 16 "datsun 510 (sw)" 28 92 2288 17 "toyota corona mark ii (sw)" 23 97 2506 14.5 "dodge colt (sw)" 28 80 2164 15 "toyota corolla 1600 (sw)" 27 88 2100 16.5 "buick century 350" 13 175 4100 13 "amc matador" 14 150 3672 11.5 "chevrolet malibu" 13 145 3988 13 "ford gran torino" 14 137 4042 14.5 "dodge coronet custom" 15 150 3777 12.5 "mercury marquis brougham" 12 198 4952 11.5 "chevrolet caprice classic" 13 150 4464 12 "ford ltd" 13 158 4363 13 "plymouth fury gran sedan" 14 150 4237 14.5 "chrysler new yorker brougham" 13 215 4735 11 "buick electra 225 custom" 12 225 4951 11 "amc ambassador brougham" 13 175 3821 11 "plymouth valiant" 18 105 3121 16.5 "chevrolet ...

Error using tabular/sortrows (line 57) Variable index exceeds table dimensions. Error in sort_cars (line 4) B = sortrows(cars,2) Error in Test1 (line 6) assert(isequal(sort_cars(N),[35; 31; 39.1; 35.1; 31]));

2   Fail
N = 6 load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat')); Model = strtrim(string(Model)); cars = table(Model, MPG, Horsepower, Weight, Acceleration); save cars.mat cars assert(isequal(sort_cars(N),[33; 29.5; 26; 29; 38; 32]));

N = 6 cars = 100x1 table <strong>cars</strong> <strong>Model</strong> <strong>MPG</strong> <strong>Horsepower</strong> <strong>Weight</strong> <strong>Acceleration</strong> <strong>___________________________________________________________________________________</strong> "chevrolet chevelle malibu" 18 130 3504 12 "buick skylark 320" 15 165 3693 11.5 "plymouth satellite" 18 150 3436 11 "amc rebel sst" 16 150 3433 12 "ford torino" 17 140 3449 10.5 "ford galaxie 500" 15 198 4341 10 "chevrolet impala" 14 220 4354 9 "plymouth fury iii" 14 215 4312 8.5 "pontiac catalina" 14 225 4425 10 "amc ambassador dpl" 15 190 3850 8.5 "citroen ds-21 pallas" NaN 115 3090 17.5 "chevrolet chevelle concours (sw)" NaN 165 4142 11.5 "ford torino (sw)" NaN 153 4034 11 "plymouth satellite (sw)" NaN 175 4166 10.5 "amc rebel sst (sw)" NaN 175 3850 11 "dodge challenger se" 15 170 3563 10 "plymouth 'cuda 340" 14 160 3609 8 "ford mustang boss 302" NaN 140 3353 8 "chevrolet monte carlo" 15 150 3761 9.5 "buick estate wagon (sw)" 14 225 3086 10 "toyota corona mark ii" 24 95 2372 15 "plymouth duster" 22 95 2833 15.5 "amc hornet" 18 97 2774 15.5 "ford maverick" 21 85 2587 16 "datsun pl510" 27 88 2130 14.5 "volkswagen 1131 deluxe sedan" 26 46 1835 20.5 "peugeot 504" 25 87 2672 17.5 "audi 100 ls" 24 90 2430 14.5 "saab 99e" 25 95 2375 17.5 "bmw 2002" 26 113 2234 12.5 "amc gremlin" 21 90 2648 15 "ford f250" 10 215 4615 14 "chevrolet c20" 10 200 4376 15 "dodge d200" 11 210 4382 13.5 "ih 1200d" 9 193 4732 18.5 "fiat 131" 28 86 2464 15.5 "opel 1900" 25 81 2220 16.9 "ford capri ii" 25 92 2572 14.9 "dodge colt" 26 79 2255 17.7 "renault 12tl" 27 83 2202 15.3 "chevrolet chevelle malibu classic" 17.5 140 4215 13 "dodge coronet brougham" 16 150 4190 13 "amc matador" 15.5 120 3962 13.9 "ford gran torino" 14.5 152 4215 12.8 "plymouth valiant" 22 100 3233 15.4 "chevrolet nova" 22 105 3353 14.5 "ford maverick" 24 81 3012 17.6 "amc hornet" 22.5 90 3085 17.6 "chevrolet chevette" 29 52 2035 22.2 "chevrolet woody" 24.5 60 2164 22.1 "volkswagen rabbit" 29 70 1937 14.2 "honda civic" 33 53 1795 17.4 "dodge aspen se" 20 100 3651 17.7 "ford granada ghia" 18 78 3574 21 "pontiac ventura sj" 18.5 110 3645 16.2 "amc pacer d/l" 17.5 95 3193 17.8 "volkswagen rabbit" 29.5 71 1825 12.2 "datsun b-210" 32 70 1990 17 "toyota corolla" 28 75 2155 16.4 "ford pinto" 26.5 72 2565 13.6 "volvo 245" 20 102 3150 15.7 "plymouth volare premier v8" 13 150 3940 13.2 "peugeot 504" 19 88 3270 21.9 "toyota mark ii" 19 108 2930 15.5 "mercedes-benz 280s" 16.5 120 3820 16.7 "cadillac seville" 16.5 180 4380 12.1 "chevrolet c10" 13 145 4055 12 "ford f108" 13 130 3870 15 "dodge d100" 13 150 3755 14 "chevrolet cavalier" 28 88 2605 19.6 "chevrolet cavalier wagon" 27 88 2640 18.6 "chevrolet cavalier 2-door" 34 88 2395 18 "pontiac j2000 se hatchback" 31 85 2575 16.2 "dodge aries se" 29 84 2525 16 "pontiac phoenix" 27 90 2735 18 "ford fairmont futura" 24 92 2865 16.4 "amc concord dl" 23 NaN 3035 20.5 "volkswagen rabbit l" 36 74 1980 15.3 "mazda glc custom l" 37 68 2025 18.2 "mazda glc custom" 31 68 1970 17.6 "plymouth horizon miser" 38 63 2125 14.7 "mercury lynx l" 36 70 2125 17.3 "nissan stanza xe" 36 88 2160 14.5 "honda accord" 36 75 2205 14.5 "toyota corolla" 34 70 2245 16.9 "honda civic" 38 67 1965 15 "honda civic (auto)" 32 67 1965 15.7 "datsun 310 gx" 38 67 1995 16.2 "buick century limited" 25 110 2945 16.4 "oldsmobile cutlass ciera (diesel)" 38 85 3015 17 "chrysler lebaron medallion" 26 92 2585 14.5 "ford granada l" 22 112 2835 14.7 "toyota celica gt" 32 96 2665 13.9 "dodge charger 2.2" 36 84 2370 13 "chevrolet camaro" 27 90 2950 17.3 "ford mustang gl" 27 86 2790 15.6 "volkswagen pickup" 44 52 2130 24.6 "dodge rampage" 32 84 2295 11.6 "ford ranger" 28 79 2625 18.6 "chevrolet s-10" 31 82 2720 19.4

Error using tabular/sortrows (line 57) Variable index exceeds table dimensions. Error in sort_cars (line 4) B = sortrows(cars,2) Error in Test2 (line 6) assert(isequal(sort_cars(N),[33; 29.5; 26; 29; 38; 32]));

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!