Live data from Hacker News

LINQ Ruined My Favorite Interview Question

scottchamberlin.tumblr.com

141–150 of 218 posts

Re: LINQ Ruined My Favorite Interview Question

#141
post #125
post #19

Earlier quoted context omitted.

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)

In a similar spirit, but without libraries and with a programming language designed 25 years ago. Commonest[StringSplit[string], 10]

To be fair, Commonest appeared in Mathematica 6.0 about six years ago.

Re: LINQ Ruined My Favorite Interview Question

#142
post #4

LINQ tends to get thought of as "database syntax sugar", but it's way more than that. It's C#'s version of the lazy collection operations you find in most functional languages, just given friendlier sqlish names. (IE, "Select" instead of "map" and "Where" instead of "filter") I almost feel a bit gross when I have to write a "foreach" loop at this point, because there's almost always an equivalent way to do it in LINQ…

[deleted]

Re: LINQ Ruined My Favorite Interview Question

#143
post #113
post #19

Earlier quoted context omitted.

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)

I 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)

Indent with four spaces for any bits of text you want quoted and monospaced on HN.

    Like      this      here

Re: LINQ Ruined My Favorite Interview Question

#144
post #19
post #8

I hate to be the bearer of bad news, but I think there may be even simpler solutions to this problem: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) The above is a Clojure one-liner example that I believe satisfies the original problem. So while LINQ may have simplified from the C-language family solutions he had seen, it's clearly possible to take it one step further with the…

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)

Pfft <?php function top10($s) { $a = array_count_values(preg_split('/\b\s+/', $s)); arsort($a); return array_slice($a, 0, 10); }

Re: LINQ Ruined My Favorite Interview Question

#145
post #138
post #105

Earlier quoted context omitted.

For a sys-admin job: tr 'a-z' 'A-Z' | sed 's/[^A-Z][^A-Z]*/\ /g' | grep -v '^$' | sort | uniq -c | sort -nrk1 | head -10 edit: I don't know enough about HN,there should be a newline after the backslash in the sed command.

This is too complicated... echo $sentence | rs -T | sort | uniq -c | sort -rn|head -10

Ooh that's a clever use of reshape, but wrong sadly. Yet you're right I could make it simpler, it was just what came first to mind, I did not even check it. Imagine typical sentences:

  $ echo Foo foo foo. | rs -T
  Foo
  foo
  foo.

  $ echo Foo foo foo. | tr 'a-z' 'A-Z' | sed 's/[^A-Z]/\
  /g' | grep -v '^$'
  FOO
  FOO
  FOO
Also the link mentioned using wiki articles for testing, so they would have paragraphs and that's where reshape shines for things like emails, but fails here:

  $ cat foo
  Foo
  
  foo

  foo.
  $ 
That that's though makes me think, should I treat contractions special? What about plurals? It's starting to get silly now.

Re: LINQ Ruined My Favorite Interview Question

#146
I'm not sure that language features making code more succinct really ruin the question.

The C# isn't even that succinct compared to doing a similar thing in other popular languages. For example, in Haskell:

  topTenWords :: String -> [String]
  topTenWords = take 10 . map fst . sortBy (flip (comparing snd)) . map (\l -> (head l, length l)) . group . sort . words

Re: LINQ Ruined My Favorite Interview Question

#148
post #64
post #31

Earlier quoted context omitted.

Without 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]

In case anybody was wondering for a second when JS got so awesome -- it didn't, this is a snippet of Python. D'oh. Sigh.

JS 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);

Re: LINQ Ruined My Favorite Interview Question

#150
post #31

Earlier quoted context omitted.

Without 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]

Or, use a defaultdict(int), or more in line with yours: d[word] = d.get(word,0) + 1 dictionary.get is quite useful. Also, I'd consider s.split(None), instead of s.split(' '). It will group whitespace, so that any double space or other whitespace is collapsed into one delimiter.

or just `s.split()`
Post reply on HN