Skip to content

Instantly share code, notes, and snippets.

@beltiras
Forked from gunnarig/sll.py
Last active April 15, 2019 20:51
Show Gist options
  • Save beltiras/619fd2a0be90496cc3013c7485a0d941 to your computer and use it in GitHub Desktop.
Save beltiras/619fd2a0be90496cc3013c7485a0d941 to your computer and use it in GitHub Desktop.
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