Hot answers tagged

21

I think its quite long considering the work it does Your instinct is right - it's quite long. It can be implemented with much fewer lines of code, especially in Python. A solution from @RomanPerekhrest highlights it quite well. is this the efficient solution? From the time complexity prospective, it's not an efficient solution. It's O(n2), where n - ...


14

This is a good opportunity to learn two things, separation of concerns and the standard library module itertools. You have three separate issues, each of which you can put into their own function: Take input from the user Turn it into a sequence of 0 and 1. Find the longest run of 1. For the user input it can be as simple as your one line, or slightly ...


12

The whole idea is achievable with a few lines of code and based on designating segments of 1 's items (repeated consecutive sequence) using 0 segments as separators. See the concise solution below: def find_max_ones(num): if not num: return 0 bin_num = bin(num)[2:] print(bin_num) return len(max(bin_num.replace('0', ' ').split(), key=...


5

A short review; checkBalancedBrackets("([)]") returns true, not sure that was the intention b1 and b2 are not great names, what does the b stand for? Using arrays is not needed, all the code does is counting numbers, you can just use numbers The inconsistent use of curly braces in your if and else if clauses is not good


4

I am a newbie to coding myself, but your logic is kind of hard to follow - probably my problem. You should use slicing operation on the string to get the '0b' removed from bin() function result as i feel like a cleaner method. Here's an alternate logic I came up with count = 0 final_count = 0 concecutive_check = False if __name__ == '__main__': n = ...


3

Since command line arguments are null-terminated, we can avoid strlen() entirely by just running through the string until we hit '\0'; this also gets rid of contin and i (*p > 0x2F && *p < 0x3A) isn't very obvious, I'd just use '0' and '9' to make it easier to understand the intention and to be consistent with the rest of the code You could use ...


3

A few improvements following principles of Cleancode and Divide & Conquer: Split into small tasks, each one a function. Use meaningful names. Leverage the language's power. Split into small tasks, each one a function The challenge's description explicitly names 2 tasks, thus implemented in functions: convert_to_binary(number) ...


3

Think about the problem as a repeated Cartesian product. It allows us to use the very useful product function from the itertools module: >>> list(product('ab', 'AB', 'xy')) [('a', 'A', 'x'), ('a', 'A', 'y'), ('a', 'B', 'x'), ('a', 'B', 'y'), ('b', 'A', 'x'), ('b', 'A', 'y'), ('b', 'B', 'x'), ('b', 'B', 'y')] So your problem can be solved by ...


2

You can check the Flyweight Pattern if you have duplicates in the data. This pattern uses a cache to prevent the recalculation of similar objects. Here is some general recommendation for your code; those changes won’t help for the performance issue, but will help when reading the code and make it shorter. 1) Extract the java.util.regex.Pattern#compile(java....


2

Firstly, great job on creating a solution that works. That is often the first step in creating an optimal solution. But as you may have noticed, making combinations and permutations based on the number of elements in small_strings will start to scale incredibly as you process longer and longer small_strings arrays. Mathematically, your solution is ...


2

For such specific problems as you are facing here I almost always turn to BenchmarkDotNet, because it provide you with an easy what to try out different implementation strategies and various .NET features that might tweak the performance to your satisfaction. Parallel conversion Parallel.ForEach(...) and allocate a fair chunk of the array(s) to each of ...


2

The whole if..else..if..else inside the first loop is senseless because each branch does exactly the same. Using Split('|') and combining the resulting array with one space " " is the same like replacing | by a space. The Regex regx = new Regex(sentenceSeparator); is superflous because you don't use regex at all. The loop for (int i = 1; i < ...


1

I have changed my code to use substring and indexOf to extract the text from each string, which has resulted in a 30% performance increase. The memory usage is still on the high end but the method is on its own thread so there is no noticeable performance drop when the gc runs. Before (With subtitle example) private static void defineBooks(List<String&...


1

To avoid having to compile the regular expressions again and again, you can create a little helper class: public class Patterns { private final Map<String, Matcher> matchers = new HashMap<>(); public Matcher compile(String pattern) { return matchers.computeIfAbsent(pattern, any -> Pattern.compile(pattern).matcher("")); } ...


1

Yes, I think your variable names are a bit long. While it is good to be expressive, you don't want to be pushing your line width beyond the recommended max width if avoidable. I don't know if you need many custom functions here. The shared calls which filter empty and duplicate strings can be a custom call. Otherwise, everything else is single-use. ...


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