Advertisement
epidzhx

strings

Mar 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.25 KB | None | 0 0
  1. function x = strings(str1 = "12 ABCD 67 -89 hfj7jf12 22", str2 = "-1.2 ABCD -6.7. 89 hfj7.7jf 2.2")
  2.  
  3.     intnums = integers(str1)
  4.     floatnums = doubles(str2)
  5.     dist1 = hemming(str1, str2)
  6.     dist2 = levenshtein(str1, str2)
  7. end
  8.  
  9.  
  10. function ans = integers(str)
  11. % state:
  12. % 1 - был разделитель
  13. % 2 - было число
  14. % 0 - было не число и не разделитель
  15.  
  16.     ans = [];
  17.     settlers = [' ', ',', ';'];
  18.     nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  19.     h = "";
  20.     state = 0;
  21.  
  22.     i = 1;
  23.     if ismember(str(i), nums)
  24.  
  25.         h = [h str(i)];
  26.         i = 2;
  27.         state = 2;
  28.     end
  29.    
  30.     while (i <= length(str))
  31.  
  32.         ch = str(i);
  33.  
  34.         if (ismember(ch, nums))
  35.  
  36.             if (state == 0)
  37.                 h = "";
  38.                 state = 0;
  39.             else
  40.                 h = [h ch];
  41.                 state = 2;
  42.             end
  43.  
  44.         elseif (ch == '-')
  45.  
  46.             if ((state == 2) || (state == 0))
  47.                 h = "";
  48.                 state = 0;
  49.             else
  50.                 h = [h ch]
  51.             end
  52.        
  53.         elseif (ismember(ch, settlers))
  54.  
  55.             if (state == 2)
  56.                 ans = [ans; str2num(h)];
  57.                 h = "";
  58.                 state = 1;
  59.             else
  60.                 state = 1;
  61.             end
  62.            
  63.         else
  64.  
  65.             state = 0;
  66.             h = "";
  67.         end
  68.         i+=1;
  69.     end
  70.  
  71.     if (state == 2)
  72.         ans = [ans; str2num(h)];
  73.     end
  74. end
  75.  
  76.  
  77. function ans = doubles(str)
  78. % state:
  79. % 1 - был разделитель
  80. % 2 - было число перед точкой
  81. % 3 - была точка
  82. % 4 - было число после точки
  83. % 0 - было не число, не точка и не разделитель
  84.  
  85.     ans = [];
  86.     settlers = [' ', ',', ';'];
  87.     nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  88.     h = "";
  89.     state = 0;
  90.  
  91.     i = 1;
  92.     if ismember(str(i), nums)
  93.  
  94.         h = [h str(i)];
  95.         i = 2;
  96.         state = 2;
  97.     end
  98.    
  99.     while (i <= length(str))
  100.  
  101.         ch = str(i);
  102.  
  103.         if (ismember(ch, nums))
  104.  
  105.             if (state == 1) || (state == 2)
  106.                 state = 2;
  107.                 h = [h ch];
  108.             elseif (state == 3) || (state == 4)
  109.                 state = 4;
  110.                 h = [h ch];
  111.             else
  112.                 h = "";
  113.                 state = 0;
  114.             end
  115.  
  116.         elseif (ch == '-')
  117.  
  118.             if (state == 1) || (i == 1)
  119.                 state = 1;
  120.                 h = [h ch];
  121.             else
  122.                 state = 0;
  123.                 h = "";
  124.             end
  125.  
  126.         elseif (ismember(ch, settlers))
  127.  
  128.             if (state == 4)
  129.                 ans = [ans; str2num(h)];
  130.                 h = "";
  131.                 state = 1;
  132.             else
  133.                 state = 1;
  134.                 h = "";
  135.             end
  136.  
  137.         elseif (ch == ".")
  138.  
  139.             if (state == 2)
  140.                 state = 3;
  141.                 h = [h ch];
  142.             else
  143.                 state = 0;
  144.                 h = "";
  145.             end
  146.  
  147.         else
  148.             state = 0;
  149.             h = "";
  150.         end
  151.         i+=1;
  152.     end
  153.  
  154.     if (state == 4)
  155.         ans = [ans; str2num(h)];
  156.     end
  157. end
  158.  
  159.  
  160. function ans = hemming(str1, str2)
  161.  
  162.     ans = 0;
  163.     if (length(str2) > length(str1))
  164.         c = str1;
  165.         str1 = str2;
  166.         str2 = c;
  167.     end
  168.     for i=1:length(str2)
  169.         if (str1(i) != str2(i))
  170.             ans += 1;
  171.         end
  172.     end
  173.  
  174.     ans += length(str1) - length(str2);
  175. end
  176.  
  177.  
  178. function ans = levenshtein(str1, str2)
  179.  
  180.     ans = zeros(length(str1), length(str2));
  181.     for i=1:length(str1)
  182.         for j=1:length(str2)
  183.             if ((i == 1) && (j == 1))
  184.                 ans(i, j) = 0;
  185.             elseif ((i > 1) && (j == 1))
  186.                 ans(i, j) = i;
  187.             elseif ((j > 1) && (i == 1))
  188.                 ans(i, j) = j;
  189.             else
  190.                 if (str1(i) == str2(j))
  191.                     t = 0;
  192.                 else
  193.                     t = 1;
  194.                 end
  195.                 ans(i, j) = min(min(ans(i, j - 1) + 1, ans(i - 1, j) + 1), ans(i - 1, j - 1) + t);
  196.             end
  197.         end
  198.     end
  199.     ans = ans(end, end);
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement