-
-
Save beltiras/619fd2a0be90496cc3013c7485a0d941 to your computer and use it in GitHub Desktop.
This file contains 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 SLL_Node: | |
def __init__(self, data, next): | |
self.data = data | |
self.next = next | |
# IMPLEMENT THIS | |
def average_of_list(head): | |
if type(head) == float: | |
return head | |
elif isinstance(head, SLL_Node): | |
return rec_avg(0,0,head) | |
def rec_avg(count,total,head): | |
if head.next != None: | |
return rec_avg(count + 1,total + head.data,head.next) | |
else: | |
return float(total + head.data) / (count + 1) | |
if __name__ == "__main__": | |
# MAKE BETTER TESTS! | |
print(average_of_list(SLL_Node(7, SLL_Node(5, SLL_Node(7, SLL_Node(5, None)))))) | |
print(average_of_list(SLL_Node(1, SLL_Node(3, SLL_Node(2, SLL_Node(4, None)))))) | |
print(average_of_list(SLL_Node(7, SLL_Node(5, SLL_Node(6, None))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment