Skip to content

Instantly share code, notes, and snippets.

@gunnarig
Created April 15, 2019 20:44
Show Gist options
  • Save gunnarig/940d74401573cd3e696da133acd59be6 to your computer and use it in GitHub Desktop.
Save gunnarig/940d74401573cd3e696da133acd59be6 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(SLL_Node,type(object)):
return rec_avg(0,0,head)
def rec_avg(count,total,head):
if head.next != None:
total = head.data + total
count += 1
rec_avg(count,total,head.next)
else:
count += 1
total = total + head.data
avg = float(total) / float(count)
return average_of_list(avg)
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