You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.1 KiB
Python
105 lines
2.1 KiB
Python
2 years ago
|
import sys
|
||
2 years ago
|
import time
|
||
2 years ago
|
import random
|
||
|
|
||
|
# hard coded in
|
||
|
sys.setrecursionlimit(1000000000)
|
||
|
print(sys.getrecursionlimit())
|
||
|
|
||
|
|
||
|
corpus = []
|
||
|
with open('corpus/words') as f:
|
||
|
corpus = f.readlines()
|
||
|
|
||
|
corpus = [word.strip() for word in corpus]
|
||
|
|
||
|
fruit_list = corpus
|
||
|
#print(fruit_list)
|
||
2 years ago
|
|
||
|
class letterLeaf:
|
||
|
def __init__(self,letter,wordFruit):
|
||
|
self.leftAlphabet = None
|
||
|
self.rightAlphabet = None
|
||
|
self.letter = letter
|
||
|
self.wordFruit = wordFruit
|
||
|
|
||
|
COUNT = [10]
|
||
|
|
||
|
def print2DUtil_flat(root, space,growth_rate):
|
||
|
if (root == None) :
|
||
|
return
|
||
|
|
||
|
space += COUNT[0]
|
||
|
print2DUtil_flat(root.rightAlphabet, space,growth_rate)
|
||
|
|
||
|
print()
|
||
|
|
||
|
for i in range(COUNT[0], space):
|
||
|
print(end = " ")
|
||
|
|
||
|
print(root.letter)
|
||
|
|
||
|
for i in range(COUNT[0], space):
|
||
|
print(end = " ")
|
||
|
|
||
|
print(root.wordFruit)
|
||
|
time.sleep(growth_rate)
|
||
|
|
||
|
print2DUtil_flat(root.leftAlphabet, space,growth_rate)
|
||
|
|
||
|
def print2D(root, growth_rate):
|
||
|
print("here is a tree that's laying on the ground: ")
|
||
|
print2DUtil_flat(root, 0, growth_rate)
|
||
|
|
||
|
def grepFirstLetter(word):
|
||
|
firstLetter = word[0]
|
||
|
return firstLetter
|
||
|
|
||
|
def insertLeaf(root,wordFruit):
|
||
|
letter = grepFirstLetter(wordFruit)
|
||
|
newleaf = letterLeaf(letter,wordFruit)
|
||
|
x = root
|
||
|
y = None
|
||
|
|
||
|
while (x != None):
|
||
|
y = x
|
||
|
if (letter < x.letter):
|
||
|
x = x.leftAlphabet
|
||
|
else:
|
||
|
x = x.rightAlphabet
|
||
|
|
||
|
if (y == None):
|
||
|
y = newleaf
|
||
|
|
||
|
elif (letter < y.letter):
|
||
|
y.leftAlphabet = newleaf
|
||
|
|
||
|
else:
|
||
|
y.rightAlphabet = newleaf
|
||
|
|
||
|
return y
|
||
|
|
||
|
import random
|
||
|
|
||
|
root = None
|
||
|
|
||
|
random_fruit = random.choice(fruit_list)
|
||
|
root = insertLeaf(root, random_fruit)
|
||
|
fruit_list.remove(random_fruit)
|
||
|
len_list = (len(fruit_list))
|
||
|
|
||
|
while len_list > 0:
|
||
|
random_fruit = random.choice(fruit_list)
|
||
|
insertLeaf(root,random_fruit)
|
||
|
fruit_list.remove(random_fruit)
|
||
|
len_list -= 1
|
||
|
|
||
|
|
||
|
def ask_input():
|
||
|
growth_rate = int(input("Enter growth rate value for tree in seconds: "))
|
||
|
return growth_rate
|
||
|
|
||
|
growth_rate = ask_input()
|
||
|
print2D(root, growth_rate)
|
||
2 years ago
|
|