C# Checking if text-based answer is correct [closed] - c#

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.

Related

List.Reverse() without any effect in C# [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 2 years ago.
Improve this question
Given the Following code:
List<Digit> tempDigits = input.Digits;
string test = tempDigits.ToString(); // -> {2,2,7}
tempDigits.Reverse();
string test2 = tempDigits.ToString(); // -> {2,2,7}
This is my code:
and that the result (in between, I renamed the variable according to naming convention as suggested by Thiessen)
For some reason, the reverse soes not seem to do its job.
It must be something really simple. But I cant get what the Issue is. I hope, someone can help out.
The code you've posted would work correctly, in a vacuum. (Except, in what world does List<>.ToString() produce "{2,2,7}"?)
My main guess would be, as Lesiak commented, that this.Digits and input.Digits points to the exact same List<> instance. So Digits1.Reverse() reverses the list and Digits2.Reverse() reverses the reversal, putting it back how it started. Perhaps this and input are the same thing. Or perhaps they are two different things that happen to use the same underlying Digits list. Who knows?
There are many other possibilities, some of which would be impossible to guess at based on the information provided. For example:
Maybe the input control that's giving you that list is trying to actively manage the list, and reorders it as soon as it notices it's been reversed.
Maybe you're not using the System.Collections.Generic.List<> type, but instead you're referencing some other namespace where someone has written a List<> type that doesn't behave how you'd think it would. Maybe it doesn't even implement Reverse(), so the compiler is picking up on the Enumerable.Reverse() extension method, which does nothing to mutate the underlying data.
It really is impossible to know for sure without knowing more about your environment.
The information you give is incomplete to address the issue.
A reproducible example from the information you provide does not show the same symptoms.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> digits = new List<int> {2,2,7};
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
digits.Reverse();
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
}
}
Which gives the expected output
227
722
https://dotnetfiddle.net/Hs9H0k
My guess would be, this.Digits is not a mutable reference. But that is just an hypothesis.

