Figuring out the next number(s) in a pattern [closed] - c#

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 :/

Related

Order of Value.CreateBatch

Can someone please confirm or correct my understanding of the order of values when using Value.CreateBatch instead of MinibatchSource?
Assuming the CTF equivalent is:
|x 1 2 3
|x 4 5 6
|x 7 8 9
Would a batch of size 3 become (a) 1 2 3 4 5 6 7 8 9 or (b) 1 4 7 2 5 8 3 6 9?
My current perception is (a) however I'd greatly appreciate this being confirmed or corrected. Many thanks in advance.
Of course, a) is the correct answer. In each line x is the name of the input data followed by the input data itself.
It is implicated in this tutorial for CNTK: https://cntk.ai/pythondocs/CNTK_103C_MNIST_MultiLayerPerceptron.html#Data-reading
and I also can tell it from my experience.

Day Validation using Regex [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
I'm trying to use Regex to validate the days in a month. With this i am able to validate 01 to 31. How can i also validate 1-31, so that way i can either have 01 to 31 or 1 to 31
(0[1-9]|[12]\d|3[01])
Following will work for you.
(([12]\d)|(3[01])|(0?[1-9]))
I think you didn't understand the way your expression works.
Here it is:
| stands for OR.
Thus you have 3 cases here:
[12]\d
3[01]
0?[1-9]
1 - match either 1 or 2 as the 1st character and \d match any digit (same as [0-9]) as the second character.
2 - match 3 as the 1st digit. And either 0 or 1 as the second one.
3 - match with any digit between 1 to 9 (both included). 0? add an optional 0 as the 1st character.

How to select various-length sections from list using LINQ [closed]

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 9 years ago.
I have a list (it could be a list of complex type, but for better idea explanation I will use string).
Edit:
Please, do not provide me split method solutions. They are completely useless. I have a list of complex type, string is only simplification
There are some repeated elements which indicates start of new section. Every section may have various-length.
Example:
(in my real problem - every string is a part of complex type)
class Complex
{
public string Header { get; set; }
...
...
...
}
start
1
2
3
4
5
6
7
start
1
2
3
start
1
2
3
4
5
6
start
1
start
1
2
I would like to get all possible lists using LINQ. I mean
1 2 3 4 5 6 7
1 2 3
1 2 3 4 5 6
1
1 2
How to do this using only LINQ?
use GroupAdjacent method, provided my MoreLinq
var strings = new []{"start","1","2","3","4","5","6","7",
"start","1","2","3",
"start","1","2","3","4","5","6",
"start","1",
"start","1","2"};
strings.GroupAdjacent(v => v != "start")
.Where(grp => grp.Key) //skip "start" items
.ForEach(groupItems => Console.WriteLine(string.Join(" ", groupItems)));
prints:
1 2 3 4 5 6 7
1 2 3
1 2 3 4 5 6
1
1 2
MoreLinq also provides Split extension method:
strings.Split("start")
.Where(grp => grp.Any()) //skip first item as empty collection
.ForEach(groupItems => Console.WriteLine(string.Join(" ", groupItems)));
will print the same
Try this way
YourList.Split('start').ToList();
Hope this will help.

Finding all possible combinations of numbers [duplicate]

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.

How to generate 6 digit random number based on current date & time [closed]

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 11 years ago.
what logic i can use to generate 6 digit random number based on current date and time. suppose i want to generate a 6 digit random number which will be valid up to 5 second. after 5 second if anyone input that random number into system then system will say it is expire. one thing i need to mention that there will be no database interaction. i don't want to generate random number and store it in database table. 5 second validity logic will be embedded in generated random number as a result i can validate it later that whether it is generate before 5 sec or not.
i asked this question in another forum and they gave me code to generate 6 digit random number based on current date and time like
var random = new Random(System.DateTime.Now.Millisecond);
int randomNumber = random.Next(1, 500000);
is it ok? because i am not advance developer.if i can generate 6 digit random number using the above logic then later how can i determine programatically that the number was generate before 5 sec or not. basically i need two routine one for generating 6 digit random number based on current date & time and another routine will check that generated number was generated before 5 sec or not.
please guide me with concept and code. thanks
If it is the same process which stays during these five seconds, you could just store the key inside an hashtable, and to validate it check that it is inside the hashtable. Also keep in the hashtable the creation time of the key, and periodically clean that hashtable (removing obsolete keys and associated data).
Of course you'll need to generate that key either "randomly" or "cryptographically"
You can't reverse the process of random number generation to determine when the number were generated, particularly if you're seeding Random with milliseconds.
Getting a 6-digit random number:
int randomNumber = random.Next(0, 1000000);

Categories