This would be improved by a richer test set to avoid solutions based on simple lookup of the test cases.
I think we can assume the entries are all positive integers, but it would be good if that was stated in the problem specification.
Definetely agree. Adding more tests in the test suite and rescoring the solutions will elmiminate solution under a score of 92
My 33 is a terrible solution. Please add more test cases.
Tim's 80 is very good and my 83 is also valid. Addition of a common random value at an end overlap will eliminate chicanery. v=randi(100) [1 2 3 v] [v 4 5].
Created legitimate size 72.
Added a few more tests. Thanks for the comments!
s1 = [3 1 4 1 5 9 2 6 5 3 5];
s2 = [9 2 6 5];
s3_correct = [3 1 4 1 5 9 2 6 5 3 5]; maybe is not correct ;-)
Problems 1 and 2 have alternate solutions:
1) [5 4 3 1 2 3 4 5]
2) [1 0 1 7 7 7 6 0 1]
Did I just solved this myself? Toughest for me so far. Thanks!!!
This did not make me sleep. Most difficult so far, took me some few hours lol.
minor point - the specification is poorly worded.
The test input/output is not strings, but rather integer arrays.
e.g.
s1 = [10 20 30 40 50]
rather than "10 20 30 40 50"
I suppose this problem was written before the introduction of strings to Matlab (2016b) and so it may not have been so ambiguous at the time. (although even back then I would have interpreted the word "string" to mean a character array.)
my code, only writy by number order.If you can write through 'string' or 'character',maybe easier to work out the problem.
f_true =1;
b_true =1;
if numel(s1)<numel(s2)
s3 = s1;
s1 = s2;
s2 = s1;
end
num_1 = numel(s1);
num_2 = numel(s2);
first_index = find(s1 == s2(1));
lap_num_f = zeros(numel(first_index),1);
for i = 1:numel(first_index)
lap = first_index(i);
lap_num = 0;
for j = 1:num_2
if lap>num_1
break
end
if s1(lap) == s2(j)
lap_num =lap_num+1;
lap = lap+1;
j = j+1;
else
f_true = 0;
lap_num;
break
end
end
lap_num_f(i) = lap_num;
end
lap_num = max(lap_num_f);
if isempty(lap_num)
lap_num = 0;
end
if f_true
if lap_num<num_2&&lap_num>0
overlap_f = s1;
overlap1 = s2;
overlap1(1:lap_num) = [];
overlap_f = [overlap_f, overlap1];
else
if lap_num == 0
overlap_f = [s1,s2];
else
overlap_f = s1;
end
end
else
overlap_f = [s1,s2];
end
last_index = find(s1 == s2(end));
lap_num_b = zeros(numel(last_index),1);
for i = 1:numel(last_index)
lap = last_index(i);
lap_num = 0;
for j = 1:num_2
if lap<1
break
end
if s1(lap) == s2(num_2+1-j)
lap_num =lap_num+1;
lap = lap-1;
j = j-1;
else
b_true = 0;
lap_num = 0;
break
end
end
lap_num_b(i) = lap_num;
end
lap_num = max(lap_num_b);
if isempty(lap_num)
lap_num = 0;
end
if b_true
if lap_num<num_2&&lap_num>0
overlap_b = s1;
overlap2 = s2;
overlap2((end+1-lap_num):end) = [];
overlap_b = [overlap2, overlap_b];
else
if lap_num == 0
overlap_b = [s1,s2];
else
overlap_b = s1;
end
end
else
overlap_b = [s1,s2];
end
if numel(overlap_f)<numel(overlap_b)
overlap = overlap_f;
else
overlap = overlap_b;
end
s3 = overlap
Nice, but misses an edge case, hence not quite correct. It will enter an infinite recursion loop when the two inputs, s1 and s2, are disjoint (i.e. they do not overlap).
Easy to fix with a check that intersect(s1,s2) is not empty.
The solution is not correct.
It hard codes a test case that it cannot pass otherwise.
There are other cases that it cannot solve, although not in the test set.
hello, have you ever tried s1 = [ 1 2 3 ], s2= [1 2]?
Nice :) I was trying to think of clever ways to use convolution, but didn't manage. This should be the leading solution instead of the lookup based on the test cases..
well commneted
Come on. Put in a general solution rather than gaming the solution set.
1575 Solvers
Sum of diagonal of a square matrix
1386 Solvers
Number of 1s in a binary string
3986 Solvers
07 - Common functions and indexing 6
377 Solvers
1827 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!