Sort by

recency

|

1614 Discussions

|

  • + 0 comments

    First tried a few loops, worked but slow so didn't pass:

    words = [input() for i in range(0, int(input()))]
    
    wordsset = set(words)
    
    def count(words, searchterm):
        return str(len([i for i in words if i == searchterm]))
    
    print(len(wordsset))
    
    counts = [count(words,i) for i in wordsset]
    
    print(" ".join(counts))
    

    So did a one pass attempt which works and passed:

    dict = {}
    for i in range(0, int(input())):
        strin = input()
        count = dict.get(strin, 0)
        dict[strin] = count+1
        
    print(len(dict))
    print(' '.join(map(str, dict.values())))
    
  • + 1 comment

    import collections

    n = int(input()) odi = collections.OrderedDict()

    for word in range(n): word = input() odi[word] = odi.setdefault(word, 0) + 1

    print(len(odi)) print(*list(odi.values()))

    • + 0 comments

      This was using OrderedDict and setdefault()

  • + 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)