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 try to make the largest number, of entered number. Have some ideas, but all of them end by error.
int n = 123 // in the end shell be 321, 567 -> 765, 184 -> 841
StringBuilder str = new StringBuilder(n.ToString());
int[] aray = new int[3];
for (int i = 0;i < str.Length;i++) {
aray[i] = int.Parse(str[i].ToString());
}
aray.OrderBy(x => x);
var a = aray.Join(' '); //eror
Console.WriteLine(a);
Well, we have two cases: postive and negative numbers:
n = 43125 -> 54321
n = -43125 -> -12345
We can hold both cases with a help of Linq:
using System.Linq:
...
string result = string.Concat(n < 0
? n.ToString().OrderBy(c => c)
: n.ToString().OrderByDescending(c => c));
Here we exploit the fact that '-' < '0' and we don't have to do any special work for - in case of negative numbers. If you want to obtain int value, just add int.Parse:
int result = int.Parse(string.Concat(n < 0
? n.ToString().OrderBy(c => c)
: n.ToString().OrderByDescending(c => c)));
but be careful: large n will cause integer overflow:
n = 1234567890 -> 9876543210 > int.MaxValue
You have a lot of code to accomplish something very simple:
var ans = int.Parse(String.Concat(n.ToString().OrderByDescending(dc => dc)));
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 2 years ago.
Improve this question
this is my first post so excuse me if I am not being clear enough.
As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.
Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.
Sorry I can't provide any code because I am not sure how I should tackle this problem.
As #mjwills said, mod is what you want.
static IEnumerable<int> Digitize(int num)
{
while (num > 0)
{
int digit = num % 10;
yield return digit;
num = num / 10;
}
}
And then call it like so:
static void Main(string[] _)
{
int num = 1998;
int[] digits = Digitize(num).ToArray();
Console.WriteLine($"Original: {num}");
foreach (var digit in digits)
Console.WriteLine(digit);
}
make a string of that number and make it like this
```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
intArray[index] = int.Parse(ch.ToString());
index++;
}
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 need to get non repetitive alphanumeric character in 10 digit using LINQ. I searched google a lot. But i could not able to find it out. Please help me to get the solution. Thanks
If you don't have to use linq
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var stringChars = new char[10];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
var randomNumber = random.Next(chars.Length);
stringChars[i] = chars[randomNumber];
chars = chars.Replace(chars[randomNumber].ToString(), "");
}
var finalString = new String(stringChars);
It is a very interesting LINQ question... Probably by using Aggregate it is solvable...
Mmmh... yes... it is evil enough:
var rnd = new Random();
var chars = "ABCDEFGHIJ0123456789";
var res = Enumerable.Range(0, 10)
.Select(x => rnd.Next(0, chars.Length - x))
.Aggregate(
Tuple.Create(string.Empty, chars),
(prev, ix) => Tuple.Create(
prev.Item1 + prev.Item2[ix],
prev.Item2.Substring(0, ix) + prev.Item2.Substring(ix + 1)
)
).Item1;
In general using LINQ is wrong here, because every character depends on all the previous characters. This is complex to do in LINQ. I had to cheat heavily, using the .Aggregate() and keeping a "state" of all the unused characters (the Item2) and adding the characters for the "response" to the Item1.
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
For example:
{1, 1, 11, 1111, 1111111} This array is valid because each element use only digit 1.
And {11, 101, 1111, 11111} This array is not valid because each element use 0 and 1 digit.
You can try something like this:
var isValid = arr
.Select(a => a.ToString()) //Call .ToString() on each integer
.SelectMany(a => a) //Select each character in the string and flatten
.Distinct() //Get only the unique characters
.Count() <= 1; //If there are more than 1 unique characters, it's invalid.
Try this:
var allSame = source.SelectMany(n => n.ToString()).Distinct().Count() == 1;
This gives the correct results for your two inputs.
Solution 1
Invalid= {111, 222, 333}, {111, 110, 101}. Valid = {111, 111}, {222, 222}
char c = arr[0].ToString()[0]; //get the first digit to compare
//char c = arr.First().ToString().First()
bool isValid = arr
.SelectMany(i => i.ToString()) //select all digits as chars
.All(s => s == c); //compare them to the c char
Using only LINQ (Bad performance)
bool isValid = arr
.SelectMany(i => i.ToString()) //select all digits as chars
.All(s => s == arr.First().ToString().First()); //compare them to the c char
The second example will create an IEnumerable<Char> for each digit of the Array (Bad performance)
Solution 2
Invalid= {111, 110, 101}. Valid = {111, 111}, {222, 222}, {111, 222, 333}
bool isValid = arr
.SelectMany(i => i.ToString())
.Distinct()
.Count() == 1
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
im trying to make a song in beeps with pi and i need to delet the dot (pi1[1]) () and i need to convert the char array pi1 to int array pi2 (*)
double a = Math.PI;
string b=a.ToString();
char[] pi1 = b.ToCharArray();
* pi1[1] = '0';
int[] pi2;
for (int i = 0; i < pi1.Length; i++)
{
** pi2[i] = int.Parse(pi1[i].ToString());
}
//for (int i = 0; i > 40; i++)
//{
// Console.Beep(100*c, 100);
//}
The part that seems to be causing the problem is the need to not try to convert the decimal point. You can do this using string.Replace.
The following is a one-line solution to get the first 15 digits of pi as an array of integers:
int[] piDigits = Math.PI.ToString()
.Replace(".", "")
.Select(c => c - '0')
.ToArray();
Assuming that pi1 consists entirely of the characters 0–9, you could parse it into an int array using:
int[] pi2 = pi1.Select(c => c - '0').ToArray();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to C# so please be gentle. I am using c# in a transformation script and I need to find the 6th highest value in a list such as this
57
50
90
60
57
93
100
53
73
87
77
I can change it into a string array by using
string [] arr = args.Content.Split("\r\n".ToCharArray());
but I get lost from there
Thanks
Paul Fone
If you want to sort it numerically you have to convert the strings to int first, then you can use Enumerable.OrderByDescending and Enumerable.Skip(5).Take(1):
IEnumerable<int> ints = arr.Select(int.Parse)
.OrderByDescending(i => i)
.Skip(5).Take(1);
Console.Write("Sixth element is: " + ints.First());
or create a new list from the ordered sequence at then use Enumerable.ElementAt:
List<int> ints = arr.Select(int.Parse).OrderByDescending(i => i).ToList();
Console.Write("Sixth element is: " + ints.ElementAt(5));
(omitted exception handling for invalid format or too little items)
You can use LINQ, like this:
var res = args.Content.Split("\r\n".ToCharArray())
.Select(int.Parse)
.OrderBy(x=>x)
.Skip(5)
.FirstOrDefault();
To start, you need to first convert your numbers into an int[]. You can do that like this:
string[] strs = args.Content.Split("\r\n".ToCharArray());
int[] ints = new int[strs.Length];
for (int i = 0; i < strs.Length; i++)
ints[i] = int.Parse(strs[i]);
Then you can use Array.Sort(ints); to actually sort them. Then, you use int result = ints[ints.Length - 6 - 1]; to get the sixth-to-last element in the sorted array: that is, the 6th highest element.
The completed code looks like this:
string[] strs = args.Content.Split("\r\n".ToCharArray());
int[] ints = new int[strs.Length];
for (int i = 0; i < strs.Length; i++)
ints[i] = int.Parse(strs[i]);
Array.Sort(ints);
int result = ints[ints.Length - 6 - 1];