Created
March 28, 2011 15:22
-
-
Save sc68cal/890645 to your computer and use it in GitHub Desktop.
Recursive algorithm test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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