Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pairs(n):
- return n * (n - 1) / 2
- def solution(lst):
- res = 0
- stack = lst[0:1]
- lst = sorted(lst[1:])
- while(len(lst)):
- nxt = lst.pop()
- if len(stack) and stack[0] == nxt:
- stack.append(nxt)
- else:
- res += pairs(len(stack))
- stack = [nxt]
- else:
- res += pairs(len(stack))
- return res
- assert(pairs(3) == 3)
- assert(pairs(2) == 1)
- assert(pairs(0) == 0)
- assert(solution([5, 3, 1, 5, 5, 3]) == 4)
- assert(solution([5, 5]) == 1)
- assert(solution([5]) == 0)
- assert(solution([]) == 0)
Add Comment
Please, Sign In to add comment