Interview Question

Production Engineer Interview

-Toronto, ON


Take a paragraph as Input and output the top three most repeated words

AnswerAdd Tags

Interview Answers

5 Answers


from collections import Counter def get_most_3_repeated_words(paragraph): counter = Counter(paragraph.split()) return sorted(counter.items(), key=lambda x: x[1])[-3:]

Colin on


#/usr/bin/env python from collections import defaultdict wordlst = [] wordcnt = defaultdict(lambda : 0) paragraph = open('/home/nacho/python_test.txt','r') for line in paragraph.readlines(): for word in line.split( ): wordcnt[word] += 1 del wordlst paragraph.close() mostCommon3 = wordcnt.items() del wordcnt mostCommon3 = sorted(mostCommon3, key=lambda value: value[1], reverse=True) for indx in xrange(3): print "%s: %i" % (mostCommon3[indx][0], mostCommon3[indx][1])

Anonnymous Coward on


A more correct shell answer, which returns the top word first: for word in `cat input.txt`; do echo $word; done | sort | uniq -c | sort -nr | head -3 | awk '{print $2}'

Margo on


for word in $(cat paragraph.txt);do echo $word;done|sort|uniq -c|sort -n|tail -3|awk {'print $2'}

Anonymous on


for i in `cat input. txt` ; do echo $i |sort|uniq - c|sort - r| head - 3

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.