r/dailyprogrammer • u/rya11111 3 1 • Jun 29 '12
[6/29/2012] Challenge #70 [easy]
Write a program that takes a filename and a parameter n and prints the n most common words in the file, and the count of their occurrences, in descending order.
Request: Please take your time in browsing /r/dailyprogrammer_ideas and helping in the correcting and giving suggestions to the problems given by other users. It will really help us in giving quality challenges!
Thank you!
22
Upvotes
3
u/jarjarbinks77 0 0 Jun 30 '12
I would of done this challenge had you provided a standard file for us to use.
In fact, I would do a lot more challenges if the answers/test files were provided so that we were all working towards the expected answer so that we could compare our output to what is expected. Plus having the answers often help the challenge be more understood. It would really help those of us that post code that no one ever comments on, at least we would know we went in the right direction. I think this would be a good idea at least for the EASY challenges.
5
u/ssk Jun 29 '12
One-liner in Ruby.
4
u/derpderp3200 Jun 30 '12
Short bash:
Has a side effect of counting empty lines but meh, it doesn't matter.
3
u/ashashwat Jun 29 '12
Python,
2
u/ixid 0 0 Jun 29 '12 edited Jul 01 '12
A little verbose in D, I'm sure it can be done more elegantly.
A more elegant version:
2
u/mikeoquinn Jun 29 '12 edited Jun 29 '12
Can convert to a script with argument/variables later, but dirty bash:
There's probably a better way to do this with awk, but awk and I don't get along as well as I'd like just yet.
Edit: First time posting here - any tips on not ticking people off are welcome. I'm hoping to hit this sub regularly to see if I can figure problems out in a couple of languages/interpreters, so I can keep myself active across the board.
2
u/opisafig Jun 29 '12
Cheating with a shell script:
It doesn't handle apostrophes correctly though.
2
u/SangriaSunrise Jun 29 '12
J:
2
u/[deleted] Jun 29 '12 edited Jun 29 '12
This isn't the kind of thing I often do in plain C. Fun to mess with hashing, dynamic memory allocation, etc. for once
2
u/muon314159 Jun 30 '12
C++11 code.
If C++98 is desired: (i)remove std::move(), (ii) rename unordered_map to map, (iii) delete the <unordered_map> #include header, (iv) convert all range-based for loops to normal for loops iterating through the containers, and (v) replace all uses of auto with the proper types.
If the file cannot be open, then no output is produced.
Please note that all words that appear with the same frequency are always output together as a group. As a result it is possible for >N words to be output iff there are ties at the last frequency level. If this is undesired then add an:
statement after the line:
in the loop that outputs each word.
As the output is unspecified, the frequency count is output first followed by the words at that frequency.
Using test.txt as follows:
The following run outputs occur (where $ is a Unix/Linux command prompt):
2
u/[deleted] Jun 30 '12 edited Jun 30 '12
OK GUYS I TRIED! It works. This is in Python.
Please give me criticism! This is only my 5th or so coding project. What should I do differently, why is it so slow, and what cool things could I have done?
1
u/Scroph 0 0 Jun 29 '12 edited Jun 29 '12
PHP, taking it easy on the memory :
Screw the memory :
1
u/[deleted] Jun 29 '12
Am I going crazy or was this thread a knapsack problem first?
1
u/onmach Jun 29 '12 edited Jun 29 '12
Wrote this in haskell. I wanted to learn to use the conduit library and this was a nice chance. I don't expect anyone to understand it if you aren't fairly intimate with haskell. On the bright side it worked on the first try.
Edit: Modified text2Words to use conduitState instead of ConduitIO.
1
u/mrpants888 Jun 29 '12
My Ruby solution.
1
u/matematikaadit Jun 30 '12
Using Ruby here. The code and the example files available on gist.
1
u/theOnliest Jun 30 '12 edited Jun 30 '12
Here's a Perl version, converting everything to lowercase:
1
u/centigrade233 Jun 30 '12
My first attempt at one of these challenges, done in Python and by no means elegant:
1
u/_Daimon_ 1 1 Jun 30 '12 edited Jun 30 '12
Python.
Solution much like ashashwat, almost don't feel like posting it. But ohh well, it has a bit more full names and doesn't have the bug, at least to my reading of the puzzle, of seeing "Hello." as "Hello." (with period) and not "Hello".
1
u/zane17 Jun 30 '12
Java:
1
u/Arthree Jun 30 '12 edited Jul 01 '12
I used the text of Macbeth as suggested. Also I tried to eliminate "words" (ie, things that were surrounded by spaces but not actually words) from the results.
Autohotkey_L:
And the results:
1
u/emcoffey3 0 0 Jun 30 '12
C#
Usage:
1
u/rxst Jul 01 '12
Another one in Java.
1
u/hmmdar 0 0 Jul 01 '12 edited Jul 01 '12
Solved using Go. On my laptop it finishes in ~650ms for Gutenburg's War and Piece. Without regex striping of .?!" it runs in ~240ms.
I ended up not sorting the list until the very end by pushing the top occurring words to a weighted stack. I want to try this again, but instead of leaving the words in a map until the very end I convert them to an array, sort the array then, pull out a slice of the top N.
/edit: I realized it was not very efficient to do the regex and to lower for each word, so instead i do it per line. This saved me about 200ms in running time.
1
u/H4ggis Jul 01 '12
here's my attempt:
First time posting here as well, this place looks fun.
1
u/Erocs Jul 02 '12 edited Jul 07 '12
Scala 2.9
I'm just learning Scala so there is likely to be a more concise solution.
1
u/cdelahousse Jul 28 '12
Javascript