Saving Program Data in C# [closed]

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 7 years ago.
Improve this question
So I'm working on a quick program for my mom. It has a textbox user input, a combobox for category selection, and multiple textbox for representing each category. The idea is that she can input a number value into the textbox and select a category. Then that value is stored in its respective category's textbox. I am absolutely certain I did not write this code well, but I don't know C# very well. Long story short, I need a way to be able to save the data stored in the textbox categories so she can close the program. I've never worked with something like this before, even in other languages, so this is foreign to me.
EDIT:
Here is the code illustrating what I am currently doing (this code is under a button click action, private void button1_Click(object sender, EventArgs e):
string userInput;
userInput = textBox1.Text;
if(comboBox1.SelectedIndex == 0)
{
category1Box.AppendText(userInput +"\n");
}
I did not specify how I wanted to save the data , as I'm not even sure how I want to do it. Like I said, I'm not experienced in this area at all, so I don't even know what the different options I had were. Hopefully this edit helps.
Serialization is a fairly simple way to save and load data on a single machine. You mention a single user so I think this will fit your requirements, but without more information I can't be sure.
Your question is very ambiguous in terms of how you want to save data, so I'll go with a very simple solution. You can utilize a few methods in the File class in System.IO.
File.WriteAllBytes(string path, byte[] data)
File.WriteAllLines(string path, string[] data)
File.WriteAllText(string path, string data)
These methods are very useful, and I suggest you learn to use them!

Theorycrafting a program to create mnemonic major trigger words and need some advice [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 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.

Quiz game application [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm new to C# programming so I need to ask C# experts here, what techniques should I use for a quiz game application I'm planning to develop. I'd like my quiz app to be like this:
1.) What is the capital city of UK? a. London b. Washington D.C. c. Tokyo d. Manila
2.) What is the capital city of Russia? a. Bangkok b. Beijing c. Islamabad d. Moscow
ETC....
I want the questions to be randomly generated using Rand(). The questions should be randomly placed, not in the order I set up here, but still those 4 choices I declared are the only ones to display in the set of the quiz. If you have any links to tutorials please give me so I can study it. I really love to develop this app, but I don't have any clue to start this. Any help is truly appreciated. Thanks!
There are many different ways to go about doing this. The way to do it if you want the option of being able to easily maintain the questions and answers is to put them in an XML file. You could then use XMLDocument to load the questions and answers at runtime. You XML file would look something like this:
<?xml version="1.0" ?>
<quiz>
<question>
What is the capital city of Russia?
<answers>
<correctAnswer>Moscow</correctAnswer>
<wrongAnswer>Bangkok</wrongAnswer>
<wrongAnswer>Beijing</wrongAnswer>
<wrongAnswer>Islamabad</wrongAnswer>
</answers>
</question>
</quiz>
You can parse this in C# using XMLDocument.
First you should understand your problem. Just check your requirements and think of objects. You certainly have "question" and "answers". Each Question has 4 possible answers and only one is correct. So a first, very simple approach would look like that.
class Question
{
public string QuestionText{ get; set; }
public string AnswerA { get;set }
public string AnswerB { get;set }
public string AnswerC { get;set }
public string AnswerD { get;set }
}
This is a good start, but not perfect. You could now store the correct answer aswell inside this question object. But to use this new property to its fullest, it would make sense to make our answers a bit more dynamic.
class Question
{
public Question()
{
Answers = new string[4];
}
public string QuestionText{ get; set; }
public string[] Answers { get;set; }
public int CorrectAnswer {get;set; }
}
So with this small object we can now create all our questions like this:
var question = new Question();
question.QuestionText = "What color is snow?";
question.Answers[0] = "Red";
question.Answers[1] = "Yellow";
question.Answers[2] = "White";
question.Answers[3] = "Green";
question.CorrectAnswer = 2;
// ... more questions
var listOfQuestions = new List<Question>();
listOfQuestions.Add(question);
How to sort randomly is another topic which is not difficult to find here on SO.
I personaly like icemaninds idea, you can use his answer to improve my basic approach.
What is your intended data source and how big is it? If you can define the format of your data source, I would suggest having a text file in which each line has three or four fields, separated by some sort of delimiter. The fields would be a question, the correct answer, and either a list of characters indicating the categories into which the question and answer both belong, or a list of categories for the question and another for the answer.
To clarify the last point, in many multiple-choice tests, if one were to simply choose ten questions at random from a pool of 25, and then for each question print three random answers from the pool along with the correct answer, one may end up with a question like: "How many sides does a triangle have? (a) square (b) Euclid (c) three (d) rhombus". A COMPUTE! magazine article some decades back offered a multiple-choice quiz generator which solved this problem by what it called "discrimination"--attaching categories to questions and answers, and for each question only picking answers that were suitable for the question's category. I don't remember how that article did things, but would suggest for simplicity of coding and data entry that you identify categories of questions and answers, and pick a letter for each. For the above question, a reasonable category might be "written-out whole numbers less than thirteen", so if one arbitrarily decides to use the character "Q" for that, both the question and answer would have a category of "Q". In many cases, a single category for question and answer would suffice (I think that's how the COMPUTE! program worked, but in some cases one may need to allow something more sophisticated (e.g. for "A shape with four sides, and with pairs of opposite sides equal, is:", it may be reasonable to offer up "pentagon" as an option, but probably not "square", "rectangle", or "rhombus").
There are a few more issues to consider in the design of the data set, such as how it should handle the possibility that multiple questions may have the same answer, and whether answers should be listed in random order or consistent order (e.g. for "How many sides does a pentagon have", it may be nicer to list the answers as "(a) three (b) five (c) six (d) eight" than as "(a) eight (b) five (c) six (d) three").

Conditional Logic dynamically

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...

Categories

Resources