Iterating through dictionary and doing some stuff - c#

is it possible to iterat through dictionary like this?
I want to count all dictionary items (sum every Value),
Next, for each Key I want to take their Value
Then, I want to divide every EACH Key Value with sum
And lasty I want to multiply every output from 3
I made it so it works with 1 item, but not sure how to make it so it works with every item in dictionary.
Here is sample code for 1 item:
var dic = new Dictionary<string, int>();
//this is sum
double y = 0;
foreach (var item in dic)
{
y += item.Value;
}
//this is selected item
double x = 0;
foreach (var item in dic)
{
if (item.Key == "Smith")
{
x = item.Value;
}
}
double z = 0;
z = x / y;
Console.WriteLine("Smith/DicSum: " + z);
Now I would like to multiply the Z's (each Z's for each key in dictionary).
I was thiking about making one big loop for this like:
for (int i=0; i<y; i++) where y is the sum for all items in dictionary and multiply z's on the end of the loop
but I still don't know how to grab all seperate values and divide them while not saying the specific key for each of them.
#edit
Thanks for answears but check my edit. I have a string list, let's say
"Smith is very cool member of Smith Company"
And my program is counting the number of Smith's, so it will show x as two. Then I want to divide snumber of smiths (two) by number of all words, so it's 2/8 = 0,25. Then I want to do this with every word and multiply it so it will be 2/8*1/8*...*1/8 in this example. SO I want to multiply by a preious number from loop (from dictionary), and not by a fixed amount, that's what making this problem.

var words = "Smith is very cool member of Smith Company"
.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var dic = new Dictionary<string, double>();
foreach (string word in words)
{
if (dic.ContainsKey(word))
dic[word]++;
else
dic[word] = 1;
}
var sum = dic.Sum(x => x.Value);
var result = dic.Values.Aggregate(1.0, (current, item) => current * (item / sum));

Related

C# looping through a list to find character counts

I'm trying to loop through a string to find the character, ASCII value, and the number of times the character occurs. So far, I have found each unique character and ASCII value using foreach statements, and finding if the value was already in the list, then don't add it, otherwise add it. However I'm struggling with the count portion. I was thinking the logic would be "if I am already in the list, don't count me again, however, increment my frequency"
I've tried a few different things, such as trying to find the index of the character it found and adding to that specific index, but i'm lost.
string String = "hello my name is lauren";
char[] String1 = String.ToCharArray();
// int [] frequency = new int[String1.Length]; //array of frequency counter
int length = 0;
List<char> letters = new List<char>();
List<int> ascii = new List<int>();
List<int> frequency = new List<int>();
foreach (int ASCII in String1)
{
bool exists = ascii.Contains(ASCII);
if (exists)
{
//add to frequency at same index
//ascii.Insert(1, ascii);
//get { ASCII[index]; }
}
else
{
ascii.Add(ASCII);
//add to frequency at new index
}
}
foreach (char letter in String1)
{
bool exists = letters.Contains(letter);
if (exists)
{
//add to frequency at same index
}
else
{
letters.Add(letter);
//add to frequency at new index
}
}
length = letters.Count;
for (int j = 0; j<length; ++j)
{
Console.WriteLine($"{letters[j].ToString(),3} {"(" + ascii[j] + ")"}\t");
}
Console.ReadLine();
}
}
}
I'm not sure if I understand your question but that what you are looking for may be Dictionary<T,T> instead of List<T>. Here are examples of solutions to problems i think you trying to solve.
Counting frequency of characters appearance
Dictionary<int, int> frequency = new Dictionary<int, int>();
foreach (int j in String)
{
if (frequency.ContainsKey(j))
{
frequency[j] += 1;
}
else
{
frequency.Add(j, 1);
}
}
Method to link characters to their ASCII
Dictionary<char, int> ASCIIofCharacters = new Dictionary<char, int>();
foreach (char i in String)
{
if (ASCIIofCharacters.ContainsKey(i))
{
}
else
{
ASCIIofCharacters.Add(i, (int)i);
}
}
A simple LINQ approach is to do this:
string String = "hello my name is lauren";
var results =
String
.GroupBy(x => x)
.Select(x => new { character = x.Key, ascii = (int)x.Key, frequency = x.Count() })
.ToArray();
That gives me:
If I understood your question, you want to map each char in the provided string to the count of times it appears in the string, right?
If that is the case, there are tons of ways to do that, and you also need to choose in which data structure you want to store the result.
Assuming you want to use linq and store the result in a Dictionary<char, int>, you could do something like this:
static IDictionary<char, int> getAsciiAndFrequencies(string str) {
return (
from c in str
group c by Convert.ToChar(c)
).ToDictionary(c => c.Key, c => c.Count());
}
And use if like this:
var f = getAsciiAndFrequencies("hello my name is lauren");
// result: { h: 1, e: 3, l: 3, o: 1, ... }
You are creating a histogram. But you should not use List.Contains as it gets ineffective as the list grows. You have to go through the list one item after another. Better use Dictionary which is based on hashing and you go directly to the item. The code may look like this
string str = "hello my name is lauren";
var dict = new Dictionary<char, int>();
foreach (char c in str)
{
dict.TryGetValue(c, out int count);
dict[c] = ++count;
}
foreach (var pair in dict.OrderBy(r => r.Key))
{
Console.WriteLine(pair.Value + "x " + pair.Key + " (" + (int)pair.Key + ")");
}
which gives
4x (32)
2x a (97)
3x e (101)
1x h (104)
1x i (105)
3x l (108)
2x m (109)
2x n (110)
1x o (111)
1x r (114)
1x s (115)
1x u (117)
1x y (121)

