Sort by

recency

|

1612 Discussions

|

  • + 0 comments

    Dont crtl c/v ! Read and pls try to comprehend

    n = int(input()) # Number of words l = [] # List to store the words seen = {} # Dictionary to keep track of occurrences

    for i in range(n): word = input() # Input the word l.append(word) # Append word to list if word in seen: seen[word] += 1 # Increment count if word is already in the dictionary else: seen[word] = 1 # Initialize count to 1 if it's the first occurrence of the word

    result = [] for word in l: if seen.get(word) is not None: result.append(seen[word]) # Append the count of occurrences del seen[word] # Remove the word from the dictionary to avoid duplicates in output

    print(len(result)) # Print the number of distinct words print(*result) # Print the counts of occurrences in the order of appearance

  • + 0 comments

    from collections import defaultdict

    dic=defaultdict(int)

    for i in range(int(input())):

    dic[input()]+=1
    

    print(len(dic))

    print(' '.join([ str(dic[x]) for x in dic.keys() ]))

  • + 0 comments
    def distinct(words: list[str]) -> None:
        counter: dict[str,int] = {}
    
        for word in words:
            if word in counter:
                counter[word] += 1
            else:
                counter[word] = 1
    
        print(len(counter))
        for v in counter.values():
            print(v, end=' ')
    
    
    if __name__ == "__main__":
        n = int(input())
        words = [input() for i in range(n)]
        distinct(words)
        
    
  • + 0 comments
    from collections import OrderedDict
    
    od = OrderedDict()
    for _ in range(int(input())):
        key = input()
        od[key] = od.get(key, 0) + 1
    print(len(od))
    print(" ".join(map(str, od.values())))
    
  • + 0 comments
    1. using default dict
    2. from collections import defaultdict
    3. n =int(input())
    4. li = []
    5. my_dict = defaultdict(int)
    6. for i in range (n):
    7. i = input()
    8. li.append(i)
    9. for i in li:
    10. my_dict[i] +=1
    11. print(len(my_dict)) print(" ".join(map(str,my_dict.values())))