I have a string array with values
patrick
portland
vic
and I want to achieve the following:
for the first pass I want to have an array with * appended to the end of each entry:
patrick*
portland*
vic*
for second pass, I want to replace * with a ~:
patrick*
portland*
vic~
patrick*
portland~
vic*
patrick~
portland*
vic*
for third pass, I want to replace another * with ~:
patrick~
portland*
vic~
patrick~
portland~
vic*
patrick*
portland~
vic~
and so on until all the * are replaced by ~.
Is there a way do it without recursion?
Edit 1:
Need the following strings generated based on: patrick portland vic
patrick*portland*vic*
patrick*portland*vic~
patrick*portland~vic*
patrick~portland*vic*
patrick~portland*vic~
patrick~portland~vic*
patrick*portland~vic~
patrick~portland~vic~
I thought it would be easier to split the string into an array and then work on.
Edit 2:
Managed to solve this using cartesian product.
string[] stnameSplit = streetName.Split(' ');
string[] chars = { "*", "~" };
var cartesianProduct = from name in stnameSplit
from cha in chars
select new { name, cha };
List<string> vals = cartesianProduct.Select(p => p.name + p.cha).ToList();
List<List<string>> embeddedList = new List<List<string>>();
int ctr = 0;
List<string> l = new List<string>();
foreach (string s in vals)
{
l.Add(s);
if (ctr % 2 == 1)
{
embeddedList.Add(l);
l = new List<string>();
}
ctr++;
}
var result = embeddedList.ToArray().CartesianProduct();
The last line calls the method CartesianProduct as described by Eric Lippert.
Thanks everyone for the help and guiding me in the right direction.
Given the solution that you posted in your question I thought it seemed a bit verbose. I thought I'd try to shorten.
Here it is:
string streetName = "patrick portland vic";
string[] split = streetName.Split(' ');
string[] chars = { "*", "~" };
IEnumerable<IEnumerable<string>> choices = split.Select(n => chars.Select(c => $"{n}{c}"));
IEnumerable<IEnumerable<string>> result = choices.CartesianProduct();
Related
I want to find two longest words from array ,made from smaller words. my code are given below.
current out put is:
catxdogcatsrat, ratcatdogcat, catsdogcats, dogcatsdog
required output is:
ratcatdogcat, catsdogcats
class program
{
public static void Main(String[] args)
{
List<string> list2 = new List<string>();
string[] stringrray = { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog",
"hippopotamuses", "rat", "ratcatdogcat" };
list2.Add(stringrray[0]);
list2.Add(stringrray[1]);
list2.Add(stringrray[2]);
list2.Add(stringrray[3]);
list2.Add(stringrray[4]);
list2.Add(stringrray[5]);
list2.Add(stringrray[6]);
list2.Add(stringrray[7]);
list2.Add(stringrray[8]);
List<string> list = new List<string>();
var mod = list2.OrderByDescending(x => x.Length).ToList();
int j = 1;
for (int k = 0; k < mod.Count; k++)
{
for (int i = 0; i < mod.Count-j; i++)
{
if (mod[i].Contains(mod[mod.Count - j]))
{
j++;
list.Add(mod[i]);
}
}
}
var mod1 = list.OrderByDescending(x => x.Length);
foreach (var i in mod1)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
I think you are looking for something like this
string[] stringrray = { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat" };
List<string> list2 = new List<string>(stringrray);
List<string> Finallist = new List<string>();
char[] smallstrchar = String.Join("", list2.Where(x => x.Length <= 4)).ToCharArray();
char[] bigstrchar = String.Join("", list2.Where(x => x.Length > 4)).ToCharArray();
char[] modchar = bigstrchar.Except(smallstrchar).ToArray();
foreach(string bigstr in list2)
{
if(!(bigstr.IndexOfAny(modchar) != -1))
{
Finallist.Add(bigstr);
}
}
Finallist = Finallist.OrderByDescending(x => x.Length).Take(2).ToList();
foreach(string finalstr in Finallist)
{
Console.WriteLine(finalstr);
}
So first is the stringrray which contains all the strings which are supposed to be taken care and find the longest one out of it. With your code it also takes the string which contains x in them but all other chars are matched. So I have made a list of strings in list2 which contains all the values. Then splitted the list2 in 2 parts that is smallstrchar array contains all the chars of the smaller strings less than length of 4 and Bigstrchar contains all chars of strings which are bigger than length of 5. Now Except takes out all the chars which does not exsist in the smallstrchar and present in Bigstrchar. Now we have the list of chars which need to be excluded from the sort.
Finally IndexOfAny to find in that string contains that char or not. If not then add to Finallist. Later we can take 2 from the list.
Hope this helps
You could simplify adding the array to list2 with
list2.AddRange(stringrray)
You may use this code...
static void Main(string[] args)
{
List<string> words = new List<string>() { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat" };
List<string> result = new List<string>();
// solution 1
foreach (string word in words)
{
if (IsCombinationOf(word, words))
{
result.Add(word);
}
}
// solution 2
result = words.Where(x => IsCombinationOf(x, words)).ToList();
}
public static bool IsCombinationOf(string word, List<string> parts)
{
// removing the actual word just to be secure.
parts = parts.Where(x => x != word).OrderByDescending(x => x.Length).ToList();
// erase each part in word. Only those which are not in the list will remain.
foreach (string part in parts)
{
word = Regex.Replace(word, part, "");
}
// if there are any caracters left, it hasn't been a combination
return word.Length == 0;
}
but...
This code has a little bug. The OrderbyDescending clause ensures that cats will be removed before cat. Otherwise the s would remain and the code wouldn't work as expected. But if we use some fictional values this code will not work properly. For example:
List<string> words = new List<string>() { "abz", "e", "zefg", "f", "g", "abzefg" };
Let's have a look at abzef. The algorithm will remove zefg first, but then it's not possible to go any futher. Indeed, the word is a combination of abz, e, f and g .
I want to ignore the punctuation.So, I'm trying to make a program that counts all the appearences of every word in my text but without taking in consideration the punctuation marks.
So my program is:
static void Main(string[] args)
{
string text = "This my world. World, world,THIS WORLD ! Is this - the world .";
IDictionary<string, int> wordsCount =
new SortedDictionary<string, int>();
text=text.ToLower();
text = text.replaceAll("[^0-9a-zA-Z\text]", "X");
string[] words = text.Split(' ',',','-','!','.');
foreach (string word in words)
{
int count = 1;
if (wordsCount.ContainsKey(word))
count = wordsCount[word] + 1;
wordsCount[word] = count;
}
var items = from pair in wordsCount
orderby pair.Value ascending
select pair;
foreach (var p in items)
{
Console.WriteLine("{0} -> {1}", p.Key, p.Value);
}
}
The output is:
is->1
my->1
the->1
this->3
world->5
(here is nothing) -> 8
How can I remove the punctuation here?
You should try specifying StringSplitOptions.RemoveEmptyEntries:
string[] words = text.Split(" ,-!.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Note that instead of manually creating a char[] with all the punctuation characters, you may create a string and call ToCharArray() to get the array of characters.
I find it easier to read and to modify later on.
string[] words = text.Split(new char[]{' ',',','-','!','.'}, StringSplitOPtions.RemoveEmptyItems);
It is simple - first step is to remove undesired punctuation with function Replace and then continue with splitting as you have it.
... you can go with the making people cry version ...
"This my world. World, world,THIS WORLD ! Is this - the world ."
.ToLower()
.Split(" ,-!.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.GroupBy(i => i)
.Select(i=>new{Word=i.Key, Count = i.Count()})
.OrderBy(k => k.Count)
.ToList()
.ForEach(Console.WriteLine);
.. output
{ Word = my, Count = 1 }
{ Word = is, Count = 1 }
{ Word = the, Count = 1 }
{ Word = this, Count = 3 }
{ Word = world, Count = 5 }
Here is the code and it is working fine for a single input string
string[] stop_word = new string[]
{
"please",
"try",
"something",
"asking",
"-",
"(", ")",
"/",
".",
"was",
"the"
};
string str = "Please try something (by) yourself. -befor/e asking";
foreach (string word in stop_word)
{
str = str.ToLower().Replace(word, "").Trim();
}
and the output is by yourself before
and now I want to have
string str[] = new string[]
{
"Please try something-by yourself. before (CAD) asking/",
"cover, was adopted. The accuracy (of) the- change map was"
};
and also may be the number of strings is greater than 2 then how to alter this above code to display the str array or store in a text file or database.
Please help with acknowledgements. Thanks
The code for single string need to be put inside a loop for string array
List<string> result = new List<string>();
for(int i =0; i<str.Length; i++)
{
foreach (string word in stop_word)
{
str[i] = str[i].ToLower().Replace(word, "").Trim();
str[i] = Regex.Replace(str[i], #"\s+", " ");
}
result.Add(str[i]);
}
foreach(string r in result)
{
//this is to printout the result
Console.WriteLine(r);
}
You can try it here: https://dotnetfiddle.net/wg83gM
EDIT:
Use regex to replace multiple spaces with one single space
Here is an easy to understand way to do it:
List<string> list = new List<string>();
foreach (string text in str)//loops through your str array
{
string newText =text;
foreach (string word in stop_word) //loops through your word array
{
newText = newText.ToLower().Replace(word, "").Trim();
}
list.Add(newText); //store the results in a list
}
Here is a working Demo
Does this work as you expect?
var results =
str
.Select(x => stop_word.Aggregate(x, (a, y) => a.ToLower().Replace(y, "").Trim()))
.ToArray();
I used this input:
string[] str = new string[]
{
"Please try something-by yourself. before (CAD) asking/",
"cover, was adopted. The accuracy (of) the- change map was"
};
string[] stop_word = new string[]
{
"please", "try", "something", "asking", "-", "(", ")", "/", ".", "was", "the"
};
I got this output:
by yourself before cad
cover, adopted accuracy of change map
You can use Select() for this.
var results = str.Select(x => {
foreach (string word in stop_word)
{
x = x.ToLower().Replace(word, "").Trim();
}
return x;
}).ToList(); // You can use ToArray() if you wish too.
...
foreach(string result in results)
{
Console.WriteLine(result);
}
Result:
by yourself before cad
cover, adopted accuracy of change map
Say I have the following 2 arrays...
string[] A = ["word1", "word2", "word3"];
string[] B = ["word0", "word1", "word2", "word3", "word4", "word5", "word6", "word1", "word2", "word3"];
If I want to compare A to B and remove the first occurrence in B so it looks like this...
string[] B = ["word0", " ", " ", " ", "word4", "word5", "word6", "word1", "word2", "word3"];
How would I go about this?
A straightforward way would be to use Array.IndexOf to find the first occurrence of each word from A in B:
foreach (var word in A)
{
var index = Array.IndexOf(B, word);
if (index >= 0) {
B[index] = " "; // or whatever other value
}
}
Note this might not work as expected if the replacement value is itself present inside A -- if that is possible you should specify what you want to happen.
Update: It looks like you want to find and replace the subsequence A as a whole inside B, and not individual elements. This is a very different problem. One (naive) implementation would be:
var start = Enumerable.Range(0, B.Length - A.Length + 1)
.Where(i => B.Skip(i).Take(A.Length).SequenceEqual(A))
.DefaultIfEmpty(-1)
.First();
if (start != -1)
{
for (var i = 0; i < A.Length; ++i)
{
B[start + i] = " ";
}
}
I fully support the answer #Jon gave. It is quite fast and succinct and precise.
Still I had a totally different approach, a more functional one, just in case
by string[] you actually wanted to say something more streamy:
Say you have a possibly infinite sequence of strings instead of a primitive array in the role of B. It could be anything: A reading of entities coming straight from a database, a monadic string generator, anything:
string[] A = ["word1", "word2", "word3"];
IEnumerable[] B = ...;
You could write yourself a nice little extension method:
public static class MyHelpers {
public static IEnumerable<string> ReplaceFirstOccurrencesWithEmpty(this IEnumerable<string> #this, IEnumerable<string> a) {
// prepare a HashSet<string> to know how many A elements there still exist
var set = new Hashset<string>(a);
// iterate and apply the rule you asked about
// virtually forever (if needed)
foreach (var value in #this) {
if (set.Remove(value))
yield return "";
else
yield return value;
}
}
}
And then you could use it like so, even on your initial A and B arrays:
string[] A = ["word1", "word2", "word3"];
string[] B = ["word0", "word1", "word2", "word3", "word4", "word5", "word6", "word1", "word2", "word3"];
var cQuery = B.ReplaceFirstOccurrencesWithEmpty(A);
string[] c = cQuery.ToArray();
With LINQ
string[] A = new string[] { "word1", "word2", "word3" };
string[] B = new string[] { "word0", "word1", "word2", "word3", "word4", "word5", "word6", "word1", "word2", "word3" };
string[] result = B.Select((word, i) => i <= A.Length && i > 0 && A[i-1] == word ? "" : word).ToArray();
i am listbox to store different strings which user gives as input.
but i want to split those listbox items where i want to have the first word of every item as seperate string and rest as other string.
i am iterating the listbox item as
foreach (ListItem item in lstboxColumnList.Items)
{
column_name = temp + "\" "+item+"\"";
temp = column_name + "," + Environment.NewLine;
}
how could i get the splitted string
Assuming firs word ends with a space, you can use something like below:
string firsWord = sentence.SubString(0, sentence.IndexOf(' '));
string remainingSentence = sentence.SubString(sentence.IndexOf(' '), sentence.Length);
I dont know your listbox item's format..
but I assumed that your listbox item have at least 2 word and separate by a space..
so, you can do the splitting using substring and index of..
string first = sentence.SubString(0, sentence.IndexOf(" "));
string second = sentence.SubString(sentence.IndexOf(" ") + 1);
public void Test()
{
List<string> source = new List<string> {
"key1 some data",
"key2 some more data",
"key3 yada..."};
Dictionary<string, string> resultDictionary = source.ToDictionary(n => n.Split(' ').First, n => n.Substring(n.IndexOf(' ')));
List<string> resultStrings = source.Select(n => string.Format("\"{0}\",{1}", n.Split(' ').First, n.Substring(n.IndexOf(' ')))).ToList;
}
resultDictionary is a dictionary with the key set to the first word of each string in the source list.
The second closer matches the requirements in your question that it outputs a list of strings in the format you specified.
EDIT: Apologies, posted in VB first time round.
checkout:
var parts = lstboxColumnList.Items.OfType<ListItem>().Select(i => new {
Part1 = i.Text.Split(' ').FirstOrDefault(),
Part2 = i.Text.Substring(i.Text.IndexOf(' '))
});
foreach (var part in parts)
{
var p1 = part.Part1;
var p2 = part.Part2;
// TODO: use p1, p2 in magic code!!
}