algorithm

Apple Picking Optimization Algorithm

懵懂的女人 提交于 2021-01-28 08:11:04
问题 I found these problem: Suppose that I have fields with N apple tree, each with A i apple on it. I also have M basket, each basket have the property C i for capacity and F i for flexibility. When I'm going to pick the apple trees I can only pick from tree in order from 1 to N. At each tree I have two options, to pick all the apple on the tree or to expand my basket to increase it's capacity by F. When I expand the basket, I cannot pick the fruit in the that tree. Find the maximum number of

Timing C# code using Timer

我们两清 提交于 2021-01-28 08:01:22
问题 Even though it is good to check performance of code in terms of algorithmic analysis and Big-Oh! notation i wanted to see how much it takes for the code to execute in my PC. I had initialized a List to 9999count and removed even elements out from the them. Sadly the timespan to execute this seems to be 0:0:0 . Surprised by the result there must be something wrong in the way i time the execution. Could someone help me time the code correct? IList<int> source = new List<int>(100); for (int i =

Print number patterns in JavaScript

天大地大妈咪最大 提交于 2021-01-28 08:00:43
问题 I want to print numbers in pattern as below also I need this to print using only one for loop not in if condition inside for loop. If I give s = 7 the output pattern would be 7, 5, 3, 1, 3, 5, 7 If s=6 then output is 6, 4, 2, 4, 6 This is what I tried but not successful. const s = 7, b = 2 for (x = s, d = b; x > 0 && x <= 7; x -= 2) { console.log(x) } I don't want to use any pre-built libraries to achieve this such as Math.abs() 回答1: With ternary operator: const s = 10, b = 2 for (x = s, step

Find all the possible N-length anagrams - fast alternatives

為{幸葍}努か 提交于 2021-01-28 07:43:10
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

How to implement a k-way merge sort?

寵の児 提交于 2021-01-28 07:25:19
问题 I need to implement a function which does a k-way merge sort on an unsorted array or integers. The function takes in two parameters, an integer K, which is the "way" of the sort and always a power of 2. The second parameter is the array of integers to be sorted, whose length is also a power of 2. The function is to return an array containing the sorted elements. So far, I know how to implement a regular merge sort. How would I modify this code so that it implements a K-way merge sort? (Note:

Find all the possible N-length anagrams - fast alternatives

♀尐吖头ヾ 提交于 2021-01-28 07:21:18
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

Compute entropy of different file extensions to find randomness of data?

北城余情 提交于 2021-01-28 07:12:41
问题 I have different file types which includes JPEG and jpg ,#mp3 ,#GIF,#MP4,#FLV, M4V ,exe, zip etc. Take data in blocks , something like 4k block size, find randomness Generate randomness score between 0 and 1 Try to put classes according to the randomness score. How can we find entropy of different types of files as mentioned above and can we scale each of the file score between 0 and 1. 回答1: +1 very interesting question here few untested ideas just from top of my head right now: how about

How to convert a PNG image to grayscale and without losing the transparency C#

╄→гoц情女王★ 提交于 2021-01-28 07:12:28
问题 I want to convert a transparent png image to greyscale without losing its transparency. The problem is the algorithm that I am using in is converting the transparent part into Black, which some picture with black character wouldn't be shown. To give you an idea. Heres the original picture : Look what happens when I pass it through the algorithm. The algorithm : public static void ToWhiteBlack(Bitmap original) { try { for (var i = 0; i < original.Width; i++) { for (var j = 0; j < original

Most efficient way to merge lists where the final values are the max from each list maintaining position

岁酱吖の 提交于 2021-01-28 07:09:58
问题 What's the most efficient way to merge these lists into one single list where the final values are the max from each list maintaining position? Right now I'm doing a brute force iteration over all of the lists and setting the max value in the final list. It works but it's not very efficient since my data sets are massive. Any ideas on how to make this more efficient? graph1 = [[0, 0, 0], [1, 0, 1], [2, 0, 0]] graph2 = [[5, 0, 0], [1, 0, 1], [2, 0, 0]] graph3 = [[2, 1, 0], [0, 0, 1], [0, 0, 0]

Minimizing the difference between sums

爷,独闯天下 提交于 2021-01-28 06:11:02
问题 Lets say you have a list of 8 prices - for example: 1$ 2$ 3$ 4$ 5$ 6$ 7$ 8$ You want to divide the prices into 2 groups of 4 prices each. Let the total price of a group be the sum of the individual prices of the group. How do you divide the group such that the difference between the two total prices is as small as possible? The obvious solution is to try all the pairs and see which is the lowest, but is there a more efficient solution that could work if this were extended to more than 8