Cover image for Meta
Logo

Meta

Actieve werkgever

Meta

Een sollicitatiegesprek toevoegen

Sollicitatievraag

Sollicitatiegesprek voor de functie Production Engineer

-

Meta

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

Antwoorden op sollicitatievragen

5 antwoorden

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 op

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 op

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 op

0

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

Anoniem op

0

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

Anoniem op

Voeg antwoorden of opmerkingen toe

Meld u aan of registreer u om hier een opmerking over te maken.