Sollicitatievraag
Sollicitatiegesprek voor de functie Production Engineer
-
MetaTake a paragraph as Input and output the top three most repeated words
Antwoorden op sollicitatievragen
5 antwoorden
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 op
#/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 op
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 op
for word in $(cat paragraph.txt);do echo $word;done|sort|uniq -c|sort -n|tail -3|awk {'print $2'}
Anoniem op
for i in `cat input. txt` ; do echo $i |sort|uniq - c|sort - r| head - 3
Anoniem op