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 2 years ago.
Improve this question
I have challenge to solve Sales by Match problem using c# without use List collection ?
*Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
For example, there are N =7 socks with colors ar=[1,2,1,2,1,3,2] There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2 .*
you can try this code
static int sockMerchant(int n, int[] ar) {
int matchingPairsCount= 0 ;
int[] arr =new int[n];
var ix=0;
for (int i = 0 ; i< ar.Length ; i++)
{
int countPairs = 0 ;
for(int j = 0 ;j< ar.Length ;j++)
{
if(ar[i] == ar[j] && i<j&& (!arr.Contains(i)|| i==0))
{
arr[ix]=j;
ix++;
matchingPairs = matchingPairs + 1 ;
break;
}
}
}
return matchingPairsCount;
}
You can use it using the following code
var ar = new int[] { 1,2,1,2,1,3,2 };
ar.GroupBy( g => g ).Sum( g => Math.Floor( g.Count() / 2.0 ) );
It groups all the items in the array first, and then sums up all pairs. You can try it here
Related
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}";
}
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a List<List<int>>.
All the list will be of equal size. I need to find the max value in every index of the list.
ie if I have 100 list each of size 3. I need to find the max value at index 0,1 and 2 across the 100 items.
ie
Max value out of list1[0], list2[0], list3[0],......list100[0]
Max value out of list1[1], list2[1], list3[1],......list100[1]
Max value out of list1[2], list2[2], list3[2],......list100[2]
Need a function which accepts the List<List<int>> and returns an list with max value of each index. Like below
public List<int> FindMaxValueByIndex(List<List<int>> items)
{
}
Performance is a key factor that needs to be considered.
Ignoring data validation & guaranteeing the inner lists will always have the same number of elements:
public List<int> FindMaxValueByIndex(List<List<int>> items)
{
var maxListSize = items[0].Count;
var maxValues = new List<int>(items.Count);
for (var index = 0; index < maxListSize; index++)
maxValues.Add(items.Select(x => x[index]).Max());
return maxValues;
}
Input:
var data = new List<List<int>>
{
new List<int> { 5, 10, 300, 1 },
new List<int> { 3, 24, 2, 56 },
};
Output:
{5, 24, 300, 56}
I would recommend such approach using LINQ methods :)
private static int[] FindMaxByIndex(List<List<int>> lists)
{
var listLength = lists
.Select(x => x.Count)
.Distinct()
// If all lists are of equal length
// this should contain single element.
// Otherwise this would throw exception -
// this is validation.
.Single();
var maxesByIndex = new int[listLength];
for (int i = 0; i < listLength; i++)
{
maxesByIndex[i] = lists
.Select(x => x[i])
.Max();
}
return maxesByIndex;
}
I understand that the question states that the lists will always be the same size, however, since a generic list of generic lists is used and a generic list of generic lists does not enforce that requirement, this code takes the safe approach of ensuring that out of bounds indices are not accessed.
public List<int> FindMaxValueByIndex(List<List<int>> items)
{
List<int> ret = new List<int>();
var MaxListSize = items.Select(l => l.Count).Max();
for(int i = 0; i < MaxListSize; i ++)
{
ret.Add(items.Where(l => l.Count > i).Select(l => l[i]).Max());
}
return ret;
}
Fiddle
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 3 years ago.
Improve this question
I have this array and I want that every time the user inputs a number, the program immediately makes the sort. How can I do that? For example, if I input 6 and then 3, immediately takes the 3 to the first position. And then if I put 2, immediately take it to the first position and sort the others (2,3,6). Then if I put 1, takes it to the first position, sorting the others(1,2,3,6) and so on
int[] a = new int[5] ;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
As #juharr suggest you can use System.Collections.Generic.SortedSet:
static void Main(string[] args)
{
var list = new SortedSet<int>();
int count = 5;
for ( int i = 0; i < count; i++ )
list.Add(Convert.ToInt32(Console.ReadLine()));
foreach (int item in list)
Console.WriteLine(item);
Console.ReadKey();
}
add the numbers to a list and call list.sort()
List<int> list = new List<int> { 6, 3 };
list.Sort();
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
I have this code :
for(int i=0;i<???;i++)
{
...
}
tb.SetWidths(new int[] { 50, 50, 50,.... });
The problem is:
The amout of array elemens must be equal to "i"
I also want to set the value to all these elements to 50
How can i do that?
Tb is an object from itextsharp, i'm using it to draw tables on pdf files
I guess something like that would work for you? (https://stackoverflow.com/a/34379619/986160)
if count is random you can do:
Random rand = new Random();
int count = rand.Next(1, 101); // creates a number between 1 and 100
( 50 is the fixed value for all 'count' elements)
int[] array = Enumerable
.Repeat(50, count)
.ToArray();
then you can do:
tb.SetWidths(array);
It seems like you've put little to no effort into this exercise.
By simply Googling "c# define array of size" I found the following code:
int i = random number;
int[] myArray = new int [i];
Next, in order to populate the array with a certain integer, you simply loop through it:
for(int x = 0; x < myArray.Length; x++){
myArray[x] = 50;
}
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 8 years ago.
Improve this question
What is wrong here? How to filter out negative elements correctly?
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, -3, 4, 5, -1, 4, -2 };
double sumOfElem = array.Sum(element => element < 0);
Console.WriteLine(sumOfElem);
}
}
I think you need this:
double sumOfElem = array.Sum(element => (element < 0 ? 0 : element));
This way you are using an overload of Sum that utilises a transform function (so called selector), that is applied to each element of the array.
The above filters out negative elements. If you want to filter out positive ones, then simply inverse the comparison operator:
double sumOfElem = array.Sum(element => (element > 0 ? 0 : element));
You could try this one:
int sumOfElem = array.Where(element => element < 0)
.Sum();
You can't filter your values inside the Sum. You should first filter the values and then sum them.
Update
Saying that You can't filter your values inside the Sum, I mean you can't pass a predicate in the Sum method.