compare different combinations of a string [closed] - c#

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 4 years ago.
Improve this question
I have a dictionary with key value pairs something like below
dict("A-B-C", "This is abc");
dict("X-Y-Z", "This is xyz");
so on
now when I receive an input say something like "B-A-C" the system should intelligent to know its "A-B-C" or if its "Z-Y-X" should map to "X-Y-Z", one way is to put all the combinations into the dictionary such that say dict("B-A-C", "A-B-C") but this may lead to more maintainability thing, just thinking if there is anything from .NET framework can address this issue more easily.
Is there an easy way to compare a string like below
A string either A-B or B-A is equal to A-B
A string either A-B-C or B-C-A or A-C-B or C-A-B or C-B-A equals to A-B-C
likewise a different combinations of A-B-C-D should equals to A-B-C-D
so as A-B-C-D-E
so on
A B C D E in the above example are a two letter non numeric word. something like
Su-Ma-Ju-Ve
UPDATE
For now I solved this with below code which returns the string tokens in the order which is recognized in the dictionary I maintain, not sure if its the best way but is solving the problem for now
string sortPls(string pls)
{
Dictionary<string, int> dctPls = new Dictionary<string, int>();
dctPls["Su"] = 1;
dctPls["Mo"] = 2;
dctPls["Ju"] = 3;
dctPls["Me"] = 4;
dctPls["Ve"] = 5;
dctPls["Ma"] = 6;
dctPls["Sa"] = 7;
string[] arrPls = pls.Split('-');
int j = 0;
string sortPls = string.Empty;
for(int i = 0; i < arrPls.Length; i++)
{
for (j = i + 1; j < arrPls.Length; j++)
{
if (dctPls[arrPls[j]] < dctPls[arrPls[i]])
{
string tmp = arrPls[i];
arrPls[i] = arrPls[j];
arrPls[j] = tmp;
}
}
}
for (int k = 0; k < arrPls.Length; k++)
sortPls += arrPls[k] + "-";
return sortPls.Remove(sortPls.Length - 1) ;
}

Yes. If looking for set equality, then HashSet<T> gives you the tools.
"ABBA".ToHashSet().SetEquals("BA")
Otherwise for an "anagram" type comparison, order the characters and compare sequences:
"CDAB".OrderBy(x=>x).SequenceEqual("DCBA".OrderBy(x=>x))

Split the string into individual letters, add the letters to a Set, and compare the sets using their Equals methods

void Main()
{
string check = "CADB";
string equalTo = new string(check.ToCharArray().OrderBy(x => x).ToArray());
Console.WriteLine(equalTo);
}
is just one way to do that.

I think, this will be the fastest way to do it:
bool StringEquals(string string1, string string2)
{
foreach (char ch in string1)
{
if (!string2.Contains(ch))
{
return false;
}
}
return true;
}