Need to find recurring values in array between two bounds

I have this code to find recurring values in my array. I have 81 textboxs forming a grid, and they are in 9 rows of 9 boxes. I earlier on in my code saved them all to a one dimensional array with 81 elements. I found some of this code on another question: Finding duplicate integers in an array and display how many times they occurred, and it worked for me, but I cannot find which array element it actually was that recurred.
int[] OrigValues = new int[];//Already defined earlier, and assigned.
for (int c = 1; c <= 9; c++) //in this case, I called my int c instead of the usual i
{
Console.WriteLine("Row {0}:", c);
var dict = new Dictionary<int, int>();
foreach (var value in OrigValues.SubArray(c * 9 -9, 9))
{
if (dict.ContainsKey(value))
dict[value]++;
else
dict[value] = 1;
}
foreach (var pair in dict)
{
Console.WriteLine("Value {0} occurred {1} times.", pair.Key, pair.Value);
if (pair.Value >= 2 && pair.Key != 0)
{
//I have no way of finding which 2 array slots were the ones that had the same value in each of these rows.
}
}
}
OrigValues.SubArray is an extension method, that works like substring, except it is for arrays, taking array element starting at an index, and going for a length(there, c* 9 - 9 is my index, and 9 is my length)
You can turn this entire process into a LINQ query:
var duplicates =
OrigValues
.Select((value, index) => new
{
Coordinate = index,
Value = value
})
.GroupBy(tuple => tuple.Value)
.Where(group => group.Count() > 1)
.ToList();
foreach (var group in duplicates)
{
Console.Write($"{group.Key} appears in");
foreach (var tuple in group)
Console.Write($" {tuple.Coordinate}");
Console.WriteLine();
}

How to remove strings in list from another list?

