nodes: 0, module; 1, function_definition; 2, function_name:lexical_distance; 3, parameters; 4, block; 5, identifier:a; 6, identifier:b; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, for_statement; 12, return_statement; 13, comment:"""
       Computes the lexical distance between strings A and B.
       The "distance" between two strings is given by counting the minimum number
       of edits needed to transform string A into string B. An edit can be an
       insertion, deletion, or substitution of a single character, or a swap of two
       adjacent characters.
       This distance can be useful for detecting typos in input or sorting
       @returns distance in number of edits
      """; 14, assignment; 15, assignment; 16, identifier:i; 17, call; 18, block; 19, identifier:i; 20, call; 21, block; 22, subscript; 23, identifier:d; 24, boolean_operator; 25, identifier:d_len; 26, boolean_operator; 27, identifier:range; 28, argument_list; 29, for_statement; 30, identifier:range; 31, argument_list; 32, for_statement; 33, subscript; 34, call; 35, list_comprehension; 36, list; 37, call; 38, integer:1; 39, identifier:d_len; 40, identifier:j; 41, call; 42, block; 43, integer:1; 44, binary_operator:len(a) + 1; 45, identifier:j; 46, call; 47, block; 48, identifier:d; 49, call; 50, identifier:len; 51, argument_list; 52, list; 53, for_in_clause; 54, identifier:len; 55, argument_list; 56, identifier:range; 57, argument_list; 58, if_statement; 59, call; 60, integer:1; 61, identifier:range; 62, argument_list; 63, expression_statement; 64, expression_statement; 65, if_statement; 66, identifier:len; 67, argument_list; 68, identifier:b; 69, identifier:i; 70, identifier:i; 71, call; 72, identifier:d; 73, integer:1; 74, binary_operator:len(b) + 1; 75, comparison_operator:i == 0; 76, block; 77, else_clause; 78, identifier:len; 79, argument_list; 80, integer:1; 81, binary_operator:len(b) + 1; 82, assignment; 83, assignment; 84, boolean_operator; 85, block; 86, identifier:a; 87, identifier:range; 88, argument_list; 89, call; 90, integer:1; 91, identifier:i; 92, integer:0; 93, expression_statement; 94, block; 95, identifier:a; 96, call; 97, integer:1; 98, identifier:cost; 99, conditional_expression:0 if a[i - 1] == b[j - 1] else 1; 100, subscript; 101, call; 102, boolean_operator; 103, comparison_operator:a[i - 2] == b[j - 1]; 104, expression_statement; 105, binary_operator:len(a) + 1; 106, identifier:len; 107, argument_list; 108, call; 109, expression_statement; 110, identifier:len; 111, argument_list; 112, integer:0; 113, comparison_operator:a[i - 1] == b[j - 1]; 114, integer:1; 115, subscript; 116, identifier:j; 117, identifier:min; 118, argument_list; 119, boolean_operator; 120, comparison_operator:a[i - 1] == b[j - 2]; 121, subscript; 122, subscript; 123, assignment; 124, call; 125, integer:1; 126, identifier:b; 127, attribute; 128, argument_list; 129, call; 130, identifier:b; 131, subscript; 132, subscript; 133, identifier:d; 134, identifier:i; 135, binary_operator:d[i - 1][j] + 1; 136, binary_operator:d[i][j - 1] + 1; 137, binary_operator:d[i - 1][j - 1] + cost; 138, comparison_operator:i > 1; 139, comparison_operator:j < 1; 140, subscript; 141, subscript; 142, identifier:a; 143, binary_operator:i - 2; 144, identifier:b; 145, binary_operator:j - 1; 146, subscript; 147, call; 148, identifier:len; 149, argument_list; 150, subscript; 151, identifier:append; 152, identifier:j; 153, attribute; 154, argument_list; 155, identifier:a; 156, binary_operator:i - 1; 157, identifier:b; 158, binary_operator:j - 1; 159, subscript; 160, integer:1; 161, subscript; 162, integer:1; 163, subscript; 164, identifier:cost; 165, identifier:i; 166, integer:1; 167, identifier:j; 168, integer:1; 169, identifier:a; 170, binary_operator:i - 1; 171, identifier:b; 172, binary_operator:j - 2; 173, identifier:i; 174, integer:2; 175, identifier:j; 176, integer:1; 177, subscript; 178, identifier:j; 179, identifier:min; 180, argument_list; 181, identifier:a; 182, identifier:d; 183, identifier:i; 184, subscript; 185, identifier:append; 186, integer:0; 187, identifier:i; 188, integer:1; 189, identifier:j; 190, integer:1; 191, subscript; 192, identifier:j; 193, subscript; 194, binary_operator:j - 1; 195, subscript; 196, binary_operator:j - 1; 197, identifier:i; 198, integer:1; 199, identifier:j; 200, integer:2; 201, identifier:d; 202, identifier:i; 203, subscript; 204, binary_operator:d[i - 2][j - 2] + cost; 205, identifier:d; 206, identifier:i; 207, identifier:d; 208, binary_operator:i - 1; 209, identifier:d; 210, identifier:i; 211, identifier:j; 212, integer:1; 213, identifier:d; 214, binary_operator:i - 1; 215, identifier:j; 216, integer:1; 217, subscript; 218, identifier:j; 219, subscript; 220, identifier:cost; 221, identifier:i; 222, integer:1; 223, identifier:i; 224, integer:1; 225, identifier:d; 226, identifier:i; 227, subscript; 228, binary_operator:j - 2; 229, identifier:d; 230, binary_operator:i - 2; 231, identifier:j; 232, integer:2; 233, identifier:i; 234, integer:2
code: def lexical_distance(a, b):
      """
       Computes the lexical distance between strings A and B.
       The "distance" between two strings is given by counting the minimum number
       of edits needed to transform string A into string B. An edit can be an
       insertion, deletion, or substitution of a single character, or a swap of two
       adjacent characters.
       This distance can be useful for detecting typos in input or sorting
       @returns distance in number of edits
      """
  
      d = [[i] for i in range(len(a) + 1)] or []
      d_len = len(d) or 1
      for i in range(d_len):
          for j in range(1, len(b) + 1):
              if i == 0:
                  d[i].append(j)
              else:
                  d[i].append(0)
  
      for i in range(1, len(a) + 1):
          for j in range(1, len(b) + 1):
              cost = 0 if a[i - 1] == b[j - 1] else 1
  
              d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
  
              if i > 1 and j < 1 and a[i - 1] == b[j - 2] and a[i - 2] == b[j - 1]:
                  d[i][j] = min(d[i][j], d[i - 2][j - 2] + cost)
  
      return d[len(a)][len(b)]