Guest User

Untitled

a guest
Dec 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. def pairs(n):
  2. return n * (n - 1) / 2
  3.  
  4. def solution(lst):
  5. res = 0
  6. stack = lst[0:1]
  7. lst = sorted(lst[1:])
  8. while(len(lst)):
  9. nxt = lst.pop()
  10. if len(stack) and stack[0] == nxt:
  11. stack.append(nxt)
  12. else:
  13. res += pairs(len(stack))
  14. stack = [nxt]
  15. else:
  16. res += pairs(len(stack))
  17. return res
  18.  
  19. assert(pairs(3) == 3)
  20. assert(pairs(2) == 1)
  21. assert(pairs(0) == 0)
  22. assert(solution([5, 3, 1, 5, 5, 3]) == 4)
  23. assert(solution([5, 5]) == 1)
  24. assert(solution([5]) == 0)
  25. assert(solution([]) == 0)
Add Comment
Please, Sign In to add comment