Non repetive Alphanumeric 10 digit Char using LINQ [closed] - c#

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.

Related

Make the largest number of the number [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 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)));

3 random numbers into array [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am writing the beginning of my program which is to have the computer generate 3 random numbers on the console.
They need to be 1-9 (including both), no numbers can repeat, and I need to put this answer into an array. What's needed in the main method class vs the class.
You should try it out yourself first (site rules say so), but some hints may be provided:
1) integer random numbers can be generated using Random class. More details can be found here and an answer has been already provided about generation
2) to avoid duplicates each number should be tested against the existing list of numbers:
array.Contains(generatedNumber)
3) For your particular request, an elegant option is to generate all numbers between 1 and 9, shuffle the array and pick the first three elements:
var initArray = Enumerable.Range(1, 9).ToArray();
var randomArray = initArray.OrderBy(x => rnd.Next()).ToArray();
Get first three elements and those are random and distinct.
Generally, you can a subarray using the method specified here.
Try this:
Random rnd = new Random();
int[] arr = Enumerable.Range(0, 10).OrderBy(n => rnd.Next()).Take(3).ToArray();
foreach (var n in arr)
{
Console.WriteLine(n);
}
Try this code below :
//range set 0 to 9
int Min = 0;
int Max = 10;
//declare an array which store 3 random number
int[] arr = new int[3];
Random randNum = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = randNum.Next(Min, Max);
Console.WriteLine(arr[i]);
}

C# loop through sets of items in list [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 list of Strings, sometimes over 10.000.000 strings in the list.
I need to loop through the list and send chunks of this data to an API, a sublist of 2k strings per API-call.
What would be the most efficient way to do this?
Use LINQ Take and Skip operator wisely with variables.
For example syntax will be something like below-
IEnumerable<resultStrings> page1 = myStrings.Skip(0).Take(2000);
IEnumerable<resultStrings> page2 = myStrings.Skip(2000).Take(2000);
Try GetRange method:
List<int> list = new List<int>() { 1, 2, 3, 4, 5};
var chunk = 2;
var iterations = list.Count / chunk;
for (int i = 0; i < iterations; i++)
{
var portion = list.GetRange(chunk * i, chunk);
//Do API
}
var remainder = list.GetRange(chunk * iterations, list.Count - chunk * iterations);
//Do API
You can look at some benchmark test on GetRange vs Take where GetRange wins.
https://icodeit.wordpress.com/2012/08/27/performance-of-skip-and-take-in-linq-to-objects/
Probably the most efficient approach is to use a database instead of loading all into memory(from wherever) and then use Skip/Take to take parts of it.
However, you could use GroupBy:
var chunks = largeStringList.Select((str, index) => new { str, index })
.GroupBy(x => x.index / 2000, x => x.str);
foreach (var chunkGroup in chunks)
Console.WriteLine(String.Join(",", chunkGroup));
I've run a little performance test with this result:
List.GetRange:
00:00:00.0404119 (40 milliseconds)
(my) GroupBy:
00:00:02.2386504 (two seconds)
Skip/Take:
00:10:11.6467726 (yes, more than 10 minutes)

char array to int array and delete an char c# [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 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();

How to find the 6th highest value in a list [closed]

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

Categories