New answers tagged

1

The following code part could be inefficient for large sets: .OrderByDescending(x => x.count) .First(); We don't need sorting at all, we need just to find an item with the maximum value. Let's create a couple of extension methods for this. For reference types: private static TSource MaxItem<TSource, TKey>(this IEnumerable<TSource> source, ...


1

You don't need to use a dictionary. Instead, order by the count directly: var champion = test.GroupBy(t => t[1]) .OrderByDescending(g => g.Count()) .First(); Note that this might do the count per group several times while sorting. For a very large number of items, you could improve it by storing the result in a ...


1

This can be simplified by using destructuring assignment into an array, followed by simple assignment into the object obj. While it may not be much more efficient it can eliminate the need to use the forEach method. const val = "someValue,display"; const [columnVal, displayValue] = val.split(","); const obj = {columnVal, display: displayValue === "...


0

Here's the realization with Z-function. It can be solved with prefix function as well. https://pastebin.com/Uu07Ni5b


0

Here is my 4 lines long alternative: var anagrams = from word in words let pair = new { Original = word, AlphabeticallyOrdered = string.Concat(word.OrderBy(@char => @char)) } group pair by pair.AlphabeticallyOrdered into anagram select anagram.Select(@group => @group.Original); Here are my line by line explanations: Iterate through the ...


-1

i had already answered this from within my http://lynx.browser.org/ unbloated hypertext browser within superfast http://www.washington.edu/alpine/ mail commander but http://stackexchange.com/ does not behave like written from humans for humans and rather unfortunately insists on a robot captcha unsolvable on a braille typewriter. i will recreate my first ...


1

I'd write increment = (<|> (Just 1)) . fmap (+1) You can probably also write reverseBySecond = flip $ comparing snd if you don't mind getting EQ for equal pairs.


1

Please don't post random slapped-together lines of code that couldn't possibly compile to CodeReview. We strongly prefer code that compiles, even if all the dependencies aren't available to us. This method will never compile. In canonical Java, there is whitespace after a , String building is typically cleaner when done with a StringBuilder. A simple helper ...


0

With C++, you can use constexpr functions in your case statements to (effectively) switch on (certain) strings. I believe you will need at least C++11 to do this. You might need an even newer version of C++ (not sure about that). Here is an example: #include <stdio.h> constexpr unsigned long s2n ( const char * s ) { return s[0]; } void ape (...


-1

This works for toto@domain.com and also with toto@tutu@domain.com which is an RFC compliant email address. def splitMail(mail: str): parts = mail.rsplit('@', 1) user = parts[0] domain = parts[1] return user, domain


1

Stingy makes many good points, though I'd disagree about calling any variable "i" - in my opinion you should always try to find a meaningful name for anything in your program. In this case, I think the variable represents a character position in the string, so I'd probably call it something like "currentCharPos" (I'd even consider "...


1

Avoid hardcoding translation strings to 16 bytes There's no need to set the size of the strings to 16 bytes, just store them as pointers to const char instead of arrays of char: typedef const char *tr_chars_t[TR_SIZ]; Store all languages in an array For each language you have a separate variable holding the translations strings, and you have a long if-then-...


2

Naming The technique looks like unsharp mask, but performed on the most intense channel of RGB pixel (V component). Although unbrighten_mask might sound like a German word (I don't know any German), I believe it better fits since the code performs similar actions. Unclear usage At the moment the code has contradictory interface. On one hand, it seems like an ...


2

This function has too many responsibilities, as is evident by the number and variety of arguments. We have mixed filesystem and image processing, making a function that's hard to reuse (e.g. in a GUI program, where we might want to preview the result before saving) and hard to unit-test. If we split it, we have smaller, more readable functions. Other ...


4

The biggest simplification and speed improvement would be to avoid using the split function. I would suggest just multiplying the 3-channel image directly. Something like this: cv::multiply(img_hsv, Scalar(1, 1, enhancement_times), img_hsv); Of course, we can’t know for sure which is faster until we compare the running time for each of the alternatives. In ...


2

Improvements: The return type is strange and awkward enough that it needs a comment to explain it! What benefit does returning a reference to an array have over simply returning a pointer to the first element? It would be better to return an object. It might simply contain a pointer and length, but it means you could update it to support dynamically loaded ...


2

First of all, I see two problems with your choice of name for the int variable Iterator. For one thing, it goes against the Java coding convention of variable names starting with a lower case letter. And for another, and this might admittedly be more subjective than the first argument, there exists an interface named Iterator in the java.util package, and ...


0

I'm going to be the heretic in the room... I think your first code sample is just fine and doesn't need any refactoring at all. Let's pretend for a moment that the code lines in question are in wildly different places in the code base instead of one right after the other. When I see NSDictionary *imageKeyValues = @{@"Avatar" : @"apple.png"...


Top 50 recent answers are included