dow = load('djia.txt');

% convenient tags for the various columns
YEAR = 1;
MONTH = 2;
DATE = 3;
OPEN = 4;
HIGH = 5;
LOW = 6;
CLOSE = 7;
VOLUME = 8;

shortStart = 0;            % row# for start of shortest year
shortEnd = 0;              % row# for end of shortest year
curStart = 1;              % row# for start of current year
curYear = dow(1, YEAR);    % value of the current month (e.g., 10)
shortNumDays = -1;

totalDays = [];       % totalDays(yr) will be number of days for that year

% go through all market days
for today = 1:length(dow)
   
    % if today is a different month than yesterday
    if dow(today, YEAR) ~= curYear
        % new year is beginning
        if (curStart > 1)        % only interesting in years beyond the first (partial?) year
            curEnd = today -1;   % year that is ending
            numDays = (curEnd - curStart + 1);
            totalDays(curYear) = numDays;
            if shortNumDays == -1 || numDays < shortNumDays
               % we found a new winner
               shortNumDays = numDays;
               shortStart = curStart;
	       shortEnd = curEnd;
            end
        end
        
        % prepare for the newly starting year
        curStart = today;
        curYear = dow(today,YEAR);
    end
    
end    % done with for loop over all days

for yr = 1929:2008
    fprintf('Year %d: %d\n', yr, totalDays(yr));
end

fprintf('The shortest year was %4d\n', dow(shortStart,YEAR));
fprintf('There were %d days.\n', shortEnd-shortStart+1);






%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
