Earlier quoted context omitted.
Since we're already on the JVM, here's my solution for Scala: def top10(s: String) = s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2).take(10).map(_._1)
That's almost identical to what I came up with, but yours is better...I didn't know about the mapValues function. def mostCommon(str: String, num: Int) = { str.split(" ").groupBy { s => s} .map { case (k,v) => k -> v.length }.toList .sortBy { _._2 }.reverse.take(num).map { _._1 } }
LINQ Ruined My Favorite Interview Question
191–200 of 218 posts
Re: LINQ Ruined My Favorite Interview Question
#192I hate to be the arrogant know it all on Hacker News, but seriously, if you're writing c# and not using LINQ all the time, you need to catch up. I'm tired of seeing people answer interview questions with anything _other_ than LINQ.
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…
Re: LINQ Ruined My Favorite Interview Question
#193Earlier quoted context omitted.
Because of an older version of python?!
If 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.
Just to give another example, our consulting company still gets requests for projects to be deployed against Java 1.4!
Re: LINQ Ruined My Favorite Interview Question
#194Earlier quoted context omitted.
LINQ is not a magic bullet. It has some serious pitfalls. I'd like to aee people using the right tools for the job.
LINQ is a great mechanism for expressing complex operations in a succinct manner. Sure, LINQ may not be a magic bullet and can be abused, but you know the old adage about premature optimisation. Perhaps you could elaborate on "It has some serious pitfalls.".
Re: LINQ Ruined My Favorite Interview Question
#195Earlier quoted context omitted.
LINQ is not a magic bullet. It has some serious pitfalls. I'd like to aee people using the right tools for the job.
LINQ is a great mechanism for expressing complex operations in a succinct manner. Sure, LINQ may not be a magic bullet and can be abused, but you know the old adage about premature optimisation. Perhaps you could elaborate on "It has some serious pitfalls.".
Re: LINQ Ruined My Favorite Interview Question
#196So, 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)…
string s,f;
map M;
set> S;
while(cin >> s) {
M[s]++;
int x=M[s];
if(x>1) S.erase(make_pair(x-1,s));
S.insert(make_pair(x,s));
}
auto it=S.rbegin();
int topK=10;
while(topK-- && (it!=S.rend())) {
cout secondfirst
Surely, it could even be more improved with help from lambdas and algorithms.Re: LINQ Ruined My Favorite Interview Question
#197LINQ 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…
I almost feel a bit gross when I have to write a "foreach" loop at this point Agreed. I've transitioned to spending most of my time in JS, and wherever I can I use .map(), but the chaining it's not quite the same as LINQ. Someday I intend to write a library of Array addons to provide GroupBy and so on, but I can't imagine it'll be super efficient.
Re: LINQ Ruined My Favorite Interview Question
#198I'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
For \l -> (head l, length l) I tend to use head * * * length (without the spaces between those stars).
Re: LINQ Ruined My Favorite Interview Question
#199Earlier quoted context omitted.
I almost feel a bit gross when I have to write a "foreach" loop at this point Agreed. I've transitioned to spending most of my time in JS, and wherever I can I use .map(), but the chaining it's not quite the same as LINQ. Someday I intend to write a library of Array addons to provide GroupBy and so on, but I can't imagine it'll be super efficient.
In Underscore, you can do something like: _([ ... ]).map( ... ).filter( ... ).value() It's not exactly extending the native array...but there are far fewer side effects to doing it this way.
Re: LINQ Ruined My Favorite Interview Question
#200Earlier quoted context omitted.
string s,f; map M; set > S; while(cin >> s) { M[s]++; int x=M[s]; if(x>1) S.erase(make_pair(x-1,s)); S.insert(make_pair(x,s)); } set >::reverse_iterator it=S.rbegin(); int topK=10; while(topK-- && (it!=S.rend())) { cout second first
Not bad. I think it would be a little simpler and faster with: while (cin >> s) M[s]++; for (map ::iterator i = M.begin(); i != M.end(); i++) { S.insert(make_pair(i->second, i->first)); } But maybe there's a downside to that approach that isn't obvious to me?