Skip to content

Instantly share code, notes, and snippets.

@sc68cal
Created March 28, 2011 15:22
Show Gist options
  • Save sc68cal/890645 to your computer and use it in GitHub Desktop.
Save sc68cal/890645 to your computer and use it in GitHub Desktop.
Recursive algorithm test
class level(object):
def __init__(self, date):
self.children = []
self.date = date
def addchild(self,child):
self.children.append(child)
def latest_date(self):
if not self.children:
return self.date
else:
biggest = self.date
for child in self.children:
current = child.latest_date()
if current > biggest:
biggest = current
return biggest
def __repr__(self):
return str(self.date)
#/usr/bin/env python
import random
from level import level
def main():
root = level(0)
add(root)
for child in root.children:
add(child)
print "Top level"
print root.children
print "Second level"
for child in root.children:
print child
print child.children
print "result"
print root.latest_date()
def add(root):
generator = random.Random()
step = generator.randint(1,5)
index = 0
while index < step:
tmp = generator.randint(1,100)
root.addchild(level(tmp))
print tmp
index +=1
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment