algorithm

Optimal way to compute permutations in julia

五迷三道 提交于 2021-02-18 11:38:09
问题 Consider a list [1,1,1,...,1,0,0,...,0] (an arbitrary list of zeros and ones). We want the whole possible permutations in this array, there'll be binomial(l,k) permutations ( l stands for the length of the list and k for the number of ones in the list). Right now, I have tested three different algorithms to generate the whole possible permutations, one that uses a recurrent function, one that calculates the permutations via calculating the interval number [1,...,1,0,0,...,0] to [0,0,...0,1,1,

Why is compare-and-swap (CAS) algorithm a good choice for lock-free synchronization?

筅森魡賤 提交于 2021-02-18 11:22:21
问题 CAS belongs to the read-modify-write (RMW) family, a set of algorithms that allow you to perform complex transactions atomically. Specifically, Wikipedia says that CAS is used to implement synchronization primitives like semaphores and mutexes, as well as more sophisticated lock-free and wait-free algorithms. [...] CAS can implement more of these algorithms than atomic read, write, or fetch-and-add, and assuming a fairly large amount of memory, [...] it can implement all of them. https://en

Mysql count rows using filters on high traffic database

情到浓时终转凉″ 提交于 2021-02-18 10:49:06
问题 Let's say you have a search form, with multiple select fields, let's say a user selects from a dropdown an option, but before he submits the data I need to display the count of the rows in the database . So let's say the site has at least 300k(300.000) visitors a day, and a user selects options from the form at least 40 times a visit, that would mean 12M ajax requests + 12M count queries on the database, which seems a bit too much . The question is how can one implement a fast count (using

Optimizing (minimizing) the number of lines in file: an optimization problem in line with permutations and agenda scheduling

旧巷老猫 提交于 2021-02-18 09:55:12
问题 I have a calendar, typically a csv file containing a number of lines. Each line corresponds to an individual and is a sequence of consecutive values '0' and '1' where '0' refers to an empty time slot and '1' to an occupied slot. There cannot be two separated sequences in a line ( e.g. two sequences of '1' separated by a '0' such as '1,1,1,0,1,1,1,1'). The problem is to minimize the number of lines by combining the individuals and resolving the collisions between time slots. Note the time

When to use low < high or low + 1 < high for loop invariant

帅比萌擦擦* 提交于 2021-02-18 07:34:33
问题 I've read multiple articles including Jon Bentleys chapter on binary search. This is what I understand about CORRECT binary search logic and it works in the simple tests I did: binarysearch (arr, low, high, k) 1. while (low < high) 2. mid = low + (high - low)/2 3. if (arr[mid] == k) return mid 4. if (arr[mid] < k ) high = mid -1 5. else low = mid + 1 Now to find the 1st occurence with sorted duplicates, you'd chance line 3 if condition to continue instead of returning mid as binarysearch_get

Efficient accumulate

故事扮演 提交于 2021-02-18 06:18:11
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

Efficient accumulate

我的梦境 提交于 2021-02-18 06:18:07
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

SHA algorithm generates each time unique hash string for a same key

陌路散爱 提交于 2021-02-18 03:34:26
问题 I know there are lots lots of articles available about hashing and encryption algorithm. I have figure it out from them that use hashing function instead of encryption to store password in the database . So I decided to use SHA-256 algorithm to generate hash key and I am storing that hash key into my server database instead of plain password. Now I am really not able to understand how I should use it, because each time I am passing the same password to generate SHA key it gives me different

Find out if point is inside one of N (possibly overlapping) rectangles in less than O(N)

試著忘記壹切 提交于 2021-02-17 22:08:14
问题 I have an image, and I want to show tooltips when mouse moves over certain rectangular areas. The rectangular areas can be up to 1000. However, just checking each rectangle if the point is in it, which is O(N), makes the interface unresponsive when moving the mouse. Is there a way to do it in less than O(N)? I can sort the rectangles beforehand (I'm assuming it would be needed). The rectangles might be (very rarely) overlapping, but no more than 4-5 rectangles can overlap the same area. In

Wilson's Confidence Interval for 5 Star Rating

蓝咒 提交于 2021-02-17 19:40:15
问题 Wilson's Confidence Interval takes as arguments the values TRUE or FALSE, or "upvotes" and "downvotes" respectively. From these votes it generates a rating. For the purpose of my project, I think WCI is perfect. However, the scalar upvote and downvote is not enough to describe the thing I am rating. That's where 5 star rating comes in, and this is where I need someone to disprove my logic. Now I'm thinking, if I were to implement a 5 star rating with WCI then the following should work without