Percent similarity of two ranges of numbers [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get the percent similarity or overlap of two ranges of numbers.
For example, consider the ranges [10,1] and [5,1]. the range [5,1] covers 50% of the range [10,1], and a range [100,50] covers 0% of [10,1].
Edit: forgot to state the question. How do I do this?

I would just get the Count of the Intersection and compare it to the Count of the range we're interested in, for example:
var firstRange = Enumerable.Range(1, 10);
var secondRange = Enumerable.Range(1, 5);
var secondPercentCoverageOfFirst =
(double)firstRange.Intersect(secondRange).Count() / firstRange.Count() * 100;
Console.WriteLine($"{secondPercentCoverageOfFirst:0.00}%");
Console.Write("\nPress any key to exit...");
Console.ReadKey();
Of course this would apply to any set of comparable objects, not just ranges of consecutive numbers...
var firstSet = new List<string> { "car", "bus", "boat", "plane" };
var secondSet = new List<string> { "bicycle", "car", "boat", "motorcycle" };
var secondPercentCoverageOfFirst =
(double)firstSet.Intersect(secondSet).Count() / firstSet.Count() * 100;
Output for both cases

Related

make dictionary that will loop every 9 numbers. C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Im new to programming and sorry if I cant explain properly. I am trying to iterate through a list that has items in it in multiples of 9. So the list can have 9,18,27.. items.
I have the code working for it to read when there is 9 using this dictionary. But I would like it work for any amount in multiples of 9.
var alphabets = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"}
};
So for example if there was 18 items it would like this dictionary to have this function.
var alphabets2 = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"},
{10,"A3"},{11,"B3"},{12,"C3"},{13"D3"},
{14,"E3"},{15,"F3"}, {16,"G3"},{17,"H3"},
{18,"I3"}
};
Thank you
As #DiplomacyNotWar commented, it sounds as if you need to input int value to convert to a correlating string value which is uniformly based on multiples of 9. If this is the case, I agree with #DiplomacyNotWar that you don't need to store anything but create a function to output the needed string value based on an int value. Here is a function that will output the pattern in your examples.
// value should be 0
string ConvertIntToSpecificString(int value)
{
// this will give you an int value 0-8
var modValue = (value - 1) % 9;
// The unicode for 'A' is 65
var firstCharValue = (char)(65 + modValue);
// This will return a whole number giving the iteration count. FE: 19 / 9 = 2
// Adding 2 to fit the pattern stated in the examples.
var secondValue = ( value / 9 ) + 2 ;
return $"{firstCharValue}{secondValue}";
}

what is the most efficient data structure for matching specific numbers to ranges of percents? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In my program, I want to be able to match certain numbers to ranges of percentages, like: 0 would be the match of 0%, 1 would be the match of less then 10%, 2 would be the match of 10%-20%... and so forth. What is the most efficient data structure/method to do it ?
I would like to perform it in c#.
A Dictionary for this purpose could be a decent solution. The keys of the Dictionary would be the numbers and the values could be Tuples with the corresponding min and max percentages. If you want to learn the range for a number you could retrieve it's range in O(1).
You could define it as:
var numbersPercentageRanges = new Dictionary<int, Tuple<double, double>>
{
{ 0, Tuple.Create(0,0) },
{ 1, Tuple.Create(0.1,0.2)}
};
and you could retrieve the corresponding range as:
if(numbersPercentageRanges.TryGetValue(1, out var range))
{
var min = range.Item1;
var max = range.Item2;
}

Getting values from Dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi i have used dictionary as like below.
Dictionary<int, List<double>> conditionbase = new Dictionary<int, List<double>>();
in this dictionary i have stored maximum and minimum values based on columnindex as key.
Now i need to get particular columnindex based these max and minimum values?
First assuming that the min and/or max values are not unique to a given column index then you want to find the columns that have a particular min and max.
So, ignoring the trickly business of double equality, try something like:
var min = 1.0;
var max = 100.0
var columns = conditionbase.Where(x => x.Value.Max() == max && x.Value.Min() == min).Select(y => y.Key).ToArray();
or, if your max and min values are at indexes:
var columns = conditionbase.Where(x => x.Value[1] == max && x.Value[0] == min).Select(y => y.Key).ToArray();
Note: Equality comparison of doubles can be difficult.
If your list has only a max and a min then consider using a type like:
public class Limits
{
double Minimum {get; set;}
double Maximum {get; set; }
}
Then ... your dictionary becomes:
var conditionbase = new Dictionary<int, Limits>();
Much better. Lots of options.

Find anomaly in list of doubles [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a method that returns a list of numbers which i can not trust as sometimes i get a number that is not accurate. As an example:
var nums = new List<double> {675,596,125278,490,545,567,470};
The 125278 value clearly is an anomaly, can someone help in devising a method that will get rid of all numbers in the list that are completely out of range based on the other figures?
At the moment i am ordering the list and then getting the median however on occasion this has failed. No number should be able to exceed around 36000.
One idea would be to return the lowest value? However ideally i would like to return the last number in the list that is not an 'anomaly' e.g. from the list above 470 should be returned.
If not anomally means lower than 36000 then try this:
var notanomally = nums.Where(x=>x<36000); // lower than 36k
var lastnotanomally = notanomally.Last();
It is not very efficient method but you can try something like this.
var nums = new List<double> { 675, 596, 125278, 490, 545, 567, 470 };
var removing = new List<double>();
var temp = new List<double>();
double EPSILON = 5000;
foreach (var num in nums)
{
var average = nums.Average();
temp = nums.Where(n => n != num).ToList();
var average1 = temp.Average();
if (Math.Abs(average1 - average) > EPSILON)
{
removing.Add(num);
}
}
nums.RemoveAll(n=>removing.Contains(n));
This method temporarily removes an element and observes change in average. Significant change in average means the current value is far too large than the others.

C# Linq query calculate percentage from integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Country TotalSales
UK 2512793.05
USA 119176.75
AUST 83599.25
GERM 45613.75
IREL 43352.50
SWED 32955.75
Then I have:
var list = new List<int> { 2512793, 119176, 83599, 45613, 43352 };
How to convert to Percentage list from above list. please advise.
E.G: {45%, 20%, 15%, 12%, 8% }
Convert the list to a decimal list, and use something like the following :
var list = new List<decimal> { 2512793, 119176, 83599, 45613, 43352 };
decimal sum = list.Sum();
var perc = list.Select(x => (x / sum) * 100);
Alternatively, you can cast each value as decimal.
Or Alternatively as #James Pointed out there is no need to multiply by 100 if you need to output the value.
var perc = list.Select(x => x / sum);
Console.WriteLine(perc.FirstOrDefault().ToString("P")); // Output the first value
Well you can try
var list = new List<int> {2512793, 119176, 83599, 45613, 43352 };
var percentage1 = list.Select(ele => (ele * 100) / list.Sum());

Categories

Resources