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.
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 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 5 years ago.
Improve this question
The static Matches is
public static void Matches(string expectedRegexPattern, string actualString);
What is the difference between when I pass expectedRegexPattern value instead of actualString value and passing actualString value instead of expectedRegexPattern value?
Way 1 - Assert.Matches("EN", result[0].LanguageCode);
Way 2 - Assert.Matches(result[0].LanguageCode,"EN");
Both of ways doing same work with same performance. So am confused about the difference between the above ways and which one is best?
#Stivi correctly points out a significant difference in the case of Matches, but there's another less significant but still important distinction even for Equals and other assertion methods.
You might get an incorrect message in the test log output. Many testing frameworks will log an error such as
Values do not match: expected: {expectedValue}, actual: {actualValue}".
So if you switched them, you'd see the wrong "expected" value, potentially confusing someone trying to diagnose a problem.
Take this test as an example:
void TestValueIsZero():
{
int value = 1;
Assert.Equals(0, value);
// logs "Values to not match. expected: 0, actual: 1
Assert.Equals(value, 0);
// logs "Values to not match. expected: 1, actual: 0
}
Someone looking at the failed test logs might be confused by the incorrect "expected" and "actual" values in the log due to the switched parameters.
The mechanics may be exactly the same regardless of the order of parameters (compare the two objects and return true if no differences are found), but the semantics are still important.
I expect that you are using xunit as test framework because i have found that method there. So if you only want to check if both strings have the same content you could also use Assert.Equal("Your text", "Your text"). The Assert.Matches method is used to validate if your text is in a desired format. The format is described by .net regular expressions. For example with this call Assert.Matches("^[0-9]$", "1") you can check if your string is a number between 0 and 9. If you change the order of the parameters to Assert.Matches( "1", "^[0-9]$") the method will throw an exception and your test will be marked as failed.
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 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck
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 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 9 years ago.
Improve this question
In one of the interview I was asked to test a method. The details are mentioned below. Though I could answer , still they were expecting some more test cases. Am I missing any scenarios here ?
string concatenatefunc(strin1,string2).
{
//returns concatenation
}
This method accepts two string parameters and returns the concatenation. No other details are mentioned. I need to test this method and i have written below scenarios/unit test cases:
1.Pass empty parameters and see empty string is returned
2.Pass valid non empty strings and see the string returned is correct or not.
3.Pass the special characters in the both the parameters and test the response.
4.Pass integers and test the response.
5.pass large strings(not sure what we can give as the max length) and test the response.
....
Anything to add here?
You're not testing for null parameters.
Test for multi language support. An area that many developers fail to test which can give a guy like me problems. I have the danish character 'ΓΈ' in my name. This has been a problem in my web based interactions with several VERY BIG companies including software companies, preventing me from logins, accounts, payments etc.
Number 5 will really depend on the size of available resources so testing that might be problematic.
You could add tests for either parameter being empty and see if the non-empty parameter is returned
Same for Passing integers, test if either parameter is integer
Then you could start mixing them empty/integer, non-empty/integer, etc. and reverse