We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Collections
- Word Order
- Discussions
Word Order
Word Order
+ 0 comments from collections import Counter
def word_order(words): word_counts = Counter(words)
values = ' '.join(str(value) for value in word_counts.values()) dict_len = len(word_counts) return dict_len, values
if name == 'main': n = int(input()) words = [input() for _ in range(n)] result = word_order(words) print(result[0]) print(result[1])
+ 0 comments N=int(input()) s={} for _ in range(N): word = input() if word in s.keys(): s[i] += 1 else: s[i] = 1 print(len(s.keys())) print(*[i for i in s.values()])
+ 0 comments Not sure why the problem was rated 'medium' and why there's a low success rate. I used OrderedDict and it was accepted on first submission, here's my code:
import collections n = int(input()) ordered_count = collections.OrderedDict() uniq_words = 0 for i in range(n): word_list = input() if (word_list in ordered_count): ordered_count[word_list] += 1 else: ordered_count[word_list] = 1 uniq_words += 1 print (uniq_words) val_lst = list(ordered_count.values()) print (*val_lst)
+ 0 comments def main(): n = int(input()) words = {} counter = 0 appearances = "" for i in range(n): word = input() if words.get(word) == None: counter += 1 words[word] = 1 else: words[word] += 1 for x in words: value = str(words[x]) if len(appearances) == 0: appearances = value continue appearances += " " + value print(counter) print(appearances) main()
+ 0 comments Python Counter
from collections import Counter def word_count(words): return Counter(words) def print_word_counts(d): print(len(d)) print(' '.join(map(str,list(d.values())))) if __name__ == "__main__": n = int(input()) words = [input() for _ in range(n)] word_counts = word_count(words) print_word_counts(word_counts)
Load more conversations
Sort 1405 Discussions, By:
Please Login in order to post a comment