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]
LINQ Ruined My Favorite Interview Question
141–150 of 218 posts
Re: LINQ Ruined My Favorite Interview Question
#142LINQ 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…
Re: LINQ Ruined My Favorite Interview Question
#143Earlier 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)
Like this hereRe: LINQ Ruined My Favorite Interview Question
#144I 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)
Re: LINQ Ruined My Favorite Interview Question
#145Earlier 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
$ 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
#146The 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 . wordsRe: LINQ Ruined My Favorite Interview Question
#147 count = take 10 . map head . reverse . sortBy (comparing length) . group . sort . words
That's ignoring Unicode rules for word splitting of courseRe: LINQ Ruined My Favorite Interview Question
#148Earlier 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.
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
#149 .say for (bag($text.words) ==> sort {-*.value})[^10]Re: LINQ Ruined My Favorite Interview Question
#150Earlier 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.