Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is my Tree code too long?
#1
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']
Reply


Messages In This Thread
Is my Tree code too long? - by BladedSupernova - Feb-11-2020, 06:45 AM
RE: Is my Tree code too long? - by stullis - Feb-11-2020, 04:09 PM
RE: Is my Tree code too long? - by BladedSupernova - Feb-11-2020, 05:38 PM
RE: Is my Tree code too long? - by BladedSupernova - Feb-11-2020, 08:35 PM
RE: Is my Tree code too long? - by BladedSupernova - Feb-12-2020, 01:26 AM
RE: Is my Tree code too long? - by Larz60+ - Feb-12-2020, 03:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 635 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Does this code need to be so long? duckredbeard 4 954 Sep-27-2022, 09:36 AM
Last Post: ibreeden
  how long can a line of code be? Skaperen 2 2,242 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,341 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Mix-in class tree file not running the self test code. arjunsingh2908 3 3,023 Aug-14-2018, 05:46 PM
Last Post: arjunsingh2908
  What is wrong with code? Bin. Tree counting Peter_EU 3 3,482 Nov-08-2017, 08:41 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020