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}";
}
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 3 years ago.
Improve this question
Here is sample input and output.
I have strings like as following.
I want to increment string last digit by 1
AVAP001P001 output AVAP001P002
CD009 output CD010
Here's a quick solution that you can work with. You might want to make it more robust, but I went ahead and added applicable comments to describe what is being done.
static void Main(string[] args)
{
var s = "CBC004DS009";
// get the very last index of the character that is not a number
var lastNonNumeric = s.LastOrDefault(c => !char.IsDigit(c));
if (lastNonNumeric != '\x0000')
{
var numericStart = s.LastIndexOf(lastNonNumeric);
// grab the number chunk from the string based on the last character found
var numericValueString = s.Substring(numericStart + 1, s.Length - numericStart - 1);
// convert that number so we can increment accordingly
if (int.TryParse(numericValueString, out var newValue))
{
newValue += 1;
// create the new string without the number chunk at the end
var newString = s.Substring(0, s.Length - numericValueString.Length);
// append the newly increment number to the end of the string, and pad
// accordingly based on the original number scheme
newString += newValue.ToString().PadLeft(numericValueString.Length, '0');
Console.WriteLine(newString);
}
}
}
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 4 years ago.
Improve this question
If I have two arrays like so:
var lanes = {1,2,3};
var racers = {1,2,3,4,5};
How can I randomize and matrix these with unique values in each direction, like so?
2 3 5
3 1 2
5 4 1
4 2 3
1 5 4
Rows do not repeat any value, and columns also do not repeat a value, with respect to themselves. Given n lanes and n racers.
I'm working on a lane rotation for a race with this criteria:
1-6 possible lanes
n possible racers (10 to 20 or more)
Each racer races exactly one time on each lane
Each row must contain unique values (can't have the same racer on 2 lanes at a time)
Each column must be unique (can't allow a racer to utilize a lane more than once)
Random order
I'm using C#, and thus will likely use Linq. Regardless... I'm not sure what the logic on this should be. So far, I come up with collisions with the approaches I've tried.
This should do what you want, creating a 2-dim array with your random lineup for as many lanes and racers as you like, making sure no racer starts in the same lane more than once. The problem is as it's randomly assigning a lineup it may paint itself into a corner (i.e. there is no more unique value for the position given what has already been assigned in the current row and lane). In this case this solution, just tries again. I know this is not the most beautiful solution, but the collisions aren't too bad for 6 lanes and 20 players:
public Int32[,] GetLineup(Int32 lanes, Int32 racers)
{
var rows = new Int32[racers, lanes];
for (var lane = 0; lane < lanes; lane++)
{
for (var racer = 0; racer < racers; racer++)
{
var taken = Enumerable.Range(0, racer).Select(e => rows[e, lane]).ToList();
taken.AddRange(Enumerable.Range(0, lane).Select(e => rows[racer, e]));
var possible = Enumerable.Range(1, racers);
var remaining = possible.Except(taken).OrderBy(e => Guid.NewGuid());
if (!remaining.Any())
{
// Failed to get a solution, try again
return GetLineup(lanes, racers);
}
else
{
rows[racer, lane] = remaining.First();
}
}
}
return rows;
}
This generates something like this:
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;
}
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 6 years ago.
Improve this question
I'm trying to create a grade calculator but I'm completely unsure how to compile the code to do so.
So far I've got the ability to split a user's response, but now I need to know how to take those splits and use them as separate values in order to create an average. I'm completely clueless how to achieve this and I've been searching for 2 days now on the internet with no luck.
Console.WriteLine ("User response seperated by commas goes here.");
string response = Console.ReadLine ();
Char delimiter = ',';
string[] splitResponses = response.Split (delimiter);
I need to know how to take those splits and use them as separate
values in order to create an average.
Not sure what you mean by take those splits and use them as separate
values, result is an array you could elements using index like splitResponseses[0]
To calculate the average you need to convert them to ints (or respective types), and calculate average.
string[] splitResponses = response.Split (delimiter); // Split string
int sum=0;
foreach(string s in splitResponses)
{
var valueInt = int.Parse(s); // convert string to an int.
sum+= valueInt;
}
double average = (double)sum/splitResponses.Length;
Another simple solution using Linq extensions.
int[] splitResponses = response.Split (delimiter) // Split string
.Select(int.Parse) // Convert To int value;
.ToArray();
Now you could calculate average using
splitResponses.Average(); // Average
splitResponses.Sum(); // Sum
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.