Skip to content

Instantly share code, notes, and snippets.

@mittsh
Last active August 29, 2015 14:20
Show Gist options
  • Save mittsh/7a3b57e6181e38338336 to your computer and use it in GitHub Desktop.
Save mittsh/7a3b57e6181e38338336 to your computer and use it in GitHub Desktop.
Paw Coding Challenge #1

Paw Coding Challenge

English

Count the N most frequent words

You get N words, your goal is to find the k most frequent words.

Input

The program gets text in input, line by line. To read the input you can either (your choice) :

  • read from the standard input STDIN (if your language allows it), or
  • use a pseudo-function readline() you assume will return the next line, or an empty string when it reaches the end of file, or
  • takes a list / array of lines

First line gets the number N of words to read. The next N lines each contains one word. The last line contains the number k of most frequent words to return.

Output

Print the k most frequent words by decreasing frequency (by decreasing number of occurrences). If two words have the same frequency, order them in lexicographical order. optional, bonus.

Example

Input:

14
Fee
Fi
Fo
Fum
Fee
Fo
Fee
Fee
Fo
Fi
Fi
Fo
Fum
Fee
3

Output:

Fee
Fo
Fi

Constraint

You can assume that input is limited to 100k words 0 < N < 100000 and that each word has less than 20 characters 0 < len(word) < 20.

French

Compteur des N mots les plus fréquents

Tu as N mots, le but est de trouver les k mots les plus fréquents.

Entrée

Le programme prend du texte en entrée, ligne par ligne. Pour lire l'entrée tu peux soit (au choix) :

  • lire depuis l'entrée standard STDIN (selon le language choisi), ou
  • utiliser une fonction imaginaire readline() qui retourne la ligne suivante ou une chaine vide pour marquer la fin, ou
  • prendre en entrée une liste / array de lignes

La première ligne contient le nombre N de mots à lire. Les N lignes suivantes contiennent chacune un mot. La dernière ligne contient le nombre k des mots les plus fréquents à chercher.

Sortie

Affiche les k mots les plus fréquents par ordre descendant de fréquence (nombre d'occurences). Si deux mots ont la même fréquence, les ordonner par ordre alphabétique optionnel.

Exemple

Entrée :

14
Fee
Fi
Fo
Fum
Fee
Fo
Fee
Fee
Fo
Fi
Fi
Fo
Fum
Fee
3

Sortie :

Fee
Fo
Fi

Contraintes

Tu peux considérer que l'entrée est limitée à 100,000 mots 0 < N < 100000 et que la longueur de chaque mot est limitée à 20 charactères 0 < len(word) < 20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment