SortedDictionary<TKey, TValue>: check if contents equal to another SortedDictionary?
By : kahlersd
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You will have to be prepared to loop through them all, but there are important shortcuts you can test for first. First shortcut, is to check for object identity. While "A is A" isn't quite as profound as Ayn Rand seems to think, it is a handy way to make equality code faster. Identity always entails equality, and it is common in real code to end up comparing something to itself (esp in collection lookups, loops and where an object is passed through several layers of code). code :
public static bool EqualSortedDict<K, V>(SortedDictionary<K, V> x, SortedDictionary<K, V> y)
{
if(ReferenceEquals(x, y))
return true;
if(ReferenceEquals(x, null)  ReferenceEquals(y, null))
return false; //both being null already hit above.
if(x.Count != y.Count)
return false;
if(!x.Comparer.Equals(y.Comparer))
return false;//check if this is what you need. Probably is but might
//not be in some cases.
foreach(KeyValuePair<K, V> kvp in x)
{
V cmpValue = default(V);
if(!y.TryGetValue(kvp.Key, out cmpValue)  !kvp.Value.Equals(cmpValue))
return false;
}
return true;
}

Count average cost and update cost of all products in column with conditions
By : Imran
Date : March 29 2020, 07:55 AM
To fix this issue please help me with following: code :
DECLARE @Average_cost
SELECT @Average_cost = AVG(UnitPrice) FROM Products
UPDATE Products
SET UnitPrice = (CASE WHEN UnitPrice > @Average_cost
THEN UnitPrice  (UnitPrice * .1)
WHEN UnitPrice < @Average_cost
THEN UnitPrice + (UnitPrice * .2)
ELSE @Average_cost
END)

minimum cost path through a cost matrix with positive and negative cost
By : Inez Jester
Date : March 29 2020, 07:55 AM
hope this fix your issue Let's say that H[i, j] is the minimum health the player needs when starting from square (i, j). We are interested in H[1, 1], which is the minimum health needed from the starting square. I assume that all values in the cost matrix M are integers. Therefore, the smallest positive health is 1. code :
H[m, n] = max(1  M[m, n], 1)
H[m, i] = max(H[m, i+1]  M[m, i], 1)
H[j, n] = max(H[j+1, n]  M[j, n], 1)
H[i, j] = min(max(H[i, j+1]  M[i, j], 1),
max(H[i+1, j]  M[i, j], 1))
int[] H = new int[m, n];
H[m, n] = max(1  M[m, n], 1);
// remember to loop backwards
for (int i = m1; i >= 1; i)
H[m, i] = max(H[m, i+1]  M[m, i], 1);
for (int j = n1; j >= 1; j)
H[j, n] = max(H[j+1, n]  M[j, n], 1);
// again, loop backwards
for (int i = m1; i >= 1; i)
for (int j = n1; j >= 1; j)
H[i, j] = min(max(H[i, j+1]  M[i, j], 1),
max(H[i+1, j]  M[i, j], 1));
return H[1, 1];

Postgres EXPLAIN ANALYZE cost estimate row count massively higher than actual row count. No Vacuuming?
By : mattlezeay
Date : March 29 2020, 07:55 AM
wish helps you I have a Postgres 9.4.18 database running on Heroku in a Django project. I had noticed that queries were becoming increasingly slow so I ran an "EXPLAIN ANALYZE" on one query and noticed that for one node the row estimate was massively higher than the actual row count: , To find out if autovacuum is enabled as it should be, run code :
SHOW autovacuum;
SELECT reloptions FROM pg_class WHERE relname = 'listings_listing';
ALTER TABLE listings_listing SET (
autovacuum_vacuum_cost_limit = 1000,
toast.autovacuum_vacuum_cost_limit = 1000
);
ALTER TABLE listings_listing SET (
autovacuum_vacuum_cost_delay = 0,
toast.autovacuum_vacuum_cost_delay = 0
);
SELECT n_live_tup, n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'listings_listing';

How to compare values from one SortedDictionary to Add them to a new SortedDictionary?
By : user2679047
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Here is an incomplete attempt using LINQ to obtain two sequences, one for enumerating forward (starting from the max item) and one for enumerating backward. After that point it is not easy to continue with LINQ, because the two sequences must be enumerated parallely in a complex way, so I went with a while loop. code :
var mainsd = new SortedDictionary<double, int>()
{
{500.10, 500}, {500.09, 1000}, {500.08, 2000}, {500.07, 3000},
{500.06, 4500}, {500.05, 5500}, {500.04, 6000}, {500.03, 7000},
{500.02, 8500}, {500.01, 9500}, {500.00, 10000}, {499.99, 9000},
{499.98, 8000}, {499.97, 7500}, {499.96, 6500}, {499.95, 5000},
{499.94, 4000}, {499.93, 3500}, {499.92, 2500}, {499.91, 1500},
{499.90, 550},
};
var maxValue = mainsd.Max(e => e.Value);
var maxItemKey = mainsd.First(e => e.Value == maxValue).Key;
var forward = mainsd.SkipWhile(e => e.Key <= maxItemKey).ToArray();
var backward = mainsd.TakeWhile(e => e.Key < maxItemKey).Reverse().ToArray();
int i1 = 0;
int i2 = 0;
while (true)
{
var sum1 = i1 < forward.Length  1 ? forward[i1].Value + forward[i1 + 1].Value : 0;
var sum2 = i2 < backward.Length  1 ? backward[i2].Value + backward[i2 + 1].Value : 0;
if (sum1 == 0 && sum2 == 0) break;
if (sum1 >= sum2)
{
Console.WriteLine($"Forward: {sum1}, Keys: {forward[i1].Key}, {forward[i1 + 1].Key}");
i1 += 2;
}
else
{
Console.WriteLine($"Backward: {sum2}, Keys: {backward[i2 + 1].Key}, {backward[i2].Key}");
i2 += 2;
}
}
Forward: 18000, Keys: 500,01, 500,02
Backward: 17000, Keys: 499,98, 499,99
Backward: 14000, Keys: 499,96, 499,97
Forward: 13000, Keys: 500,03, 500,04
Forward: 10000, Keys: 500,05, 500,06
Backward: 9000, Keys: 499,94, 499,95
Backward: 6000, Keys: 499,92, 499,93
Forward: 5000, Keys: 500,07, 500,08
Backward: 2050, Keys: 499,9, 499,91
Forward: 1500, Keys: 500,09, 500,1
var valuesd = new SortedDictionary<double, int>();
valuesd.Add(forward[i1].Key, forward[i1].Value);
valuesd.Add(forward[i1 + 1].Key, forward[i1 + 1].Value);
valuesd.Add(backward[i2].Key, backward[i2].Value);
valuesd.Add(backward[i2 + 1].Key, backward[i2 + 1].Value);
if (valuesd.Values.Sum() >= mainsd.Values.Sum() * 50 / 100) break;

