6 ms·
Oh, we can use our favorite language in our new job? Here is a Python solution. from collections import Counter Counter(s1.split(' ')).most_common(10)
by shill 13y ago
Oh, we can use our favorite language in our new job? Here is a Python solution.
from collections import Counter
Counter(s1.split(' ')).most_common(10)
- untog 13y agoWell, sure, once you're allowed to use external libraries anything is a one line solution. In JS: doStuff = require("doStuff"); var result = doStuff(theString); isn't JS so efficient?!?
- jkrems 13y agoIt looks like collections is part of the standard library: http://docs.python.org/2/library/collections.html http://docs.python.org/2/library/collections.html. I think this disqualifies your JS solution, as long as doStuff isn't some fancy nodejs core module I missed.
- untog 13y agoI think the point still stands though, when you think of it as an interview question. The point is to get to the bottom of how the candidate would process such a query themselves, not to test their knowledge of the core libraries of their favourite language. I feel like that's what the post author meant when he said that LINQ had "ruined" the question- because it kind of does the same thing.
- cben 13y agocollections.Counter.most_common() uses heapq.nlargest() which is slightly more efficient than a full n*log n sort. But implementing a heap correctly from scratch is I guess outside the scope the OP intends for the interview question. I would expect a good candidate to: 1. know that a heap is optimal here (remembering whether Counter uses it is optional) 2. express reluctance to implement it from scratch because surely the stdlib can do it better. So IMHO using libraries like this ruins the question only in the sense of showing its not a challenge for the candidate. Something so simple should not take a page of code.
- klipt 13y ago> collections.Counter.most_common() uses heapq.nlargest() which is slightly more efficient than a full n*log n sort. Creating a heap is O(n) if I remember correctly, so that may well be the most efficient solution.
- rhizome 13y agoIsn't it kind of axiomatic that any question that provides for a concise and measurable solution will eventually be refactored into a library? It seems like kind of a weakness in these kinds of "programmer" questions, that they all regress to trivia. Why not an open-ended business problem and/or deliverables-type question? I'm sure that would speak more to the daily job, where I've never heard of someone being tasked with "OK, find the top 10 words by frequency in a random Wikipedia article."
- untog 13y agoSurely the 'trivia' in this instance is knowing the names of libraries, as opposed to be able to think through programming concepts? Just like math homework back in the day, the teacher didn't do it to check your answer, they did it to check how you arrived at your answer.
- rhizome 13y agoMy memory of the math homework you describe brings up two images: geometry proofs and story problems. Never did I have to explain multiplication when doing my times tables, nor quadratics.
- CanSpice 13y ago"Sorry, we're stuck with Python 2.3.7 and collections isn't part of the standard library."
- wiredfool 13y ago"""Thank you, I think I've heard enough. I don't think continuing the interview would be a productive use of time."""
- kenko 13y agoBecause of an older version of python?!
- wiredfool 13y agoIf they're stuck on a version of software that hasn't been supported in 5 years for bugs or security with a set of features that was originally released nearly 10 years ago, it means that there are some culture issues in the workplace that will most likely drive me insane. Therefore, I am unlikely to take the job.
- pjmlp 13y agoMost companies are not startups, installing software as soon as new releases get available. Just to give another example, our consulting company still gets requests for projects to be deployed against Java 1.4!
- yen223 13y agoI like the fact that you triple-quoted that statement.
- archangel_one 13y agocollections isn't an external library. It's part of the Python standard library.
- deleted 13y ago[deleted]
- cavilling_elite 13y agoAgreed, if I was looking for a candidate that will always "reinvent the wheel" then I would want them to implement from scratch. I find it perfectly valid for the interviewee to say "Hey! I wrote a module for <language> for this exact purpose! Want to check out my github page?".
- shill 13y agoWithout imports: d = {} for word in s1.split(' '): try: d[word] += 1 except KeyError: d[word] = 1 print [(x, d[x]) for x in sorted(d, key=d.get, reverse=True)][:10]
- recuter 13y agoIn case anybody was wondering for a second when JS got so awesome -- it didn't, this is a snippet of Python. D'oh. Sigh.
- 1wheel 13y agoJS isn't that bad if w/ underscore or d3: d3.entries((s.split(" ").reduce(function(p, v){ v in p ? p[v]++ : p[v] = 1; return p;}, {}))) .sort(function(a, b){ return a.value > b.value; }) .map(function(d){ return d.key;}) .slice(-10);
- deleted 13y ago[deleted]
- spenuke 13y agoI'm a newbie, but the question seemed approachable so I went for it. This is what I came up with. (Python, btw.) def top_ten(s): words = s.split(' ') word_list = set(words) return sorted(word_list, key=lambda x: words.count(x))[:10] The question didn't ask for word counts, so I didn't see the need for a dictionary. I'd appreciate any advice on my solution. I'd be thrilled if I'm not too far off from being capable of starting to apply for jobs.
- jessedhillon 13y agoThis is a great approach. Even better because you carefully examined the requirements and found an interpretation which allowed you to make an optimization (words only, not counts, therefore sets are useful)
- 13y ago
- tracker1 13y agoMy first thought after LINQ was a JS or CoffeeScript Map/Reduce/Sort/Splice.
- mzs 13y agoI find it silly everyone arguing below but when: s1 = 'a a a a a a a a a a a a a a a a A A A A A A A A A A A A A a. a. a. a. a. a. a. a. a.' those two lines give: [('a', 16), ('A', 13), ('a.', 9), ('', 1) (HN might collapse the double space in s1)
- jonny_eh 13y agoIndent with four spaces for any bits of text you want quoted and monospaced on HN. Like this here
- carlob 13y agoIn a similar spirit, but without libraries and with a programming language designed 25 years ago. Commonest[StringSplit[string], 10]
- mzs 13y agoTo be fair, Commonest appeared in Mathematica 6.0 about six years ago.
- Myrth 13y agoPfft <?php function top10($s) { $a = array_count_values(preg_split('/\b\s+/', $s)); arsort($a); return array_slice($a, 0, 10); }
- tracker1 13y agoYou managed to create an example of why I don't care for PHP... array_count_values(...) arsort(...) array_slice(...) Thinking.. two of these things belong together, two of these things are kind of the same.. but one of these things is doing his own thing... Seriously built in method naming and argument ordering don't follow any consistent convention in PHP, that's always been the most irksome thing to me...
- Myrth 13y agoWelp oops: <?php function top10($s) { $a = array_count_values(preg_split('/\b\s+/', $s)); rsort($a); return array_slice(array_keys($a), 0, 10); }
- sterling312 13y agomeh, just use sum instead of counter sorted([(i,sum([i==j for j in s.split()])) for i in list(set(s.split()))],key=lambda x:x[1],reverse=True)[:10]