jeff1evesque / interview-twitter

Least Common Ancestor
2 stars 0 forks source link

programmatically reorder lca values #12

Open jeff1evesque opened 10 years ago

jeff1evesque commented 10 years ago

If users first LCA node value is greater than the second inputed value, we get an error of the following:

MacBook-Pro-15:least-common-ancestor jeffrey$ python least_common_ancestor.py
Would you like to enter values?
y
Enter tree value (q - to quit): 23
Enter tree value (q - to quit): 45
Enter tree value (q - to quit): 67
Enter tree value (q - to quit): 98
Enter tree value (q - to quit): 8
Enter tree value (q - to quit): 5
Enter tree value (q - to quit): 3
Enter tree value (q - to quit): 1
Enter tree value (q - to quit): q
Enter the first LCA node value: 67
Enter the second LCA node value: 8
1.0
2.0
3.0
5.0
8.0
45.0
67.0
98.0
^CTraceback (most recent call last):
  File "least_common_ancestor.py", line 94, in <module>
    lca = root.lca_algorithm(cast_type(data_lca[0]), cast_type(data_lca[1]), root)
  File "/Applications/MAMP/htdocs/least-common-ancestor/definitions_class.py", line 65, in lca_algorithm
    if value1 < node.data and value2 > node.data:
KeyboardInterrupt

If, however, we input the first LCA node value less than the second, it works fine:

MacBook-Pro-15:least-common-ancestor jeffrey$ python least_common_ancestor.py
Would you like to enter values?
y
Enter tree value (q - to quit): 23
Enter tree value (q - to quit): 45
Enter tree value (q - to quit): 67
Enter tree value (q - to quit): 98
Enter tree value (q - to quit): 8
Enter tree value (q - to quit): 5
Enter tree value (q - to quit): 3
Enter tree value (q - to quit): 1
Enter tree value (q - to quit): q
Enter the first LCA node value: 8
Enter the second LCA node value: 67
1.0
2.0
3.0
5.0
8.0
45.0
67.0
98.0
('least common ancestor: ', 45.0)
jeff1evesque commented 10 years ago

cb57940: regardless whether the first value is greater than the second inputted LCA value, the above error has been eliminated:

MacBook-Pro-15:least-common-ancestor jeffrey$ python least_common_ancestor.py
Would you like to enter values?
y
Enter tree value (q - to quit): 23
Enter tree value (q - to quit): 45
Enter tree value (q - to quit): 67
Enter tree value (q - to quit): 98
Enter tree value (q - to quit): 8
Enter tree value (q - to quit): 5
Enter tree value (q - to quit): 3
Enter tree value (q - to quit): 1
Enter tree value (q - to quit): q
Enter the first LCA node value: 67
Enter the second LCA node value: 8
1.0
2.0
3.0
5.0
8.0
45.0
67.0
98.0
('least common ancestor: ', 45.0)