Skip to main content

hackerrank

AAlex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

For example, there are  socks with colors . There is one pair of color  and one of color . There are three odd socks left, one of each color. The number of pairs is .

.AFunction Description

Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

asockMerchant has the following parameter(s):

  • n: the number of socks in the pile
  • ar: the colors of each sock

.Input Format

The first line contains an integer , the number of socks represented in .
The second line contains  space-separated integers describing the colors  of the socks in the pile.

Constraints

  •  where 

.Output Format

Return the total number of matching pairs of socks that Alex can sell.

.Sample Input

9
10 20 20 10 10 30 50 10 20

.Sample Output

3
.SOLVE๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ˜€
from itertools import groupby
n = int(input())
c = map(int,input().split())

ans = 0
for val in [len(list(group)) for key, group in groupby(sorted(c))]:
    ans = ans + val/2
print(ans)

 ....

Comments

Popular posts from this blog

เคšंเคฆ्เคฐเคฏाเคจ-2,

Chandrayan-2: เคšांเคฆ เค•े เค”เคฐ เค•เคฐीเคฌ เคชเคนुंเคšा เคšंเคฆ्เคฐเคฏाเคจ-2, เคœाเคจिเค-           เคชเคคा เคฒเค—ाเคเค—ा เค—ुเคฐुเคตाเคฐ เค•ी เคฐाเคค เค‰เคชเค—्เคฐเคน เค•ो เคชृเคฅ्เคตी เค•ी เค…เค—เคฒी เค•เค•्เคทा เคฎें เคช्เคฐเคตेเคถ เค•เคฐाเคฏा เค—เคฏा। 29 เคœुเคฒाเคˆ เค•ो เคคीเคธเคฐी เคฌाเคฐ เคšंเคฆ्เคฐเคฏाเคจ-2 เค•ी เค•เค•्เคทा เค•ो เคฌเคฆเคฒा เคœाเคเค—ा।   เคฌेंเค—เคฒुเคฐु, เคช्เคฐेเคŸ्เคฐ।  Chandrayaan-2  เค‰เคชเค—्เคฐเคน เค—ुเคฐुเคตाเคฐ เค•ी เคฐाเคค เคธเคซเคฒเคคाเคชूเคฐ्เคตเค• เคšंเคฆ्เคฐเคฎा เค•ी เคคเคฐเคซ เค”เคฐ เคเค• เค•เคฆเคฎ เคฌเคข़ เค—เคฏा। เค…ंเคคเคฐिเค•्เคทเคฏाเคจ เคชเคฐ เคฒเค—े เคช्เคฐเคฃोเคฆเคจ เคช्เคฐเคฃाเคฒी เค•े เคœเคฐिเค เค—ुเคฐुเคตाเคฐ-เคถुเค•्เคฐเคตाเคฐ เค•ी เคฐाเคค เคเค• เคฌเคœเค•เคฐ เค†เค  เคฎिเคจเคŸ เคชเคฐ เคฆूเคธเคฐी เคฌाเคฐ เค‰เคชเค—्เคฐเคน เค•ी เค•เค•्เคทा เค•ो เคฌเคฆเคฒเคคे เคนुเค เค‰เคธे เคธเคซเคฒเคคाเคชूเคฐ्เคตเค• เคชृเคฅ्เคตी เค•ी เค…เค—เคฒी เค•เค•्เคทा เคฎें เคช्เคฐเคตेเคถ เค•เคฐाเคฏा เค—เคฏा। เคญाเคฐเคคीเคฏ เค…ंเคคเคฐिเค•्เคท เค…เคจुเคธंเคงाเคจ เคธंเค—เค เคจ (เค‡เคธเคฐो) เคจे เคฌเคคाเคฏा เค•ि เค•เค•्เคทा เคฌเคฆเคฒเคจे เค•ी เคฏเคน เคช्เคฐเค•्เคฐिเคฏा เคฒเค—เคญเค— 15 เคฎिเคจเคŸ เคคเค• เคšเคฒी। เค•ी เคŠंเคšाเคˆ เคชเคฐ เคœीเคเคธเคเคฒเคตी-เคฎैเค• 3 เคฐॉเค•ेเคŸ เคธे เค…เคฒเค— เคนो เค—เคฏा เคฅा เค”เคฐ เคšเค•्เค•เคฐ เคฒเค—ा เคฐเคนा เคฅा। เคฌुเคงเคตाเคฐ 24 เคœुเคฒाเคˆ เค•ो เคชเคนเคฒी เคฌाเคฐ เค‰เคชเค—्เคฐเคน เค•ो เคชृเคฅ्เคตी เค•ी เค…เค—เคฒी เค•เค•्เคทा เคฎें เคญेเคœा เค—เคฏा เคฅा। เคถुเค•्เคฐเคตाเคฐ เค•ो เคฆूเคธเคฐी เคฌाเคฐ เค‰เคธเค•े เค•เค•्เคทा เคฎें เคฌเคฆเคฒाเคต เค•िเคฏा เค—เคฏा। เค›เคน เค…เค—เคธ्เคค เคคเค• เค…เคญी เค”เคฐ เคฆो เคฌाเคฐ เคšंเคฆ्เคฐเคฏाเคจ-2 เค•ी เค•เค•्เคทा (เค‘เคฐ्เคฌिเคŸ) เค•ो เคฌเคฆเคฒा เคœाเคเค—ा। เคšंเคฆ्เคฐเคฏाเคจ-2 เคชृเคฅ्เคตी เค•ी เค•เค•्เคทा เคธे 14 เค…เค—เคธ्เคค เค•ो เคšंเคฆ...
  following version of Bubble Sort: for ( int i = 0 ; i < n ; i ++) { for ( int j = 0 ; j < n - 1 ; j ++) { // Swap adjacent elements if they are in decreasing order if ( a [ j ] > a [ j + 1 ]) { swap ( a [ j ], a [ j + 1 ]); } } } Given an array of integers, sort the array in ascending order using the  Bubble Sort  algorithm above. Once sorted, print the following three lines: Array is sorted in numSwaps swaps. , where   is the number of swaps that took place. First Element: firstElement , where   is the  first  element in the sorted array. Last Element: lastElement , where   is the  last  element in the sorted array. Hint:  To complete this challenge, you must add a variable that keeps a running tally of  all  swaps that occur during execution. Example swap a 0 [6,4,1] 1 [4,6,1] 2 ...
 THE  An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly     steps, for every step it was noted if it was an   uphill ,   , or a   downhill ,     step. Hikes always start and end at sea level, and each step up or down represents a     unit change in altitude. We define the following terms: A  mountain  is a sequence of consecutive steps  above  sea level, starting with a step  up  from sea level and ending with a step  down  to sea level. A  valley  is a sequence of consecutive steps  below  sea level, starting with a step  down  from sea level and ending with a step  up  to sea level. Given the sequence of  up  and  down  steps during a hike, find and print the number of  valleys  walked through. Example   The hiker first enters a valley   units deep. Then they climb...