Sort by

recency

|

1603 Discussions

|

  • + 0 comments
    from collections import OrderedDict
    
    N = int(input())
    
    word_dict = OrderedDict()
    
    for i in range(N):
        word = input()
        if word in word_dict:
            old_word_count = word_dict[word]
            new_word_count = old_word_count + 1
            word_dict[word] = new_word_count
        else:
            word_dict[word] = 1
    
    word_count_list = [ str(word_dict[i]) for i in word_dict ]
    print(len(word_count_list))
    print(" ".join(word_count_list))
    
  • + 0 comments

    Counter from collections module provides for us O(n) here:

    import sys
    from collections import Counter
    
    input = sys.stdin.read().splitlines()
    n = int(input[0])  
    words = input[1:]  
    
    counter = Counter(words)
    distinct_words = list(dict.fromkeys(words))  
    
    print(len(distinct_words))
    print(" ".join(str(counter[word]) for word in distinct_words))
    
  • + 0 comments
    from collections import OrderedDict
    
    # Input the number of words
    try:
        num_of_words = int(input().strip())
    except ValueError:
        raise Exception("Input was not a number")
    
    # Check the constraints for the number of words
    if num_of_words < 1 or num_of_words > 10**5:
        raise Exception("Number of words out of range")
    
    # Initialize an ordered dictionary to preserve input order
    word_dict = OrderedDict()
    
    # Read the words and count occurrences
    word_len_acc = 0  # Accumulator for total length of words
    for _ in range(num_of_words):
        try:
            word = input().strip()
        except EOFError:
            raise Exception("Unexpected end of input")
    
        # Check constraints: lowercase English letters only
        if not word.islower() or not word.isalpha():
            raise Exception("Words must contain only lowercase English letters")
    
        # Update word counts
        word_dict[word] = word_dict.get(word, 0) + 1
    
        # Update total length of words
        word_len_acc += len(word)
        if word_len_acc > 10**6:
            raise Exception("Sum of word lengths cannot exceed 1,000,000")
    
    # Output the results
    print(len(word_dict))  # Number of distinct words
    print(" ".join(map(str, word_dict.values())))  # Occurrences of each word
    
  • + 0 comments

    I noticed that noone (including the test cases themselves) do any kind of validation. Not even against the constraints clearly noted in the problem itself?

    Is there a reason for this?

    I've tried submitting the below code, but it fails a few of the test cases. Unfortunately, I can't check why as the custom test input does not accept the input from the test cases due to size constraint. This tells me that for some of the test cases, the constraints are not met, and the test cases should have failed?

    import re
    
    try:
        num_of_words = int(input())
    except ValueError:
        raise Exception("Input was not a number")
    
    if num_of_words < 1 or num_of_words > 10**5:
        raise Exception("Too many words")
    
    try:
        word = input().strip()
    except EOFError:
        raise Exception("No input")
    
    word_dict = {}
    word_len_acc = 0
    while word:
        if re.match(r".*([A-Z]).*", word) or re.match(r".*([0-9]).*", word):
            raise Exception("No capital letters are allowed")
    
        if word not in word_dict.keys():
            word_dict[word] = 1
        else:
            word_dict[word] = word_dict[word] + 1
    
        word_len_acc += len(word)
        if word_len_acc > 10**6:
            raise Exception("Sum of word lengths can not exceed 1,000,000")
    
        try:
            word = input().strip()
        except EOFError:
            break
    
    
    print(len(word_dict.keys()))
    for num in sorted(word_dict.values(), reverse=True):
        print(num, end=" ")
    
  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    # Read all input at once
    
    k = input()
    dic = {}
    key_count = 0
    s=""
    
    for _ in range(int(k)): 
        stri = input()
        if stri not in dic : 
            dic[stri] = 1 
        else: 
            dic[stri]+=1
    print(len(dic))
    for i in dic.values():
        s+=str(i)+" "
    print(s)
    #for key,value in dic.items():