New answers tagged

-2

Here is my quick solution. def summer_sum(a_list): if len(a_list)==0: return 0 elif 6 in a_list: return sum(a_list) - sum(a_list[a_list.index(6):a_list.index(9)+1]) else: return sum(a_list) pass


0

If complexity is getting to high for a method it's usually a good idea to split out helper methods. If you end up to pass in the same parameters to each of these methods, it could also be a good idea to extract a class. Some solution could be def check_for_next_section(line, current_section) ZoneCheck.new(line, current_section).run end class ZoneCheck ...


2

There's a lot of good feedback on how to work with the data as it's structured now, but my opinion is that - as soon as humanly possible - you should deserialize it out of a collection of weakly-typed dictionaries and lists, to a set of classes. This will make a handful of things better-structured, more testable and verifiable, and more easily maintainable ...


5

Ignoring the fact that your particular code doesn't actually need a nested loop, in general I would say that nested comprehensions can be fairly readable, but helps a lot to put each for on a line of its own: return [ some_function_of(item, product) for item in order['order']['items'] for product in pricing['prices'] if some_condition_on(item,...


6

Readability comprehensions have advantages they may be short one liners that are more readable (in the context code) than explicit loops they may be more efficient however - when done wrong they tend to be unreadable and thus unmaintainable. Yours is near unmaintainable. It did take me a little time to identify some of your code is superfluous. Your ...


7

As a general rule it is best not to nest comprehensions. It makes code hard to read. You are better off just writing a for loop and appending the results to a list or using a generator. Here are a couple rules with comprehensions that will make your code less brittle and easy for others to work with: Don't nest comprehensions. If a comprehension is too long ...


1

Given there are only 22-28 tiles in a Hive game, the time complexity of the check probably doesn't matter much. Nevertheless, a modified scanline fill algorithm might be faster than a DFS. This answer explains the algorithm fairly well. In this use case it might be easier to scan down the columns instead of across the rows. From the rules of the Hive game, ...


1

I can see the problem with your code. Why This "mid = right // 2 " ? You can try below code def binarySearch(arr, low, high, key): if high >= low: mid = (high + low) // 2 if arr[mid] == key: return mid elif arr[mid] > key: return binarySearch(arr, low, mid - 1, key) else: ...


1

snake_case The PEP8 standard is for these: getTargetFileNames currentWorkingDir to be get_target_filenames current_working_dir and so on for your other functions and variable names. Fail-safe directory reversion os.chdir(currentWorkingDir) should be in a finally, with the try after the first chdir. That said, you shouldn't be changing the working ...


0

I would not give you the exact solution to this problem rather I would help you to get an intuition on how to solve this question. If we try to generalize count of the number of times a particular number at index i is getting added and the number of times it is being subtracted then for every index i we can use that mathematically derived formula to compute ...


Top 50 recent answers are included