Skip to content

Commit ec4190a

Browse files
committed
find the number of pairs formed from given socks
1 parent 39a27fd commit ec4190a

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Ushur/findPairs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Given a list of numbers and each number represents a different color of
3+
a sock, Find the number of pairs that can be formed from given list of
4+
socks
5+
6+
"""
7+
8+
import collections
9+
def find_pairs(n,socks):
10+
num_pairs = 0
11+
freqs = collections.Counter(socks)
12+
for s,c in freqs.items():
13+
if c >= 2:
14+
num_pairs += (c//2)
15+
return num_pairs
16+
17+
18+
if __name__ == '__main__':
19+
N = 9
20+
socks = [1,2,2,1,3,4,5,2,2]
21+
print (find_pairs(N, socks))
22+
23+
N = 10
24+
socks = [1,1,2,1,3,4,5,2,2,0]
25+
print (find_pairs(N, socks))

0 commit comments

Comments
 (0)