I search for reusable tool or algorithm that enable me to apply conditional logic to questions dynamically.
lets explain in details:
question 1 - What is your age?
a) less 18
b)between 18-25
c) greater than 25
if he choose
a) then he go to Question 2
b) he will goto question 5
c) he will goto question 7
So as I said the next question depends on the answer of the current question.I don't need to set if condition for each question .I need it to be dynamically.
I hope it is clear now . Is there any component or design pattern or algorithm implements what I said
All ideas are welcomed.
It sounds like you probably want a lookup table, effectively mapping the pair (input question, answer) to the next question number to ask. Perhaps it should default to "go to the next question" if there's no entry in the table.
Exactly how you represent it in data structures will depend on what you're using to store the questions. For example, in SQL you could have a table with columns of "input question, answer, next question". In C# you might have a Dictionary<Tuple<int, int>, int>... or possibly (if there aren't going to be huge numbers of questions) just a List<AnswerPath> where AnswerPath contains the same three values as the SQL table would have done. (Change the name, it's awful, but you get the idea.)
Consider building your questions as a graph, where each answer points to an ordered set of other questions.
Even better, give each question a set of prerequisites that must be satisfied by previous answers. For example, for question 2, the prerequisites might be that the answer to question 1 was either a or b.
Then when determining the next question, you would just iterate through each of them until you find the next one whose prerequisites are met.
Add a property called Next Question for every answer.
you could use a Dictionary<Tuple<Question,Answer>>,Question> so that the question and answer became the key for the next question.
Sounds like a job for the Chain of Responsibility pattern...
Related
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 3 years ago.
Improve this question
I am very sure that there is a technical term for this problem, but unfortunately I do not know it.
I have an alphabetical charset and the requirement is to create the combination of all the chars with a maximum length
The idea is (sample):
Generate a collection of A, AA, AAA, AAAA, AAAAA, AAAAAA
Next: A, AB, ABA, ABAA, ABAAA
Next A, AB, ABB, ABBA, ABBAA
The reason:
We have to query an API that delivers search results.
But if I don't get search hits from the API on AAA, I don't need to search for AAAA anymore, because it can't get search hits either. I can then move on to AAB.
The question:
My problem is that I'm not sure how the code has to be built to achieve this goal. I lack the structural approach.
I've already tried nested loops, but unfortunately I don't get the result.
I also used Combination Libraries, but they focus on other problems.
Many thanks for hints!
What you're looking for is a particular data structure called a Tree, but probably more specifically in your case, a Trie.
Trie data structures are commonly used in things like Autocomplete. With the image below, if someone typed "te", I can traverse the Trie and see what options would come after that (tea, ted, ten).
It looks like this would also fit your use case from what I can tell.
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 5 years ago.
Improve this question
Let's say I have few points : -5,-4,-3,-2,-1,0,1,2,3,4,5
I'm at point 0, I need to create a line that goes all through the points of 1,2,3,4,5,-1,-2... etc.
The line would start at 0 and end at whatever point that ends as the shortest.
The answer for this example would be that it'd go like this 0->1->2->3->4->5->-1->-2->-3->-4->-5 or that it'd go first to -1 and go all through the minus to the plus, same result (5*4=20 length).
If for example we'd go 0->1->-1->2->-2... it'd end as the longest line that goes straight from point to point (1+2+3+4+5+6+7+8+9+10=10*11/2=55 length)
The question is how to write this in code?
The points might also consist of 2 or 3 dimensional points, where the start would be (0,0,0,0) or whatever, eventually the line can go through all of these points, but which way will achieve the shortest line?
How to make it as a code, as we see it in the eye?
I think this is basically the Travelling Salesman problem. You've got N destinations, and each pair of destinations has a concrete length between them, and you're trying to find out the shortest travel time to visit all destinations.
You've got two different directions to pursue this, that I can see. First, is to read up on the Travelling Salesman problem and the various algorithms that have been proposed for it (it's a very famous algorithm problem) and then try to implement one in C# - though just to warn you, you should be very proficient in math, because it's not an easy problem. Or, alternatively, you can look for someone else's existing implementation for it and just use it without understanding the theoretical underpinnings.
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 7 years ago.
Improve this question
Good day all. Say I have a question:
What is a reverse reaction?
And the answer to this question is:
A reverse reaction is a reaction in which the products react to form
reactants and vise versa.
Now what would be the best way to determine if a user-inputted answer this question is correct or not? I can think of a couple of ways but they aren't practical.
One of the ways:
string answer = "A reverse reaction is a reaction in which the products react to form reactants and vise versa.";
string input = Console.ReadLine();
if (input.Equals(answer))
{
//answer is correct
}
Other way:
Checking to see how many words match and getting a percentage from that. If it calculates to a certain percentage then the answer is right.
Number of words: 17
Number of words in input that match answer: 17
Correctness percentage: 100%
Another way:
Check to see if the input contains certain key phrases.
string input = Console.ReadLine();
string[] keyPhrases = new string[] { "Products react to form reactants" };
foreach (string keyPhrase in keyPhrases)
{
if (!input.Contains(keyPhrase))
{
//answer is incorrect
return;
}
}
If what you mean by correctness is semantically correct, and the user is free to put up his answer, then I believe there is no simple way at this moment to do that by programming at all.
If you do it with the first way:
string answer = "A reverse reaction is a reaction in which the products react to form reactants and vise versa.";
string input = Console.ReadLine();
if (input.Equals(answer))
{
//answer is correct
}
And the user forgot to put the last little dot ".",
"A reverse reaction is a reaction in which the products react to form reactants and vise versa"
then he will get wrong, but he is actually correct
If you do it the second or the third way, then if the user simply mentions its negation, he may have high percentage of match but totally wrong in his concept:
"A reverse reaction is NOT a reaction in which the products react to form reactants and vise versa"
As of now, I believe the best way to do this is by restricting the user inputs to multiple choices provided by you.
And one of the best items to do this is the radio buttons. But you could do this by combo box and button or ListBox which allows single/multiple choices as you want it, but the bottom line is the same:
restrict your user inputs or you cannot tell whether his answer is semantically right/wrong easily.
It may require expertise in grammatical understanding, lots of dictionary words, complex words-meanings relationship models, and excellent background contexts interpretations otherwise.
That being said,
Regex cannot help to check if an answer is semantically correct - it can only help you to find a pattern which you may use to check if the user puts a semantically correct answer.
Thus...
If it is used together with human inspection, then probably your second and third ways + Regex would give some benefits.
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 8 years ago.
Improve this question
Here is the mnemonic major system:
Basically, the table is as follows:
0: z or s
1: t or d
2: n
3: m
4: r
5: l
6: g or j
7: k or c
8: f or v
9: p or b
So as an example the number "31415" would be "mtrtl". Then you just fill in with any vowels or spaces that makes the gibberish into words so you might do something like "meteor tail" ("mtr tl" with vowels in between the letters). Now you can just think of "meteor tail" and decode it to remember "31415".
There are already tools like this but for a lot of numbers they don't work. Here is where the programming comes in.
I notice if you google some gibberish it often comes up with a good guess for the word you might have meant. I was thinking of perhaps making a database of words by googling consonant strings with every combination of vowels between them to see which has the most Google results and listing the top 10 for each one, but I doubt Google would appreciate that very much.
I haven't started coding yet but I feel like a dictionary won't cut it for this, but maybe it could work. Is there a list (just a long list, no definitions or formatting) of English words that I could use? Is there a more effective way of going about this rather than just brute-forcing every vowel combination with every sequence of consonants and recording which ones show in in a dictionary?
Thanks for reading! C# is my preferred language so I'm just going to tag it as such.
The most known free English dictionary is WordNet. Actually, it is not really a dictionary, but a lexical database, but you can use one of its tables as dictionary.
But it seems that the page you are refering to with the description of the Mnemonic major system has already done this! When you enter a number on this websites, it seems to search WordNet (and much more!).
Good luck.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.