New answers tagged

10

if q in dic is pointless. You initialized dic so that it does have all queries. dic = dict.fromkeys(queries, 0) should be a bit faster. dic is not a particularly meaningful name, counter or ctr would be better. Creating results at the start of the function gives the false impression that it's needed there already. I'd create it right before the loop that ...


8

Any time you are using a dict, and you need to do something special the first time a key is used, take a look at collections.defaultdict(). from collections import defauldict def matchingStrings(strings, queries): results = [] counts = defaultdict(int) for s in strings: counts[s] += 1 results = [counts[q] for q in queries] ...


3

I only have a few points to add to Dannnno's, Sriv's, and Martin Thoma's excellent answers. Consistency Sometimes, you abbreviate string as "str", sometimes as "s", sometimes not at all. Sometimes, you put the type in the identifier, sometimes not. When you put the type in the identifier, you sometimes put it as a prefix, sometimes as a ...


3

It would be helpful for you to break up the code into functions, that will simplify each task. Process the command line arguments first, then once you have the delimiter process the text files. Declare the variable delim above any logic and initialize it to the default value Process the command line to get the actual delimiter. Process the input file. This ...


5

Several general hints: Formatting: Use an auto-formatter. I recommend black. It's wide-spread, maintained by the Python Software Foundation, allows little configuration so the style is consistent among projects (no configuration is good in this case) Linting: Use linters. I recommend flake8 with flake8-bugbear, flake8-comprehensions, ...


5

According to PEP8, docstrings should use """ instead of ''' Both integer_to_reverse_binary and reverse_string are only used once. I'd just replace the usage with the body of the functions. Instead of integer_to_binary_string, I would prefer a shorter name such as int_to_bin. log2 is inaccurate for large values and should be avoided if ...


6

Your implementation seems fine at a glance, but Python actually provides two really easy ways to do this: string.format bin Your function would then become: def integer_to_binary_string(int_value): '''Converts integer to a string of base-2 bits, example: 20 becomes '10100' ''' return f"{int_value:b}" # or ...


4

Other answers have covered the algorithm so this answer will cover other aspects of review. Good things Strict equality comparisons are used indentation is consistent variables have limited scope Bug with object length Let’s look at that first function: function countA(str) { return (str.match(/a/g) || {}).length; } String.prototype.match() returns “An ...


12

The most important insight that can be made here is that when there are as to split by, it's only the span of text between the end of the first match and the start of the last match that matter, and the rest is a simple counting problem based on the substring lengths. Look at a few examples. Input: babaa ba // This is segment between the end of the first ...


6

Your algorithm is cubic against the length of the string, as it uses nested for loops that each run proportionally to the length of the string with a linear time countA function inside them. But it is possible to achieve a linear time algorithm. Note first that you will always be putting the same letter a characters in each split regardless of the split ...


5

If you can use c++17 you can use the stl filesystem, this header contains faculties for performing operations on paths and files and would make your code simpler, for example the delimiter can be handled with the std::filesystem::path class. According to the documentation of std::filesystem::path a path can be normalized by following this algorithm: If the ...


9

TL;DR private String increment(String input){ char[] chars = input.toCharArray(); boolean stopLoop = false; for(int i = chars.length - 1; i >= 0; i--){ char character = chars[i]; switch (character) { case '0', '1', '2', '3', '4', '5', '6', '7', '8' -> { chars[i] = (char) (character + 1); ...


3

Extract some of the logic to methods. As suggested by @bobby, the code in the Increment#increment method can be extracted in more than one method. I suggest that you extract the logic into multiples new methods; this will make the code shorter, easier to read and make the code more testable in any test framework (you will be able to test each new method with ...


8

Because this is an interview question, the most important part is the questions you ask. The questions show that you understand that requirements are seldom complete and know what kind of limitations affect the implementation of the algorithm. The ones that need to be asked here are: How long are the input strings of typical input? How long are the numeric ...


5

static String padLeftZeros(String inputString, int length) { Having no modifier specified means that it is package-private. Personally, I like to pretend that package-private does not exist, as it easily leads to "hidden" coupling between classes, and makes it harder for extending classes to actually utilize the classes. On top of that, you can ...


Top 50 recent answers are included