Feb-11-2020, 06:45 AM
(This post was last modified: Feb-11-2020, 06:47 AM by BladedSupernova.)
So I'm building my foundation for strong AI (a data compressor/predictor). You can see my code below (output is below it). It builds a tree made of 2 4-letter branches in this example (abab and baba). You can see at the start ab and 3 8, if you chose b then you take the pointer/number '8' and go to 8th item in list, that's what the code does. I'm happy with it but is the code too long or actually very short? It's my first algorithm and any tree I've seen on the internet was complex code (others said trees were complex/scary), but mine seems simple, I think.
tree = [''] window_start = 1 window_end = 4 for count2 in range(2): window = 'ababa'[window_start - 1 : window_end] window_start = window_start + 1 window_end = window_end + 1 char_location = 1 node = 1 for count in range(4): char_in_window = window[char_location - 1] char_location = char_location + 1 char_index = tree[node - 1].find(char_in_window) + 1 if char_index == 0: tree[node - 1] = str(tree[node - 1]) + str(char_in_window) if char_location != 5: if node == len(tree): tree.append([]) tree[(node + 1 - 1)].append(len(tree) + 1) tree.append('') node = len(tree) else: if char_location != 5: goto = node + 1 else: goto = node node = tree[goto - 1][char_index - 1] print(tree)['ab', [3, 8], 'b', [5], 'a', [7], 'b', 'a', [10], 'b', [12], 'a']