Problem 44952. Find MPG of Lightest Cars

Mayla on 10 Sep 2023
Latest activity Reply by Walter Roberson on 24 Oct 2023

That's the question:
The file cars.mat contains a table named cars with variables Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.
Load the MAT-file. Given an integer N, calculate the output variable mpg.
Output mpg should contain the MPG of the top N lightest cars (by Weight) in a column vector.
I wrote this code and the resulting column vector has the right values but it doesn't pass the tests. What's wrong?
function mpg = sort_cars(N)
load cars.mat
sorted=sortrows(cars,4)
mpg = sorted(1:N,2)
end
Dyuman Joshi
Dyuman Joshi on 10 Sep 2023
Hello Mayla,
The reason your code does not pass the test suite is because the variable mpg in your code is of Table data type, whereas the question expects the answer in Numeric data type.
Also, I would suggest you to check the assert() call while solving problems in Cody to see what and which type of output is expected.
Walter Roberson
Walter Roberson on 24 Oct 2023
Right.
The most common operating system these days is still Windows, and the most common disk file system on Windows is NTFS. NTFS file systems are case insensitive by default -- so if you have a stored NTFS file named cars.mat and you ask to load Cars.mat then it is likely to succeed.
MacOS file systems HFS+ and the newer APFS, also default to case insensitive -- but it is more common for users to deliberately switch them to case sensitive than is the case for Windows.
Mathworks' online systems such as MATLAB Online and MATLAB Grader run on Linux -- and Linux filesystems default to case sensitive
DGM
DGM on 24 Oct 2023
Filenames are case-sensitive.