int permutations in C# [closed] - c#

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 6 years ago.
Improve this question
I want to implement a method with the following signature:
public int Max(int number){ }

The largest number that can be created by a digits of a given number is obtained by ordering the digits from largest to smallest. See the following code for a possible implementation.
public int Max(int number)
{
var numberAsCharArray = number.ToString().OrderByDescending(c => c).ToArray();
var largestNumberAsString = new string(numberAsCharArray);
return Int32.Parse(str);
}

Maybe not most efficient way is to cast to string and order desc
var result = int.Parse(String.Join("", digit.ToString().OrderByDescending(x => x)))

Related

Regular expression for value Range from 1 - 1440 (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 1 year ago.
Improve this question
What will be the regular expression for value Range from 1 - 1440? (Integer)
I think this will do it. I'm running tests. I'm acting under the assumption you will not have numbers padded with zero like "0999"
"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"
breakdown:
[1-9] obvious
[1-9[0-9] allow 10 to 19
[1-9][0-9][0-9] allow 100 to 199
1[0-3][0-9][0-9] allow 1000 to 1399
14[0-3][0-9] allow 1400 to 1439
1440 obvious
Appears to work:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
for(int i = 0;i < 10000;i++)
{
if(Regex.IsMatch(i.ToString(),"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"))
{
Console.WriteLine(i);
}
}
}
}

Transform alpha-numeric strings to int value [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 2 years ago.
Improve this question
I am looking for a way to transform strings of any size to an integer.
For example: "a" - 01; "b" - 02; "c" - 03; "ab" - 0102; "aac" - 010103.
Right now, I am replacing every single char in the string with a value from an array.
Is there a simple, more fancy way to do this?
more fancy
string source = "aac";
string result_string = string.Join("", source.Select(c => (c - 'a' + 1).ToString("00")));
string source = "aac";
int result_int = source.Select(c => (c - 'a' + 1)).Aggregate((a, i) => a * 100 + i);

How do increment an integer that is part of a string at each iteration of loop [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 2 years ago.
Improve this question
i have a string like this S02E01 and now i want to run it through a loop like 20 times but at each iteration i want to increment the number after 'E' so that we have for example S02E01,S02E02,S02E03 ..S0210,...S02E20. Please help me.
Its really simple by using some string functions: Substring, PadLeft or ToString
string mystring = "S02E00";
var template = mystring.Substring(0, mystring.Length - 2);//template = "S02E"
for(int i = 0;i <= 20; i++)
{
var result = template + i.ToString("00");
Console.WriteLine(result);
}
you could use PadLeft too => var result = template + i.ToString().PadLeft(2, '0');

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;
}

How to numerically sort an array of strings? [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 an array of strings that has a number at the beginning, followed by a name. How can I sort the array numerically; ie
40 Mike, 25 Tyson, 9 Jackson, 5 Phillip, 3 Mitchell
as opposed to
9 Jackson, 5 Phillip, 40 Mike, 3 Mitchell, 25 Tyson
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] items = {"10", "1", "30", "-5"};
Array.Sort(items, (x, y) =>
{
int xNum = int.Parse(x);
int yNum = int.Parse(y);
return Comparer<int>.Default.Compare(xNum, yNum);
});
foreach(var item in items)
Console.WriteLine(item);
}
}
var sorted = yourArray.OrderBy(x => int.Parse(x.Split()[0])).ToArray();
You can use SortedDictionary data structure to obtain an array sorted.

Categories

Resources