Binary search tree effectively acts as linked list when items are added in an ordered way
By : user3806034
Date : March 29 2020, 07:55 AM
wish of those help If you don't want to use a self-balancing tree (red-black, AVL, etc.), then the "easy" solution is to just shuffle the collection before inserting it into the tree. If you want a perfectly balanced tree, you could start with a sorted collection, then insert the middle element of that list into the tree and recursively do the same with the two remaining sorted subcollections.
|
I am trying to remove items from a list based upon items from another list (inside a loop). error: list index out of ran
By : Vivek Pandey
Date : March 29 2020, 07:55 AM
I hope this helps you . Your problem is that you change the size of the list during iteration. Which obviously is a problem since after deleting a few items your j loop variable is going to be outside the range of the new (after deletion) list length. The first time it only works because the list only contains 1 element. Try this instead: code :
oc = [item for item in oc if item not in remlist]
|
How to CPU effectively add item to a list of unique items
By : Ralf Dragon
Date : March 29 2020, 07:55 AM
Hope that helps Almost each language has a list that is optimized for holding only unique values. In C++ you could use a std::set instead of a list. In C# you would use a HashSet. In JavaScript you would use an object... In your question you're doing a O(N) lookup for each element, a set or unique list will at least do a O(log(N)) which is many times faster.
|
How to compress/archive a temperature curve effectively?
By : Meghna Bhave
Date : March 29 2020, 07:55 AM
To fix this issue A simple approach would be code the temperature into a byte or two bytes, depending on the range and precision you need, and then to write the first temperature to your output, followed by the difference between temperatures for all the rest. For two-byte temperatures you can restrict the range some and write one or two bytes depending on the difference with a variable-length integer. E.g. if the high bit of the first byte is set, then the next byte contains 8 more bits of difference, allowing for 15 bits of difference. Most of the time it will be one byte, based on your description. Then take that stream and feed it to a standard lossless compressor, e.g. zlib.
|
Rearranging list items based on a score to fit a function curve
By : A Majhi
Date : March 29 2020, 07:55 AM
it fixes the issue I'd do sth along these lines. Sort the words by their points, take every second out, reverse that half and concat the two: code :
>>> s = sorted(zip(map(int, points), words))
>>> new_words = [word for p, word in list(reversed(s[::2])) + s[1::2]]
# If you have lots of words you'll be better off using some
# itertools like islice and chain, but the principle becomes evident
>>> new_words
['apple', 'car', 'older', 'values', 'exponential', 'coefficient', 'average', 'man', 'pear']
[(9999, 'apple'), (8231, 'car'), (4712, 'older'), (500, 'values'), (5, 'exponential'), (10, 'coefficient'), (3242, 'average'), (5123, 'man'), (9231, 'pear')]
|