New answers tagged

2

In response to question 1, in a REPL, help(obj) returns the docstring for the object. So the docstring should contain information for a developer using the object (class, function, module, method, ...). In response to your 3rd question, your method of scanning is fairly simple to understand and implement. However, it is not very efficient. It potentially ...


2

In my opinion head_starts_with_one_from would be cleaner by using a generator expression: def head_starts_with_one_from(match_targets: Iterable[str]) -> Optional[str]: return next( ( m for m in match_targets if expression.startswith(m, i) ), None ) Or better yet, a plain old for loop. ...


0

def cleanWord(word): # To remove punctuations The comment is a better name without_punctuation. Also you should be following snake case as per PEP-8. Much of your logic in the longest word function can be handled by max with a key function. The overall solution can be written like so. def without_punctuation(text): ...


2

correct - As far as I can tell, yes. concise - A filter() followed by a .length (or .size) can be simplified: large.sliding(small.length).count(_.diff(small).isEmpty) fast - Could be faster, but it wouldn't be as concise. Consider the following: findPermutations("abcxcba", "cab") Under the current design that would be 5 invocations of _....


2

Really nice style; super clear. I'll try not to duplicate existing and really good answers. What I think you intend to do is a kind of state machine looking something like this: The error, in this case, includes stdin EOF before return was pressed. However, you are only checking for EOF and ignoring other errors, as pointed out. I believe that the fgets, ...


6

pcmpistri This seems like the obvious choice searching withing a string. However, while pcmpistri is very general/powerful, it is also not very fast. On typical Intel processors it consists of 3 µops that all go to execution port p0 (therefore limiting this loop to at best running one iteration every 3 cycles), on AMD Zen(1/2) it's slightly less bad coming ...


3

Does not detect input errors on stdin When fgets() returns NULL due to an input error, code simply loops when a loop exit is more common. If the input error is permanent, code is stuck in a infinite loop. Avoid naked magic numbers Rather than 12, 11, etc, use a #define BUF_N 12 and code accordingly. // while (!fgets(v, 12, stdin) || strcspn(v, "\n"...


1

IMO a much simpler solution is to use POSIX getline(). And if your platform doesn't provide getline(), there are more than a few open-source implementations available. Bare code to illustrate the processing in a smaller number of lines (as in without scroll bars...): int main() { char *line = NULL; size_t len = 0; for ( ;; ) { printf(...


4

This would obviously be better as a function, so we can call with different parameters. Instead of repeating the constant 12 in the fgets() call, we could simply use sizeof v. Then it remains consistent if we change v. Instead of strcspn() with only one character, I'd prefer strchr(). Conveniently, that returns a null pointer if not found, so we don't need ...


0

For python 3.6+ you can use f-strings: ', '.join(f'{k}_{v}' for k, v in my_dict.items())


0

You can use this expression [\s+\*+]+, and use it in, for example "*** RESIDENT * * EVIL**** " Solution: "RESIDENT EVIL" n = s.replaceAll("[\s+\*+]+", "");


2

A short review; You want to put this in a function That function should either create the numeronyms or do the outputting, not both You are using && as an if statement, great for code golfing, not so great for code review Using .map() doesnt really make sense if you are not actually mapping, .forEach() makes more sense If you insist on split and map ...


2

Know the language Part of being a programmer is being familiar with the language/s you are using. These days languages are changing yearly thus keeping up to date is very important. MDN JavaScript provides a good JavaScript reference. Every now and then its good to peruse the site to keep your knowledge up to date. Strings String.slice will slice to the end ...


2

The current implementation of isAlpha ... let isAlpha = (char) => { // match any alphabetic character. let pat = /[a-zA-z]/igm; if(char.match(pat)){ return true; } else { return false; } }; ... can be refactored into something as short as this ... function isAlpha(char) { return (/[a-zA-z]/).test(char); }; ... or maybe even that ... ...


Top 50 recent answers are included