Related
I am writing microservice in Dart.
Service run workers by passing command args to workers like: "app.exe -a=1,2,3,4".
So if currentlyActiveWorkers is 4. Then jobs can be splited to sevaral workers like:
first: "app.exe -a=1,2,3,4"
second: "app.exe -a=5,6,7,8"
third: "app.exe -a=9,10,11,12"
fourth: "app.exe -a=13,14,15,16".
I wrote next prototype:
void main() {
int maxWorkers = 16;
int currentlyActiveWorkers = 2;
genJobs() {
int step = 1;
int sliceSize = (maxWorkers/currentlyActiveWorkers).round();
var list = [for(var i=step; i<=maxWorkers; i+=1) i];
for(int i in Iterable<int>.generate(currentlyActiveWorkers))
{
print(list.sublist(i * sliceSize, sliceSize * step));
step++;
}
}
genJobs();
}
It work fine if currentlyActiveWorkers is multiple of 2. It's generate suitable jobs numbers:
[1, 2, 3, 4, 5, 6, 7, 8]
[9, 10, 11, 12, 13, 14, 15, 16]
But there is bug if user specify for example 3. Last number 16 is loosing.
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
It does not matter for me the number element in every group +- 1 is ok for me.
Your rounding logic is ambiguous. Besides, you should handle the last chunk of data in different way:
void main() {
print(genJobs());
}
Map<int, List<int>> genJobs() {
final activeWorkers = 3;
final maxJobs = 16;
final jobs = List<int>.generate(maxJobs, (i) => i + 1);
final workerCapacity = (jobs.length / activeWorkers).floor();
var chunks = <int, List<int>>{};
for (var workerNumber = 0; workerNumber < activeWorkers; workerNumber++) {
final startIndex = workerNumber * workerCapacity;
final endIndex = startIndex + workerCapacity;
final chunk = jobs.sublist(
startIndex,
endIndex > jobs.length || workerNumber == activeWorkers - 1
? jobs.length
: endIndex,
);
chunks.addAll({workerNumber: chunk});
}
return chunks;
}
Your problem is that you are picking a fixed size for the slices first, even when the number of elements isn't a multiple of the slice count. You're lucky that it rounded down instead of up, otherwise you'd have gotten an index-out-of-range error (try your code with 17 elements and three groups).
First you should figure our what result you want. Then you can try coding that.
For something like 22 elements and four groups, you probably want two groups of 6 elements and two groups of 5 elements, not three groups of 6 and one of 4 (since you say +/-1 is OK, not +/- 2).
I would do something like:
/// Emits the integers from 0 to [elementCount] - 1 in [groupCount] grups.
///
/// The [elementCount] must be greater than zero.
/// The [groupCount] must be in the range 1..[elementCount],
/// meaning that each group will have at least one element, and
/// each element is in at least one group.
Iterable<List<int>> evenlySpreadGroups(int elementCount, int groupCount) sync* {
if (elementCount < 1) {
throw RangeError.range(elementCount, 1, null, "elementCount");
}
RangeError.checkValueInInterval(groupCount, 1, elementCount, "groupCount");
var list = <int>[];
var groupIndex = 1;
for (var i = 0; i < elementCount; i++) {
while (i * groupCount >= groupIndex * elementCount) {
yield list;
list = [];
groupIndex += 1;
}
list.add(i);
}
yield list;
}
(It's written to also work if you allow more groups than elements, any groupCount >= 1, you'll just get empty lists in the output which is just rarely useful).
The more solutions the better:
import 'package:lists/lists.dart';
void main() {
final maxPartSize1 = 5;
final data1 = genData(1, 16);
print('data: $data1');
final parts1 = split(data1, maxPartSize1);
print('parts by $maxPartSize1: $parts1');
print('=====');
final maxPartSize2 = 3;
final data2 = genData(2, 8);
print('data: $data2');
final parts2 = split(data2, maxPartSize2);
print('parts by $maxPartSize2: $parts2');
}
List<int> genData(int start, int length) {
return List<int>.generate(length, (i) => i + start);
}
Iterable<List<int>> split(List<int> data, int step) sync* {
final length = data.length;
for (final i in StepList(0, length - 1, step)) {
var i2 = i + step;
if (i2 > length) {
i2 = length;
}
yield data.sublist(i, i2);
}
}
Output:
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
parts by 5: ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16])
=====
data: [2, 3, 4, 5, 6, 7, 8, 9]
parts by 3: ([2, 3, 4], [5, 6, 7], [8, 9])
Another way:
void main() {
final maxWorkers1 = 8;
final datLen1 = 16;
final data1 = [1, 2, 5, 6, 9, 10, 11, 12, 13, 14, 15, 16];
print('data: $data1');
final parts1 = split(data1, maxWorkers1);
print('$maxWorkers1 workers: $parts1');
print('=====');
final maxWorkers2 = 4;
final datLen2 = 11;
final data2 = genData(3, datLen2);
print('data: $data2');
final parts2 = split(data2, maxWorkers2);
print('$maxWorkers2 workers: $parts2');
print('=====');
final maxWorkers3 = 8;
final data3 = [7, 8, 3, 4];
print('data: $data3');
final parts3 = split(data3, maxWorkers3);
print('$maxWorkers3 workers: $parts3');
}
List<int> genData(int start, int length) {
return List<int>.generate(length, (i) => i + start);
}
Iterable<List<int>> split(List<int> data, int divider) sync* {
if (divider <= 0) {
throw RangeError.value(divider, 'divider', 'Must be greater than 0');
}
if (data.isEmpty) {
// Nothing to do
return;
}
final length = data.length;
divider = divider > length ? length : divider;
var partSize = length ~/ divider;
if (length != partSize * divider) {
partSize++;
}
for (var i = 0; i < divider; i++) {
final start = i * partSize;
if (start >= length) {
break;
}
var end = start + partSize;
if (end > length) {
end = length;
}
yield data.sublist(start, end);
}
}
Output:
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
3 workers: ([1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16])
=====
data: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
4 workers: ([3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13])
=====
data: [7, 8, 3, 4]
8 workers: ([7], [8], [3], [4])
Develop a method change(amount) that for any integer amount in the range from 24 to 1000 returns a list consisting of numbers 5 and 7 only, such that their sum is equal to amount. For example, change(28) may return [7, 7, 7, 7], while change(49) may return [7, 7, 7, 7, 7, 7, 7] or [5, 5, 5, 5, 5, 5, 5, 7, 7] or [7, 5, 5, 5, 5, 5, 5, 5, 7].
I have written something like this. But it isn't working right.
enter code here
def change (amount):
assert (amount >=24)
if amount == 24:
return [5, 5, 7, 7]
if amount == 29:
return [5, 5, 5, 7, 7]
if amount == 40:
return [5, 5, 5, 5, 5, 5, 5, 5]
coins = change(amount - 5)
coins.append(5)
return coins
for coins in range(24, 1000):
print(coins)
Actually, you should find two numbers:
the number of coins with value 7 (let it be countA) and
the number of coins with value 5 (let it be countB)
As you can see 0 <= countA <= sum/7. Then sum - countA * 7 should be divided by 5 without remainder.
So, the simple implementation in java can be like this:
import java.util.Collections;
// ...
public List<Integer> getExchange(int sum)
{
int coinA = 7;
int coinB = 5;
int maxCountOfCoinA = sum / coinA;
int countA = 0;
int countB = 0;
for (int count = 0; count <= maxCountOfCoinA; count++)
{
int remains = sum - count * coinA;
if (remains % coinB == 0)
{
countA = count;
countB = remains / coinB;
break;
}
}
List<Integer> result = new ArrayList<>();
result.addAll(Collections.nCopies(countA, coinA));
result.addAll(Collections.nCopies(countB, coinB));
return result;
}
I have an array of arrays and a matching array. Each array has unique id values.
MatchingArray = [1,2,3,4,5,6]
A1 = [1, 4, 6]
A2 = [2,3,5]
A3 = [1,5]
A4 = [4]
A5 = [1, 6]
Need to find "optimal matchings". An optimal matching is an array of subsets from A1-A5 with minimal length, which should have a maximum possible intersection with MatchingArray.
For this example there are 2 possible matchings with a maximum intersection: M1 = [[2,3,5], [1, 4, 6]] and M2 = [[1,5], [4], [1, 6]]. But M1.length < M2.length, so the algorithm should output M1.
You could use sets (or hashes, whatever the language calls them) to optimise the time efficiency.
Convert the target array to a set, and then subtract the selected source from it (i.e. removing common values). Keep doing this recursively until the target set is empty. Keep track of the best result (using the fewest source arrays as possible). Backtrack if the number of source arrays being used gets past the length of the best solution already found at that moment.
Here is the code in Python:
def find_optimal_coverage(target, sources):
max_size = len(target)
best = None
def recurse(target, sources, selected):
nonlocal max_size, best
if len(target) == 0:
best = selected
max_size = len(best) - 1
return True
if len(selected) == max_size:
return None
for i, source in enumerate(sources):
result = recurse(target - set(source), sources[i+1:],
selected + [list(source)])
if result:
return True
target = set(target) # convert to set for faster lookup
# limit the source lists to elements that occur in the target
sources = list(map(target.intersection, sources))
# limit target to elements that occur in at least one source
target = set.union(*sources)
# sort sources by decreasing length to maximise probability of
# finding optimal solution sooner
sources.sort(key = len, reverse = True)
if recurse(target, sources, []):
return best
result = find_optimal_coverage(
[1, 2, 3, 4, 5, 6, 8],
[
[1, 4, 6, 7],
[2, 3, 5],
[1, 5],
[4],
[1, 6]
]
)
print(result)
See it run on repl.it
In JavaScript:
function subtractArray(s, arr) {
return arr.reduce( (s, v) => (s.delete(v), s), new Set(s) );
}
function findOptimalCoverage(target, sources) {
var maxSize = target.size;
var best = null;
function recurse(target, sources, selected) {
if (target.size == 0) {
best = selected;
maxSize = best.length - 1;
return true;
}
if (selected.length == maxSize) return;
return sources.some( (source, i) =>
recurse(subtractArray(target, source), sources.slice(i+1),
selected.concat([source]))
);
}
target = new Set(target) // convert to set for faster lookup
// limit the source arrays to elements that occur in the target
sources = sources.map( source => source.filter(target.has.bind(target)));
// limit target to elements that occur in at least one source
target = new Set([].concat(...sources));
// sort sources by decreasing length to maximise probability of
// finding optimal solution sooner
sources.sort( (a,b) => b.length - a.length );
if (recurse(target, sources, [])) return best;
}
var result = findOptimalCoverage(
[1, 2, 3, 4, 5, 6, 8],
[
[1, 4, 6, 7],
[2, 3, 5],
[1, 5],
[4],
[1, 6]
]
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Implemented algorithm in javascript:
var matchingArray = [1, 2, 3, 4, 5, 6];
var A1 = [1, 4, 6],
A2 = [2, 3, 5],
A3 = [1, 5],
A4 = [4],
A5 = [1, 6];
var M = [A1, A2, A3, A4, A5];
function compareArrays(M, machingArray) {
var intersections = []
M.forEach(function(A) {
var partOfItersections;
if (A.length > 0) {
var intersectionsCount = getIntersectionCount(A, machingArray);
partOfItersections = intersectionsCount / A.length;
} else {
partOfItersections = 0
}
intersections.push({
length: A.length,
partOfItersections: partOfItersections
});
});
//alert(JSON.stringify(intersections));
var maxLength = 0,
maxPartOfItersections = 0,
optimalArrays = [];
intersections.forEach(function(arrayData, index) {
var currentArr = M[index];
var currentArrLength = currentArr.length;
if (maxPartOfItersections < arrayData.partOfItersections) {
setCurrentOptimalArr(arrayData.partOfItersections, currentArr);
} else if (maxPartOfItersections === arrayData.partOfItersections) {
if (maxLength < currentArrLength) {
setCurrentOptimalArr(arrayData.partOfItersections, currentArr);
} else if (maxLength === currentArrLength) {
optimalArrays.push(currentArr);
}
}
});
//alert(JSON.stringify(optimalArrays));
return optimalArrays;
function setCurrentOptimalArr(intersectionsCount, currentArr) {
optimalArrays = [currentArr];
maxLength = currentArr.length;
maxPartOfItersections = intersectionsCount;
}
function getIntersectionCount(A, machingArray) {
var intersectionCount = 0;
A.forEach(function(elem) {
if (machingArray.indexOf(elem) != -1) {
intersectionCount++;
}
});
return intersectionCount;
}
}
alert(JSON.stringify(compareArrays(M, matchingArray)));
Count intersection of arrays separately.
Return arrays which contain more part of intersections.
Code updated
If I have an array of golf results:
-3, +5, -3, 0, +1, +8, 0, +6, +2, -8, +5
I need to find a sequence of three adjacent numbers which have the minimum sum. For this example, the sub-sequences would be:
[-3, +5, -3]
[+5, -3, 0]
[-3, 0, +1]
... etc ...
[+2, -8, +5]
And the minimum sequence would be [-3, 0, +1] having a sum of -2.
You could use this LINQ query:
int[] golfResult = { -3, +5, -3, 0, +1, +8, 0, +6, +2, -8, +5 };
var combinations = from i in Enumerable.Range(0, golfResult.Length - 2)
select new {
i1 = golfResult[i],
i2 = golfResult[i + 1],
i3 = golfResult[i + 2],
};
var min = combinations.OrderBy(x => x.i1 + x.i2 + x.i3).First();
int[] minGolfResult = { min.i1, min.i2, min.i3 }; // -3, 0, +1
Of course you need to check if there are at least three results in the array.
I'm not sure why you would do this with LINQ. I think a straight up iterative solution is easier to understand:
int[] scores = new[] { -3, 5, -3, 0, 1, 8, 0, 6, 2, -8, 5 };
int minimumSubsequence = int.MaxValue;
int minimumSubsequenceIndex = -1;
for (int i = 0; i < scores.Length - 2; i++)
{
int sum = scores[i] + scores[i + 1] + scores[i + 2];
if (sum < minimumSubsequence)
{
minimumSubsequence = sum;
minimumSubsequenceIndex = i;
}
}
// minimumSubsequenceIndex is index of the first item in the minimum subsequence
// minimumSubsequence is the minimum subsequence's sum.
If you really want to do it in LINQ, you can go this way:
int length = 3;
var scores = new List<int>() { -3, +5, -3, 0, +1, +8, 0, +6, +2, -8, +5 };
var results =
scores
.Select((value, index) => new
{
Value = scores.Skip(index - length + 1).Take(length).Sum(),
Index = index - length + 1
})
.Skip(length - 1)
.OrderBy(x => x.Value)
.First()
.Index;
This creates a second list that sums all length preceeding elements and then sorts it. You have
this is an algorithmic playground for me! I've seen variations of this problem tackling maximum consecutive subsequence but this is another variation as well.
the formal def:
given A[1..n] find i and j so that abs(A[i]+A[i+1]+...+A[j]) is closest to zero among others.
I'm wondering how to get O(n log^2 n), or even O(n log n) solution.
Calculate the cumulative sum.
Sort it.
Find the sequential pair with least difference.
function leastSubsequenceSum(values) {
var n = values.length;
// Store the cumulative sum along with the index.
var sums = [];
sums[0] = { index: 0, sum: 0 };
for (var i = 1; i <= n; i++) {
sums[i] = {
index: i,
sum: sums[i-1].sum + values[i-1]
};
}
// Sort by cumulative sum
sums.sort(function (a, b) {
return a.sum == b.sum ? b.index - a.index : a.sum - b.sum;
});
// Find the sequential pair with the least difference.
var bestI = -1;
var bestDiff = null;
for (var i = 1; i <= n; i++) {
var diff = Math.abs(sums[i-1].sum - sums[i].sum);
if (bestDiff === null || diff < bestDiff) {
bestDiff = diff;
bestI = i;
}
}
// Just to make sure start < stop
var start = sums[bestI-1].index;
var stop = sums[bestI].index;
if (start > stop) {
var tmp = start;
start = stop;
stop = tmp;
}
return [start, stop-1, bestDiff];
}
Examples:
>>> leastSubsequenceSum([10, -5, 3, -4, 11, -4, 12, 20]);
[2, 3, 1]
>>> leastSubsequenceSum([5, 6, -1, -9, -2, 16, 19, 1, -4, 9]);
[0, 4, 1]
>>> leastSubsequenceSum([3, 16, 8, -10, -1, -8, -3, 10, -2, -4]);
[6, 9, 1]
In the first example, [2, 3, 1] means, sum from index 2 to 3 (inclusive), and you get an absolute sum of 1:
[10, -5, 3, -4, 11, -4, 12, 20]
^^^^^