Live data from Hacker News

LINQ Ruined My Favorite Interview Question

scottchamberlin.tumblr.com

201–210 of 218 posts

Re: LINQ Ruined My Favorite Interview Question

#201
post #86

Earlier quoted context omitted.

The OP blog-post doesn't really represent it succinctly in C#. This is the most succinct way I could think of: var top = (from w in text.Split(' ') group w by w into g orderby g.Count() descending select g.Key).Take(10);

Though that will result in a lot more calls to Count(). A let would fix that.

Indeed! It wouldn't be quite as concise though, which was kind of my point :)

Re: LINQ Ruined My Favorite Interview Question

#202
post #173
post #78

Earlier quoted context omitted.

The need for the dictionary shows up when your text gets significantly large. Each call to `words.count` is going to re-examine each word of the text to count 'em up, so, if n is the number of words in the text, and m is the number of distinct words in the text, then this solution is at least O(mn + n log(n)) whereas the dictionary-based solution is O(n log(n)). That is, we're re-reading the word list over and over,…

Isn't the difference between O(mn + nlog(n)) vs O(nlog(n)) running time going to get less significant as the value of n gets larger? I thought the whole point of Big O / asymptotic analysis is that you can ignore lower-order terms and constant factors because they are insignificant for any appreciably large input size. And also because the lower order terms and constant factors vary too much depending on the programm…

I'd never heard of Big O until this thread, but from what I can tell it'll only be when a solution is in the O(logn) that "running time will get less significant as n gets larger". This is simply the way you describe logarithmic growth, so maybe that's where you got confused. Also, wouldn't it be fair to say that a logarithm (nlogn) is a lower order term than mn? In which case your definition stands.

At any rate, I wanted to test this out, so I made a naive benchmark for running these functions. The dict solution was ten times faster (0.0011s vs 0.015s) than the list version with ~1350 words. The dict solution ran in 0.13s at ~162,000 words, while I waited a couple minutes before killing the list version on that input.

Re: LINQ Ruined My Favorite Interview Question

#203
post #177
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.

You can use tr instead of sed and grep, too. Also -k1 and -n 10 are default for sort and head: $ I have a `bins` script that is just `sort | uniq -c | sort -nr`, so that reduces to `tr -cs a-zA-Z '\n' | bins | head`. Unix for Poets, man. It's the shit.

I ALWAYS forget about squeeze, thanks!

Re: LINQ Ruined My Favorite Interview Question

#204

Earlier quoted context omitted.

but seriously, if you're writing c# and not using LINQ all the time You don't sound arrogant, but rather sound naive. I agree that someone who recruits surely should have known about and have experienced LINQ significantly by now, but the notion that you should be using it "all the time" is absolute nonsense. I avoid LINQ. I encourage others to avoid LINQ. It is almost always a sign of bad code. LINQ is syntactical s…

I get what you're saying, I understand it, and I agree with the pitfalls. But I really really really don't agree that one should avoid LINQ. Sure if you don't understand how it works, and how it impact the performance, you should use it less. But don't try and drag the language down because you find it difficult. LINQ is easily one of the best things that have happened to a mainstream language in a long time, and if…

But don't try and drag the language down because you find it difficult.

Oh give me a break. My exact complaint about LINQ is the opposite -- that it makes expensive operations seem simple, which with all innovations in language means that mediocre programmers will start abusing it in all codebases.

LINQ has a place. To say that one should be using it "all the time", however, is exactly as I said -- a canary in a coal mine. The only time I've ever come across the extensive use of LINQ it has been in horrific code.

Re: LINQ Ruined My Favorite Interview Question

#205

Earlier quoted context omitted.

What are the "concepts I'm criticizing"? I suspect that you have absolutely no idea what the context of my comment was, because your diversion on the topic of functional programming is just grossly out of place. LINQ encourages the belief that set operations are free , such that you no longer have to concern yourself with concepts like memoization or appropriate structures. This has nothing to do with functional prog…

You've actually been pretty vague about the concepts you're criticizing, except for "LINQ". Now I can see you were alluding to just the deferred execution part (the "lack of memoization"). Basically, you're saying it's too easy to accidentally perform a computation twice by iterating through a sequence twice. Recurse on that problem and you get an exponential blowup. That is actually a problem mostly unique to LINQ a…

I haven't been vague whatsoever.

Re: LINQ Ruined My Favorite Interview Question

#206

Earlier quoted context omitted.

What are the "concepts I'm criticizing"? I suspect that you have absolutely no idea what the context of my comment was, because your diversion on the topic of functional programming is just grossly out of place. LINQ encourages the belief that set operations are free , such that you no longer have to concern yourself with concepts like memoization or appropriate structures. This has nothing to do with functional prog…

You've actually been pretty vague about the concepts you're criticizing, except for "LINQ". Now I can see you were alluding to just the deferred execution part (the "lack of memoization"). Basically, you're saying it's too easy to accidentally perform a computation twice by iterating through a sequence twice. Recurse on that problem and you get an exponential blowup. That is actually a problem mostly unique to LINQ a…

[deleted]

Re: LINQ Ruined My Favorite Interview Question

#207
post #173

Earlier quoted context omitted.

Isn't the difference between O(mn + nlog(n)) vs O(nlog(n)) running time going to get less significant as the value of n gets larger? I thought the whole point of Big O / asymptotic analysis is that you can ignore lower-order terms and constant factors because they are insignificant for any appreciably large input size. And also because the lower order terms and constant factors vary too much depending on the programm…

I'd never heard of Big O until this thread, but from what I can tell it'll only be when a solution is in the O(logn) that "running time will get less significant as n gets larger". This is simply the way you describe logarithmic growth, so maybe that's where you got confused. Also, wouldn't it be fair to say that a logarithm (nlogn) is a lower order term than mn? In which case your definition stands. At any rate, I w…

Oh, you're right, m is not a constant but also varies somewhat independently of n, so you can't exclude it from the big O notation. And whether mn or nlogn is the lower order of the two terms depends highly on the value of m, which is the number of unique words in the text. A really long text that's just the same word over and over will have a big n but a small m.

Re: LINQ Ruined My Favorite Interview Question

#208
post #185

So, aside from the Clojure, Mathematica, Python, Ruby, Bourne Shell, Haskell, and Scala solutions posted in the other comments, all of which are simpler than the C++, C#, and JS solutions, presented here with some minor cleanups: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) ; llambda Clojure // haakon Scala s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2)…

> Perl 5, perhaps surprisingly, is not simpler:

Here's one.

    perl -0777 -nE '$w{$_}++ for split; say for (sort {$w{$b}  $w{$a}} keys %w)[0..9]'
It is slightly different compared to your version in that each word is printed on a separate line. To print all words on the same line, is just a bit longer:

    perl -0777 -nE '$w{$_}++ for split; $, = $"; say((sort {$w{$b}  $w{$a}} keys %w)[0..9])'

Re: LINQ Ruined My Favorite Interview Question

#209
post #185

So, aside from the Clojure, Mathematica, Python, Ruby, Bourne Shell, Haskell, and Scala solutions posted in the other comments, all of which are simpler than the C++, C#, and JS solutions, presented here with some minor cleanups: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) ; llambda Clojure // haakon Scala s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2)…

[deleted]

Re: LINQ Ruined My Favorite Interview Question

#210
post #185

So, aside from the Clojure, Mathematica, Python, Ruby, Bourne Shell, Haskell, and Scala solutions posted in the other comments, all of which are simpler than the C++, C#, and JS solutions, presented here with some minor cleanups: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) ; llambda Clojure // haakon Scala s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2)…

[deleted]
Post reply on HN