New answers tagged

2

First I got same approach than previous answers (@Roland Illig & Luapulu) that are very similar. But then I remembered Item 72 of Effective Python (Brett Slatkin's book): 72: Consider Searching Sorted Sequences with bisect. Python’s built-in bisect module provides better ways to accomplish these types of searches through ordered lists. You can use ...


9

Your code looks well-formatted, it's easy to read and to follow, and the explanation you gave matches the code exactly. Well done. :) for item in my_list: This statement looks strange since in the body of this for loop, you neither use item nor my_list. You can express the idea of that code more directly: for _ in range(len(my_list)): The variable _ is ...


12

Since the goal is the best possible implementation of this algorithm, I'd suggest the following. However, faster algorithms do exist. To conform to PEP8 make sure you have two blank lines after your imports and surrounding function definitions. Since, you aren't editing each item, but rather adding and removing items until the list is sorted, I'd use the ...


3

well done providing doc strings sticking to the Style Guide for Python Code makes Python code easier to grasp, especially for someone who didn't write it to make the naming more convincing, you should factor out sequential_deterministic_stand_in_for_seeing_the_required_result() and sequential_deterministic_stand_in_for_checking_the_result() As a bonus, this ...


1

So why is the second sort slightly faster [...] On my machine, the first algorithm is "faster" (1.588s vs. 1.62s). The number of samples you have taken (1) is too low to draw conclusions. For significant statistics, you should run at least 20 times and then compare the average execution time. [...] slightly faster [...] The difference between your two ...


2

If the list never changes once loaded, the data would only ever need to be sorted one time (which ever you prefer as the default). Once sorted, you can just reverse the array as you currently are. Additionally, if your data comes from a database of sorts, you may not even need to sort on the client at all, you could return an already sorted resultset. So ...


1

If it works as expected, there's nothing wrong with it. However to improve the readability, I'd separate the setState call from actual sorting. Also be careful when you change the order state variable and when you use that variable to determine the sort order. orderUsers() { if (this.state.order) { return this.state.users.sort((a, b) => { if (...


2

Instead of resorting the list in state just store the preferred order in state. Then in render simply sort or reverse the order as needed to present the data. Be sure to use keys that don't change when the order is changed and it'll be quite performant.


2

Without knowing anything more about the nature of G or H, there isn't much to go on. This might not be optimized, but it is more readable. It takes as arguments the sequence output by G(k) and the function H. from collections import defaultdict def f(sequence, is_equivalent): """returns the number items in each equivalence class in the input sequence"...


2

Let me try and illustrate the difference naming and docstrings make - ah, well, iterating over sequences and for…else, as well: def collatz_list(k, s=[]): """ append the list of collatz values starting from k to s. """ s.append(k) return s if k <= 1 else collatz_list( k//2 if 0 == k % 2 else k * 3 + 1, s) def equivalent(l, r): """...


Top 50 recent answers are included