Sort the characters in each string, any two strings that have the same characters will have their sorted version the same. Stick the results into a `Dictionary>, with the sorted version as the key.

Related

Master mind in console [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 5 years ago.
Improve this question
I want to make some sort of mastermind game in the console where the computer generates a 3 digit code and you have to guess it. The computers also tells you which digit you get right after you take a guess. How would i best do this? I don't know a way to "chop up" input so that i can make the computer check it against the digit it has.
example:
computer generates random code: 123
you guess: 321
computer : -*- (the star indicates which digit you got right)
I have an idea about how i'd indicate what digits are right or not, but i don't know how to separate the input into parts
Convert the computer generated random number into a string. The user input is given as string anyway.
The string type has an indexer allowing you to access single characters
string s = "abc";
char a = s[0];
char b = s[1];
char c = s[2];
for (int i = 0; i < s.Length; i++) {
Console.WriteLine($"s[{i}] = '{s[i]}'");
}
You write char literals as
char ch = 'x';
using single quotes.
Example:
string userInput = Console.ReadLine();
int code = SomehowGenerateRandomCode();
string codeString = code.ToString();
// Let's assume that both numbers have 3 digits.
for (int i = 0; i < 3; i++) {
if(userInput[i] == codeString[i]) {
// digits are equal
} else {
// digits are different
}
}
To split an int to its digits you can convert to string an split like:
int i = 123;
string myInt = i.ToString();
List<int> digits =new List<int>();
foreach (char c in myInt)
{
digits.Add(int.Parse(c.ToString());
}

Can we add two integer variable names in C# rather than values? [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 5 years ago.
Improve this question
In c # i'm trying to add two different integer name, is it possible
int variablename;
for(int i=0;i<10;i++)
{
Objectname.variablename+i
}
No, you cant. C# is strongly typed programming language. What you can do, is to use Dictionary for example, like this:
Dictionary<string, int> names = new Dictionary<string,int>();
for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
If I understand you correctly you want to get the name of your variable and then change the name for each instance based on the iteration, thus you want to add a numeric value to the name. You can achieve this as follows:
string testVariable = "";
int i = 1;
// Get the name of your variable. This will result in "testVariable" as value.
string nameOfTestVariable = nameof(testVariable);
// Now add your integer to the name, but add it as a string.
nameOfTestVariable += i.ToString();
//Result, value of nameOfTestVariable: "testVariable1"
To get the datatype of your variable instead:
int a = 0;
int b = 1;
string dataTypesOfVariables = a.GetType().Name;
dataTypesOfVariables += b.GetType().Name;
//Result, value of dataTypesOfVariables: "Int32Int32"

Check if 2 strings contain same letters at same index [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 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}

Comparing a part of a string. C#

I want to compare and check if a string is a part of another string. For example:
String1 = "ACGTAAG"
String2 = "TAA"
I want to check if String1 contains String2. Im using this code but it does not work.
public bool ContainsSequence(string input, string toBeChecked)
{
for (int i = 0; i < input.Length; i++)
{
Char x = input[i];
String y = Convert.ToString(x);
for (int j = 0; j < toBeChecked.Length; j++)
{
Char a = toBeChecked[j];
String b = Convert.ToString(a);
if (b.Equals(y))
{
j = toBeChecked.Length;
return true;
}
}
}
return false;
}
input = string1 and tobechecked = string 2.
Im new in c# so some terms may be confusing.
try use String.Contains()
Check it out here:
http://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx
Good luck.
Use
If(mainString.Contains(searchedString)
{
//do stuff
}
It looks to me like you're using this to compare DNA sequences :).
Maybe string.IndexOf(string value) or one of it's overloads is better for you because it can help
you search for further occurences of the same string (stuff like "how many times does it contain the string"): http://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx
If indeed all you want is just to see if the string is contained, I'd also go for the versions provided by the others.
For nucleotide sequences you probably want some sequence alignment algorithm which has some tolerance when sequences are not equal. For example Smith–Waterman algorithm.

Get all possible word combinations

I have a list of n words (let's say 26). Now I want to get a list of all possible combinations, but with a maximum of k words per row (let's say 5)
So when my word list is: aaa, bbb, ..., zzz
I want to get:
aaa
bbb
...
aaabbb
aaaccc
...
aaabbbcccdddeeefff
aaabbbcccdddeeeggg
...
I want to make it variable, so that it will work with any n or k value.
There should be no word be twice and every combinations needs to be taken (even if there are very much).
How could I achieve that?
EDIT:
Thank you for your answers. It is not an assignment. Is is just that I forgot the combinations of my password and I want to be sure that I have all combinations tested. Although I have not 26 password parts, but this made it easier to explain what I want.
If there are other people with the same problem, this link could be helpfull:
Generate word combination array in c#
i wrote simple a function to do this
private string allState(int index,string[] inStr)
{
string a = inStr[index].ToString();
int l = index+1;
int k = l;
var result = string.Empty;
var t = inStr.Length;
int i = index;
while (i < t)
{
string s = a;
for (int j = l; j < k; j++)
{
s += inStr[j].ToString();
}
result += s+",";
k++;
i++;
}
index++;
if(index<inStr.Length)
result += allState(index, inStr);
return result.TrimEnd(new char[] { ',' });
}
allState(0, new string[] { "a", "b", "c"})
You could take a look at this
However, if you need to get large numbers of combinations (in the tens of millions) you should use lazy evaluation for the generation of the combinations.

Categories