Hot answers tagged

6

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

Simplify the code The guard statement and the following if let can be combined into a single statement: let key = "Signature" let signatures = iconURL.matches(for: "\(key)[^&]+") if let firstSignature = signatures.first, let range = firstSignature.range(of: "\(key)=") { let signature = String(firstSignature[range.upperBound...]) print(...


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

I'll show an example implementation first, and then describe it: from typing import Iterable import re def case_rank1_naming(proteins_available: Iterable[str], best_match_protein_name: str) -> str: # extract the three-letter pattern protein_pattern = re.search(r"[A-Z][a-z]{2}", best_match_protein_name).group() # extract the numbers ...


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


1

There is only one thing that jumps out at me as whacky... [a-z\d\-_\W] Regex101 breakdown I think this means to match a lowercase substring, but that's not what it is doing. Since \W is the inverse of \w and because \w represents A-Za-z0-9_, I think it is strange that the subpattern is used to replace the lowercase placeholder. As is, your pattern can ...


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