I have 2 list which names are listA and listB.
I want to remove strings in listB which are in listA, but I want to do this in this way:
if listA contains: "bar", "bar", "bar", "foo"
and listB contains : "bar"
it removes only 1 bar and the result will be:
"bar", "bar", "foo"
the code I wrote removes all "bar":
List<string> result = listA.Except(listB).ToList();
You can try to remove it one by one:
foreach (var word in listB)
listA.Remove(word);
The Remove method will only remove one element at a time and is not throwing exception (but returning false) when the item is not found: https://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx
var listA = new List<string>() { "bar", "bar", "bar", "foo" };
var listB = new List<string>() { "bar" };
foreach (var word in listB){
listA.Remove(word);
}
Here is a more efficient way to do that:
var countB = new Dictionary<string, int>(listB.Count);
foreach (var x in listB)
{
int count;
countB.TryGetValue(x, out count);
countB[x] = count + 1;
}
listA.RemoveAll(x =>
{
int count;
if (!countB.TryGetValue(x, out count)) return false;
if (count == 1)
countB.Remove(x);
else
countB[x] = count - 1;
return true;
});
This is a faster method but it is likely to change the order of elements of first list. Steps:
Map the listA to a Dictionary<string, int> (let's call it listAMap), where key is the element of the list and value is the total number of times that value has occurred in listA;
Iterate through listB and for every element of listB, if that element is in the listAMap, reduce its count;
Get the keys of listMapA using Keys property of C# dictionaries, and iterate through all the keys. For every key which has positive value, add that key to another list a total of its count times. So if an entry is "bar" -> 2, then add "bar" twice in the new list.
Total run time of the algorithm is O(m + n), where m and n are number of elements in both the original lists. It is a better running time than other approaches mentioned here which have O(m * n) running time. Obviously this algorithm uses more space.
Supportive Code for the algorithm above:
//Step-1: Create the dictionary...
var listAMap = new Dictionary<string, int>();
foreach (var listAElement in listA)
{
listAMap.ContainsKey(listAElement) ? listAMap[listAElement]++ : listAMap.Add(listAElement, 1);
}
// Step-2: Remove the listB elements from dictionary...
foreach (var listBElement in listB)
{
if (listAMap.Contains(listBElement)) listAMap[listBElement]--;
}
//Step-3: Create the new list from pruned dictionary...
var prunedListA = new List<string>();
foreach (var key in listAMap.Keys)
{
if (listAMap[key] <= 0) continue;
for (var count = 0; count < listAMap[key]; count++)
{
prunedListA.Add(key);
}
}
//prunedListA contains the desired elements now.

Need to add each number 0-x to end of line?

I'm making an app and I'm almost done. I just need to know how I can streamread a txt list and foreach line, add numbers 0-x (x will be the number the user puts in the textbox) and add it to a list. So basically, it would be like this
You import a list with 'dog' on one line, 'cat' on another, and 'fish' on the third. You type '5' into the textbox. the app puts all this into a list:
dog1
dog2
dog3
dog4
dog5
cat1
cat2
cat3
cat4
cat5
fish1
fish2
fish3
fish4
fish5
thanks!
The code below should work for you. I assume you can acquire the count value on your own.
var animals = File.ReadAllLines("yourFile.txt"); //new[] {"dog", "cat", "fish"};
var count = 5;
var merged =
from a in animals
from n in Enumerable.Range(1, count)
select a + n;
foreach (var m in merged)
Console.WriteLine(m); //act on each however you want
You can read a text file with File.ReadAllLines. This gives you an array you can iterate over with foreach.
In this foreach loop you can perform another loop from 1 to the number the user entered. int.Parse comes in handy for converting the string the user entered into a number C# can do something with. For the actual iteration you can use a for loop.
You can then add each item to a list.
There is a good example for reading each line in a filestream here: http://msdn.microsoft.com/en-us/library/e4y2dch9.aspx
private List<string> GetThings(string fileName, int count)
{
string[] lines = File.ReadAllLines(fileName);
List<string> result = new List<string>();
foreach (string item in lines)
{
for (int i = 1; i <= count; i++)
result.Add(item + i.ToString());
}
return result;
}
string[] inputList = File.ReadAllLines("yourFile.txt");
List<String> listOfThings = new List<String>();
foreach (string i in inputList)
{
for (int k = 0; k < 5; k++)
{
listOfThings.Add(i + " " + k.ToString());
}
}
then after that, you can print out the list like this:
foreach (string outp in listOfThings)
{
Console.WriteLine(outp);
}
output:
some value 0
some value 1
some value 2
some value 3
some value 4
some other value 0
some other value 1
some other value 2
some other value 3
some other value 4

Histrogram with Linq

Is there a way to do a segmented histrogram with linq? I've seen several examples where you can count the number of occurances of a particular object. Is it possible to create a linq based historgram that counts the number of occurances of a series of objects between two values?
I don't know how you would group by a range of items to create the buckets neccesary for a histogram? Suppose a start bound and a width are used to create the range.
You would need to iterate through the number array and group each number by whether it was <= Upper Bound and > Lower Bound. Then you would just sum each group. I have no idea how to accomplish the group by part
Something like this?
Random randF = new Random();
List<double> nums = new List<double>();
for (int i = 0; i < 100000; i++)
{
nums.Add(randF.NextDouble()*100);
}
double fromXF = 30;
double toXF = 80;
int groupCount = 10; // number of groups between values
var histF = from i in nums
let groupKeyF = ((i-fromXF)/(toXF-fromXF)*groupCount) // even distribution of "groupCount" groups between fromXF and toXF, simple math, really
where groupKeyF >= 0 && groupKeyF < groupCount // only groups we want
let groupKey = (int)groupKeyF // clamp it to only group number
group i by groupKey into gr // group it according to group number
orderby gr.Key
select new { Value = gr.Key, Count = gr.Count() };
foreach (var item in histF)
{
Console.WriteLine("Group number: " + item.Value + ", Count: " + item.Count);
}
You could do something like:
var groups = input.GroupBy(x => (int)((x.value - start)/width));
which creates an integer value for each bar and groups by that.

Categories