Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. def createTree(choices,levels):
  2.     #This is a special kind of tree. Let's say you have 4 choices
  3.     #The first choice has 4 branches stemming from it
  4.     #The second has 3 branches stemming from it
  5.     #The third has 2
  6.     #The fourth has only 1
  7.     #This pattern repeats for every level
  8.     choiceArray = ['r','g','b','y'];#Hardcode choices to 4 for now
  9.     i = 0;
  10.     nodeList = []
  11.     tmpNodeList = []
  12.     for choice in choiceArray:
  13.         Node = {'index':i,'parent':0,'name':choice}
  14.         i+= 1;
  15.         nodeList.append(Node);
  16.     while(len(nodeList) > 0):
  17.         node = nodeList.pop(0)
  18.         #Translate index to how many branches
  19.         branches = len(choiceArray) - node['index']
  20.        
  21.         for i in range(0,branches):
  22.             newNode = {'index':i+node['index'],'parent':node,'name':choiceArray[i+node['index']]}
  23.             tmpNodeList.append(newNode)
  24.         if(len(nodeList) == 0):
  25.             for node in tmpNodeList:
  26.                 nodeList.append(node)
  27.             tmpNodeList = [];
  28.             levels -= 1;
  29.             if(levels <= 1):
  30.                 #Done!
  31.                 print(len(nodeList))
  32.                 break
  33.  
  34. for i in range(2,11):
  35.     print("Num: " + str(i))
  36.     createTree(['r','g','b','y'],i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement