Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x = strings(str1 = "12 ABCD 67 -89 hfj7jf12 22", str2 = "-1.2 ABCD -6.7. 89 hfj7.7jf 2.2")
- intnums = integers(str1)
- floatnums = doubles(str2)
- dist1 = hemming(str1, str2)
- dist2 = levenshtein(str1, str2)
- end
- function ans = integers(str)
- % state:
- % 1 - был разделитель
- % 2 - было число
- % 0 - было не число и не разделитель
- ans = [];
- settlers = [' ', ',', ';'];
- nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
- h = "";
- state = 0;
- i = 1;
- if ismember(str(i), nums)
- h = [h str(i)];
- i = 2;
- state = 2;
- end
- while (i <= length(str))
- ch = str(i);
- if (ismember(ch, nums))
- if (state == 0)
- h = "";
- state = 0;
- else
- h = [h ch];
- state = 2;
- end
- elseif (ch == '-')
- if ((state == 2) || (state == 0))
- h = "";
- state = 0;
- else
- h = [h ch]
- end
- elseif (ismember(ch, settlers))
- if (state == 2)
- ans = [ans; str2num(h)];
- h = "";
- state = 1;
- else
- state = 1;
- end
- else
- state = 0;
- h = "";
- end
- i+=1;
- end
- if (state == 2)
- ans = [ans; str2num(h)];
- end
- end
- function ans = doubles(str)
- % state:
- % 1 - был разделитель
- % 2 - было число перед точкой
- % 3 - была точка
- % 4 - было число после точки
- % 0 - было не число, не точка и не разделитель
- ans = [];
- settlers = [' ', ',', ';'];
- nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
- h = "";
- state = 0;
- i = 1;
- if ismember(str(i), nums)
- h = [h str(i)];
- i = 2;
- state = 2;
- end
- while (i <= length(str))
- ch = str(i);
- if (ismember(ch, nums))
- if (state == 1) || (state == 2)
- state = 2;
- h = [h ch];
- elseif (state == 3) || (state == 4)
- state = 4;
- h = [h ch];
- else
- h = "";
- state = 0;
- end
- elseif (ch == '-')
- if (state == 1) || (i == 1)
- state = 1;
- h = [h ch];
- else
- state = 0;
- h = "";
- end
- elseif (ismember(ch, settlers))
- if (state == 4)
- ans = [ans; str2num(h)];
- h = "";
- state = 1;
- else
- state = 1;
- h = "";
- end
- elseif (ch == ".")
- if (state == 2)
- state = 3;
- h = [h ch];
- else
- state = 0;
- h = "";
- end
- else
- state = 0;
- h = "";
- end
- i+=1;
- end
- if (state == 4)
- ans = [ans; str2num(h)];
- end
- end
- function ans = hemming(str1, str2)
- ans = 0;
- if (length(str2) > length(str1))
- c = str1;
- str1 = str2;
- str2 = c;
- end
- for i=1:length(str2)
- if (str1(i) != str2(i))
- ans += 1;
- end
- end
- ans += length(str1) - length(str2);
- end
- function ans = levenshtein(str1, str2)
- ans = zeros(length(str1), length(str2));
- for i=1:length(str1)
- for j=1:length(str2)
- if ((i == 1) && (j == 1))
- ans(i, j) = 0;
- elseif ((i > 1) && (j == 1))
- ans(i, j) = i;
- elseif ((j > 1) && (i == 1))
- ans(i, j) = j;
- else
- if (str1(i) == str2(j))
- t = 0;
- else
- t = 1;
- end
- ans(i, j) = min(min(ans(i, j - 1) + 1, ans(i - 1, j) + 1), ans(i - 1, j - 1) + t);
- end
- end
- end
- ans = ans(end, end);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement