Skip to main content

NoahEaton

User since Oct 28, 2022
1 Posts
Python

Recent Posts

AnyTree Randomizer

0 likes • Apr 15, 2021 • 0 views
Python
import anytree as at
import random as rm
# Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white.
def random_tree(node_count):
# Generates the list of nodes
nodes = []
for i in range(node_count):
test = rm.randint(1,2)
if test == 1:
nodes.append(at.Node(str(i),color="white"))
else:
nodes.append(at.Node(str(i),color="red"))
#Creates the various main branches
for i in range(node_count):
for j in range(i, len(nodes)):
test = rm.randint(1,len(nodes))
if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]):
nodes[j].parent = nodes[i]
#Collects all the main branches into a single tree with the first node being the root
for i in range(1, node_count):
if nodes[i].parent == None and (not nodes[i] == nodes[0]):
nodes[i].parent = nodes[0]
return nodes[0]

Post Statistics

Posts

No Posts Found

It looks like NoahEaton hasn't uploaded a post yet

Likes

Please Log In

You must be authenticated to view a user's likes

Shared

Please Log In

You must be authenticated to view a user's shared posts

Profile Privacy

Multi-Factor Authentication

Multi-Factor Authentication (MFA) is an authentication method that requires you to provide two or more verification factors to gain access to your account. In addition to username and password, MFA requires you to verify your email on every login, which decreases the likelihood of someone stealing your account.

Change Password

Forgot Password?

Identity Color

Changes the color of your profile icon and cursor highlight in the live code editor. You and other users will be able to view this change.

Delete Account

Deleting your account is permanent. All data associated with your account will be lost.