| 8 | |
| 9 | # Complete the sockMerchant function below. |
| 10 | def sockMerchant(n, ar): |
| 11 | ar2 = [] |
| 12 | ar1 =sorted(ar) |
| 13 | pair = 0 |
| 14 | numb = 0 |
| 15 | |
| 16 | # The while loop is used so that the index in ar1[numb] does not go out of index limit. |
| 17 | # First I tried doing while numb <= len(ar1) which gave indexError. |
| 18 | # Hence I removed the = sign as the range is '1 less than ar1' . |
| 19 | while numb < len(ar1): |
| 20 | # Used this foor loop to slice ar1 in same item lists. |
| 21 | # To use the same list again and again O cleared the list below using del(). |
| 22 | for i in ar1: |
| 23 | if i == ar1[numb]: |
| 24 | ar2.append(i) |
| 25 | a = len(ar2) |
| 26 | del(ar2[:]) |
| 27 | |
| 28 | if a >= 2: |
| 29 | if a % 2 == 0: |
| 30 | pair += round(a/2) |
| 31 | |
| 32 | else: |
| 33 | pair += round((a-1)/2) |
| 34 | |
| 35 | numb += a |
| 36 | return pair |
| 37 | |
| 38 | |
| 39 | |