This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combination Generator in Linq
I am looking of an algorithm (using C#) that can
find all the combinations of specified numbers.
Example:
Numbers:
1 2 3
Combinations:
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
Only rule: No repetitions of numbers
I have looked around Google, Stackoverflow, as well as numerous other sites.
I would list some of my code, but I have had no success getting anything to work along the right lines.
EDIT:
The intention of this is using the generated numbers as the positions of characters in a word. I am creating a word finder, so basically this is what it is being used for:
Program generates:
0
1
01
10
From numbers: 0 1
The program got the numbers 0 and 1 from the user inputting for instance "no".
Example code:
string input = Console.ReadLine();
int size = input.Length; //This is where the 0 and 1 come from
Therefore the different combinations would rearrange the letters, using the length of the inputted word as the base, then comparing it to a word list, I could find existing words.
What you really need is a proper utilization of recursion. I think Permutations in C# Using Recursion is exactly what you are looking for.
I'll do more research as I have time and try to improve my answer.
Your program is dealing with combinatorial math (which you can google and read about). There is a formula for calculating your answer. Your question falls under the "Pick x from n" variety with respect to order.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I've thought of using a if statement to say if that number has already been produced into my line change the number which it was going to produce into a different number other then that, but I must of wrote it incorrect since it does not work.
Let's consider a sequence of random hex "digits" :
f 5 3 0 0 5 e 8 e 8 5 6 f
A naïve approach would try to check if the previous number is equal to the next in the sequence and discard it.
That will not work since only a small subset of the duplicated numbers will be come in direct sequence.
What you will need is to keep a track of the ALL numbers already present.
if you store your final sequence in an array you could check for each element, as long as the number of the desired elements is low.
For example :
f (first elements nothing to check agains, add it)
f 5 (check against index 0-0 , f, not present add it)
f 5 3 (check
against index 0-1 , 3, not present add it)
f 5 3 0 (check against
index 0-2 , 0, not present add it)
f 5 3 0 0 (check against index
0-3 , 0 , found - skip it)
f 5 3 0 5 (check against index 0-3 , 5 ,
found - skip it)
f 5 3 0
8 (check against index 0-3 , 8, not
present add it)
f 5 3 0 8 e (check against index 0-4 , e, not
present add it)
f 5 3 0 8 e
and so on. This is basically what a human eye would do, if you cannot keep all the numbers in your head.
If the sequence is large scanning the output array will quickly become too inefficient and slow.
There ways to optimize the check by using hash sets/maps.(see Remove duplicate values from JS array and Selecting Unique Elements From a List in C# )
If the number of possible elements is low, you might want a "shuffle" to get your output (see Random shuffling of an array).
I need help with sorting. I have table of numbers stored as Int32[,] array
1 2 3 4 5
5 6 7 8 6
9 10 11 12 7
13 14 15 16 8
and I need to sort it (lines not columns) according to another array for example 1 8 3 2.
Length of second array and number of lines in first always match. I know you can use Array.Sort(firstArray, secondArray); to sort first array and change second so indexes matches but that works only if second array is 1D.
Can anyone help? I know you can create comparator but how to create it for this case?
Thanks for responses in advance...
PS: I tried to find solutions here, nothing really helped and I have been trying for a while now...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
something went wrong in c# while coding a project, it divides and multiplies all different datatypes and results 27 or 26 or 108 , how to resolve ?!
Console.Read() reads a character from the keyboard, and lets you store it where you want. You typed a 6, which may look like a number to you, but to the computer is merely another character, like A, ! or &.
The character '6' has the (ASCII) code 54. Divide that by 2 and you get 27...
Problem is, code using Console.Read to read an integer, but Console.Read reads next character from input stream (Will not wait for enter). Moment when type 67, first character(6) will be converted to int and assigned to variable a immediately, so a gets 54 (ASCII value).
Which will get divided by 2 results 27
To fix your problem, use Console.ReadLine, which reads the line of characters.
int a = int.Parse(Console.ReadLine()); // or use `int.TryParse`
This question already has answers here:
How to get all the unique n-long combinations of a set of duplicatable elements?
(5 answers)
Closed 3 years ago.
I am trying to save every combination of AAAAAAAA - ZZZZZZZZ to a text file. So far after having many many errors, I have got almost nowhere. I could post my code if needed, but it doesn't work or get near the wanted outcome.
So I was wondering how to do this in c#. My method at the moment is beyond repair, I will have to start all again in order to fix this.
As the output I would like something along the lines of
AAAAAAAA, AAAAAAAB, AAAAAAAC ... ZZZZZZZX, ZZZZZZZY, ZZZZZZZZ
Thanks in advance for any help.
This is a basic combinatorics question:
You want to write a string of 8 characters.
Each character can be a letter between A-Z (26 options), therefore, there are 26^8 combinations: 26*26*26*...26.
That is 208827064576 combinations.
Each combination is 10 bytes (8 for string, then \r\n), which is a total of 1944.85 GB.
Are you sure you want to write it to a file?
This will take about 1.5-2 Terabytes. That's a huge text file to start with, probably impractical.
Secondly, the way to do this simply is to have 8 nested loops, each running through A to Z, then concatenate the string inside the inner loop, appending to the data store each time.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How would I figure out the next number to come in a pattern programatically in C#?
For example, if i had a pattern (3, 6, 9, 12), how would I programatically figure out that the next number is 15, and then 21, and so on?
Thanks.
Well, first you need to know the type of pattern or programatically figure that out. Linear number patterns increase by addition or subtraction. Exponential patterns increase by multiplication or division. With that, you have to start at the first number and determine the difference. Then look at the next number and see if it increases by the same amount. If it does, you have the pattern and just add the difference to the last number. If its not, its an exponential function and then you need to determine how much it increases with each number in the pattern, to project the next one.
Without seeing code, I can only show you the concept. Hope it helps!
You may find this useful.
Dangit, my response got lost with wireless failure. Lemme try again:
You could do it as a breadth-first search on combinations from a RPN language. Start with your pattern on the stack; ignore stack underflows, as they indicate that you're at the sequence seed. Use basic operators and single digits, for a good balance between expressive power and not exploding the search space too much. For example (and obviously, showing only some interesting points in the search space):
for +:
3 +: Stack Underflow - ignore
3 6 +: 9
3 6 9 +: 15 - wrong
for *:
3 *: SU
3 6 *: 18 - wrong
for 1:
3 1: 1 - wrong
for + -:
3 + -: SU
3 6 + -: SU
3 6 9 + -: -12 - wrong
for 3 +:
3 3 +: 6
3 6 3 +: 9
3 6 9 3 +: 12
success
next:
3 6 9 12 3 +: 15
3 6 9 12 15 3 +: 18
This will produce the simplest explanation for the sequence. As Samuel Edwin Ward notes, for all we know, there could be a complex algorithm that produces 4 as the next item, or "banana", for that matter. For instance, many people here would instantly "know" the next term in this sequence:
1
2
"fizz"
4
?
(The answer is, "obviously", "buzz", even though the pattern is not actually demonstrated in the example, only our experience.)
EDIT: Stack Underflow, not Overflow :/