Hot answers tagged

7

Redundant processing Some of the code processes the text multiple times. For example: def unique_words_count(self): return {x : (self.word_search()).count(x) for x in self.unique_words()} scans the text twice and scans a list of all the words: self.word_search() calls word_pattern.findall() .count(x) scans the list of words self.unique_words() calls ...


6

3x to 4x speedup using str.translate() Based on a quick test (see below), str.translate() is an order of magnitude faster than a regular expression for replacing a single character with another character or short string. So, use str.translate() to take care of most of the substitutions and just use the regular expressions for the few complex patterns. ...


4

Instead of diving into cumbersome and lengthy regex expressions consider the following improvement/correction: The main thesis for the underlying aspect is: Numeric literals containing a decimal point or an exponent sign yield floating point numbers. https://docs.python.org/3.4/library/stdtypes.html#numeric-types-int-float-complex Therefore Python ...


3

I'd just go with @Roman's suggestion. You should just leave it up to the language to decide what is and isn't valid. I'd make two further suggestions though: I don't think the parameter to is_numeric should be Optional; either conceptually, or to comply with the challenge. None will never be a valid number, so why even check it? I don't think dealing with ...


3

The regular expression is inadequate for real domain names. Here's a few actual examples that fail: http://hes.scot/ https://WWW.historicenvironment.scot/ https://www.bbc.co.uk https://WWW.BBC.CO.UK These are extracted as http://hes.sco https://WWW.his https://www.bbc.co https://WWW.BBC Domain names are case-insensitive, can contain any number of levels,...


3

Variable Names Variables should be lower-cased and snake-case, meaning that multiple words should look like some_variable, not someVariable or SomeVariable. I think the name you've chosen for your regex is good and expresses exactly what it is: protocol_regex = re.compile(...) Checking length of a list/container data structure It is not considered ...


3

Your code is somewhat messy because almost everything is inside a single Program class. It can use some abstractions. Few things off the bat: 1) Declare and implement a strongly typed api for your service. For example: interface IChuckService { Joke[] GetRandomJokes(Category category); Category[] GetCategories(); Name[] GetNames(); //other ...


2

The following is a basic syntax and usage pass without looking into your algorithm in too much depth. I'll first show the suggested code and then highlight significant differences where it can offer improvements on the original. #!/usr/bin/env python3 from typing import Iterable, Set def tokenize( text: str, vocab: Set[str], ...


2

Welcome to Code Review! Bad naming The method doesn't actually check that a URL is valid, as it allows values that are not URLs. Thus it should not be named as "isValidURL". It should be isValidAddress or similar, but not URL. URLs have a very well defined syntax. Unnecessary variables There is no need to store the return value of specialCharactersExists(...


2

Don't do using namespace std;. inline is really only useful for defining non-template functions in header files. Modern compilers generally decide what functions to actually inline themselves. To use std::isspace correctly we must cast the argument from a char to an unsigned char before passing it into the function. std::regex_match has a version that takes ...


1

That regex visualisation you provided is really neat. It shows that there is a lot of potential overlap in the conditions. You should be able to reduce it down to something similar to this: ^[+-]?\d+(\.\d+)?([Ee][+-]?\d+)?$


Only top voted, non community-wiki answers of a minimum length are eligible