Jorden van Leeuwen - MATLAB Cody - MATLAB Central

Jorden van Leeuwen

27349
Rank
2
Badges
170
Score
1 – 21 of 21

Jorden van Leeuwen submitted a Comment to Solution 2585335

@Jovan Krunic: % % find(vertcat(1,diff(a(:)),1)) % val=a(ans(diff(ans)==max(diff(ans)))) % % the approach is: % find the differences between the elements of the vector % consecutive zero's mean there is a run of the same values % get the indices of none zero elements of this vector % get the differences of these indices (which are a measure for the length the run of consecutive zero's, the 'gap' resulting from removing the zeros) % note that to make this work for vectors that start or end with the longest run we add a 1 to the beginning and the end of the first difference vector % get the max value from this last result which is the max sequence length % get the indices of these max values, that is, the first indices of the runs that have this max length % note that this last result is always a column vector % get the values of the first elements of the runs with max length from the original vector % note that if the original vector was a row vector, the result will be a row vector and when it was a column vector the result is a column vector, % as required in the problem description function val=longrun(a) val = []; if length(a) > 0 % a(:) will convert a to a column vector a_as_column_vector = a(:); % differences between elements of a diff_a = diff(a_as_column_vector); % differences between elements of a enclosed in ones (to make sure first and last element are non zero) vertcat_1_diff_a_1 = vertcat(1, diff_a, 1); % non zero elements of vertcat_1_diff_a_1 find_vertcat_1_diff_a_1 = find(vertcat_1_diff_a_1); % the difference between the non zero indices is equal to the length of the run of consecutive numbers lengts_of_runs_of_consecutive_numbers = diff(find_vertcat_1_diff_a_1); % set the elements that have the max length to one and the others to 0 - note that shift of indices as a result of diff is compensated by adding 1 at the front of the vector indices_of_elements_that_have_max_length_run = lengts_of_runs_of_consecutive_numbers == max(lengts_of_runs_of_consecutive_numbers); indices_of_first_elements_of_longest_run_of_consecutive_numbers = find_vertcat_1_diff_a_1(indices_of_elements_that_have_max_length_run); values_of_first_elements_of_longest_run_of_consecutive_numbers = a(indices_of_first_elements_of_longest_run_of_consecutive_numbers); val = values_of_first_elements_of_longest_run_of_consecutive_numbers; end end

on 17 Jan 2021

Jorden van Leeuwen submitted a Comment to Solution 3957788

how is the size of a solution calculated? These solutions with a regexp seem to be designed to get this count low but imho are not the best solutions and should not be allowed as leading solutions. It is more of a keep the size low trick.

on 10 Jan 2021

Jorden van Leeuwen received Commenter badge for Solution 4571038

on 10 Jan 2021

Jorden van Leeuwen submitted a Comment to Solution 4571038

It should be mentioned in the problem that hotels and ratings are column vectors. My solution good = hotels(ratings >= cutoff)'; failed because I (imho correctly) assumed that a list is a row vector.

on 10 Jan 2021

Jorden van Leeuwen received Solver badge for Solution 4570613

on 10 Jan 2021

1 – 21 of 21
Go to top of page