Live data from Hacker News

LINQ Ruined My Favorite Interview Question

scottchamberlin.tumblr.com

151–160 of 218 posts

Re: LINQ Ruined My Favorite Interview Question

#152
post #21
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)

Well, 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 ?!?

My first thought after LINQ was a JS or CoffeeScript Map/Reduce/Sort/Splice.

Re: LINQ Ruined My Favorite Interview Question

#153
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…

The most annoying thing about LINQ is that they use the SQL language. So unless you use C# and LINQ all of the time it's hard to figure out what functions translate to the ones used by every functional language in the world. I wish they would at least alias map, reduce, some, etc.

Many C# users have experience with SQL, but not necessarily with functional programming languages. The number of these users likely far outnumbers the number of C# programmers who know functional programming languages, but not SQL.

It makes sense to use terms that many C# (and SQL) users are already familiar with, rather than terms that they may not know of.

Re: LINQ Ruined My Favorite Interview Question

#154
post #68
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]

I'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…

Ooh, this is fun!

In Ruby, without imports/requires:

    def toptenwords(str)
    	words = str.split
	words.sort_by{|word| words.count(word)}.uniq.reverse.take(10)
    end
or as a one-liner, without any variable declarations in the function scope:

    def toptenwords(str) str.split.sort_by{|word| str.split.count(word)}.uniq.reverse.take(10) end

Re: LINQ Ruined My Favorite Interview Question

#155
post #144
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)

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

You 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...

Re: LINQ Ruined My Favorite Interview Question

#156
post #86
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…

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.

Re: LINQ Ruined My Favorite Interview Question

#157
post #7

/me wonders are candidates allowed to chose their favorite language? man bash | tr '[:upper:] ' '[:lower:]\n' | sed '/^$/d' | sort | uniq -c | sort -rn | head | awk '{ print $2 }' | fmt or do you only hire windows coders?

This is how I do this sort of thing all the time. But, the sort is O(n log n), so it's asymptotically less satisfying.

True, a bag of words would perform better.

man bash | tr '[:upper:] ' '[:lower:]\n' | awk '/./ { bag[$1]++ } END { for(word in bag) { print bag[word], word } }' | sort -rn | awk '{ print } NR>=10 { exit(0) }'

But it would require more typing and thinking.

sure one could do this in awk completely,

  man bash | awk '
  /./ { 
    for (i = 1; i score) {
          score=bag[word];
          best=word
        }
      } 
      printf "%s ", best
      delete bag[best]
    } 
    printf "\n" 
  }'
if the requirement is: please chose one language and not the complete Unix babylon.

Re: LINQ Ruined My Favorite Interview Question

#158
post #151

LINQ is my favorite API of all times. It rocks.

I totally agree. Sure there where similar things a in the funcional world before, but LinQ had some important pros:

- deferred ejecution by default, saving memory and time

- step by step syntax, each new operation is at the end, not the beginning

- excellent type inference and intellisense, js? ruby?...

- it works with the same syntax on the database!!! Haskell?

- map and filter where there, but groupby and join where not so common in previous query comprehensions APIs.

- the most important: it's actually usable in jobs you get paid for, not experiments you can make at home or university.

There are however two things that doesnt make it 100% perfect:

- expression tree lambas are identical to non expression ones, making it hard for developers to know if one step is going to be translated or executed. I would have chosen => for non expression and -> for expressions for example or something like that.

- having two syntax, method chain and query comprehensions, produces a frequent anoying back and forth since some operators are better written in one (let, join, group by) while others are only available in method chain (take, toDictionary...)

Re: LINQ Ruined My Favorite Interview Question

#159
post #144
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)

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

Welp oops: <?php function top10($s) { $a = array_count_values(preg_split('/\b\s+/', $s)); rsort($a); return array_slice(array_keys($a), 0, 10); }

Re: LINQ Ruined My Favorite Interview Question

#160
post #145
post #138

Earlier quoted context omitted.

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 resha…

I think it goes beyond the original question now. :) If one wants to do proper thing, then definitely contractions, word variations, etc should be considered. For this, I guess something like OpenNLP can be used.

What's interesting though is how UNIX shell, being essentially a symbolic FP language, allows one to solve the problem in a clear and concise way. And if one desires, the program can be easily modified to read sentences, say, from network from ssh tunneled via HTTPS. That kind of flexibility can rarely be achieved in languages like C# with tight coupling between the units of abstraction.

Post reply on HN