Interview Question

Production Engineer Interview

-Toronto, ON

Meta

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

AnswerAdd Tags

Interview Answers

5 Answers

1

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

1

#/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

1

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

0

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

Anonymous on

0

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.