Combining arrays of strings together - c#

I'm looking to combine the contents of two string arrays, into a new list that has the contents of both joined together.
string[] days = { "Mon", "Tue", "Wed" };
string[] months = { "Jan", "Feb", "Mar" };
// I want the output to be a list with the contents
// "Mon Jan", "Mon Feb", "Mon Mar", "Tue Jan", "Tue Feb" etc...
How can I do it ? For when it's only two arrays, the following works and is easy enough:
List<string> CombineWords(string[] wordsOne, string[] wordsTwo)
{
var combinedWords = new List<string>();
foreach (var wordOne in wordsOne)
{
foreach (string wordTwo in wordsTwo)
{
combinedWords.Add(wordOne + " " + wordTwo);
}
}
return combinedWords;
}
But I'd like to be able to pass varying numbers of arrays in (i.e. to have a method with the signature below) and have it still work.
List<string> CombineWords(params string[][] arraysOfWords)
{
// what needs to go here ?
}
Or some other solution would be great. If it's possible to do this simply with Linq, even better!

What you want to do is actually a cartesian product of all the arrays of words, then join the words with spaces. Eric Lippert has a simple implementation of a Linq cartesian product here. You can use it to implement CombineWords:
List<string> CombineWords(params string[][] arraysOfWords)
{
return CartesianProduct(arraysOfWords)
.Select(x => string.Join(" ", x))
.ToList();
}

To cross join on any amount of arrays of strings:
// Define other methods and classes here
List<string> CombineWords(params string[][] arraysOfWords)
{
if (arraysOfWords.Length == 0)
return new List<string>();
IEnumerable<string> result = arraysOfWords[0];
foreach( string[] words in arraysOfWords.Skip(1) )
{
var tempWords = words;
result = from r in result
from w in tempWords
select string.Concat(r, " ", w);
}
return result.ToList();
}

Code below works for any number of arrays (and uses linq to some degree):
List<string> CombineWords(params string[][] wordsToCombine)
{
if (wordsToCombine.Length == 0)
return new List<string>();
IEnumerable<string> combinedWords = wordsToCombine[0].ToList();
for (int i = 1; i < wordsToCombine.Length; ++i)
{
var temp = i;
combinedWords = (from x in combinedWords from y in wordsToCombine[temp]
select x + " " + y);
}
return combinedWords.ToList();
}

public static List<string> CombineWords(params string[][] arraysOfWords)
{
var strings = new List<string>();
if (arraysOfWords.Length == 0)
{
return strings;
}
Action<string, int> combineWordsInternal = null;
combineWordsInternal = (baseString, index) =>
{
foreach (var str in arraysOfWords[index])
{
string str2 = baseString + " " + str;
if (index + 1 < arraysOfWords.Length)
{
combineWordsInternal(str2, index + 1);
}
else
{
strings.Add(str2);
}
}
};
combineWordsInternal(string.Empty, 0);
return strings;
}
Second try... I'm not able to do it in LINQ... A little too much complex to linquize correctly :-)
I'm using a local anonymous function (and showing that it's quite complex to recurse on anonymous functions, because you have to declare them separately)

This is a non-recursive solution which buffers strings as it progresses, to reduce the number of concatenations. Therefore it should also be usable for more arrays.
It also preserves your desired order - the items in the first array will always be at the beginning of the resulting string.
var s1 = new string[] { "A", "B", "C" };
var s2 = new string[] { "1", "2", "3", "4" };
var s3 = new string[] { "-", "!", "?" };
var res = Combine(s1, s2, s3);
And the function in question:
private List<string> Combine(params string[][] arrays)
{
if (arrays.Length == 1)
{
// The trivial case - exit.
return new List<string>(arrays[0]);
}
IEnumerable<string> last = arrays[arrays.Length - 1];
// Build from the last array, progress forward
for (int i = arrays.Length - 2; i >= 0; i--)
{
var buffer = new List<string>();
var current = arrays[i];
foreach (var head in current)
{
foreach (var tail in last)
{
// Concatenate with the desired space.
buffer.Add(head + " " + tail);
}
}
last = buffer;
}
return (List<string>)last;
}

Could you try this method ?
static List<string> CombineWords(string[] wordsOne, string[] wordsTwo)
{
var combinedWords = new List<string>();
for(int x = 0; (x <= wordsOne.Length - 1); ++x)
{
for(int y = 0; (x <= wordsTwo.Length - 1); ++y)
{
combinedWords.Add(string.Format("{0} {1}", wordsOne[x], wordsTwo[y]));
}
}
return combinedWords;
}
Kris

Related

find the two longest word made of other words

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 .

Selecting alternate items of an array C#

I have an array statsname as
apple
X
banana
Y
Kiwi
z
I need to put apple,banana and Kiwi in an array Fruits and X,Y and Z in an array called alphabets.
Any simple C# mechanism for it please ?
Use the IEnumerable<T>.Where overload which supplies the index.
var fruits = statsname.Where((s, i) => i % 2 == 0).ToArray();
var alphabets = statsname.Where((s, i) => i % 2 != 0).ToArray();
Stolen from How to get Alternate elements using Enumerable in C#
var fruits = myArray.Where((t, i) => i % 2 == 0).ToArray();
var alphabets = myArray.Where((t, i) => i % 2 == 1).ToArray();
If i have understood you question correctly what you want is very simple:
You want put fruits in array of fruits and same for alphabets and they are appearing alternatively in array statsname so:
for(int i=0,j=0;i<statsname.length;i+2,j++)
fruits[j]=statsname[i];
for(int i=1,j=0;i<statsname.length;i+2,j++)
alphabets[j]=statsname[i];
Single LINQ:
List<string> list = new List<string>() { "apple", "X", "banana", "Y", "Kiwi", "z" };
var result = list.Select((l, i) => new { l, i })
.GroupBy(p => p.i % 2)
.Select(x => x.Select(v => v.l).ToList())
.ToList();
Then you have a list of lists:
list<string> fruits = new List<string>();
list<string> alphabet = new List<string>();
for (int i = 0; i < array.Length; i++)
{
if (i % 2 == 0)
fruits.Add(array[i]);
else
alphabet.Add(array[i]);
}
Then you can just do .ToArray on the lists
string[] rawarray = new string [] {"Apple","X" .....};
string[] Fruites = new string[rawarray.Length/2+1];
string[] Alphabets = new string[rawarray.Length/2];
For(int i=0; i<rawarray.Length;i++)
{
if(i%2==0)
{
Fruits[i/2]=rawarray[i];
}
else
{
Alphabets[i/2]=rawarray[i];
}
}
using only Arrays:
var array = new string[] { "apple", "X", "banana", "Y", "Kiwi", "z" };
var fruit = new string[array.Length];
var alphabet = new string[array.Length];
for(var i = 0,j = 0; i < array.Length / 2; i++, j += 2)
{
fruit[i] = array[j];
alphabet[i] = array[j + 1];
}
You could make an iterator which just skips every other element. The idea is to have a "view" of a collection, special enumerable which will return only some of the elements:
static IEnumerable<T> everyOther<T>( IEnumerable<T> collection )
{
using( var e = collection.GetEnumerator() ) {
while( e.MoveNext() ) {
yield return e.Current;
e.MoveNext(); //skip one
}
}
}
You can use System.Linq.Skip to skip the first element.
string[] words = "apple X banana Y Kiwi z".Split();
var fruits = everyOther( words );
var alphabets = everyOther( words.Skip(1) );
Just use them as a new collection or call .ToArray() or .ToList() on them:
foreach( string f in fruits )
Console.WriteLine( f );
string[] anArray = fruits.ToArray(); //using System.Linq
Now you have what you need.
Iterators are methods which yield return, see Iterators (C# Programming Guide). This is very strong feature of the language. You can:
skip elements
decorate elements
change ordering
concatenate sequences (see System.Linq.Concat)
...
Here is some working code, hopefully this will be helpfull to you:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadFile
{
class Program
{
static void ReadFile(string filePath, List<string> custumerNames, List<int> phoneNumbers)
{
string line = string.Empty;
var fileStream = new StreamReader(filePath);
bool isPhoneNumber = true;
while ((line = fileStream.ReadLine()) != null)
{
if (isPhoneNumber)
{
phoneNumbers.Add(Convert.ToInt32(line));
isPhoneNumber = false;
}
else
{
custumerNames.Add(line);
isPhoneNumber = true;
}
}
fileStream.Close();
}
static void Main(string[] args)
{
Console.WriteLine("Started reading the file...");
List<string> custumersNamesList = new List<string>();
List<int> custumersPhonesNumbers = new List<int>();
ReadFile("SampleFile.txt", custumersNamesList, custumersPhonesNumbers);
//Assuming both the list's has same lenght.
for (int i = 0; i < custumersNamesList.Count(); i++)
{
Console.WriteLine(string.Format("Custumer Name: {0} , Custumer Phone Number: {1}",
custumersNamesList[i], Convert.ToString(custumersPhonesNumbers[i])));
}
Console.ReadLine();
}
}
}

using foreach loop for two lists in c# for geting lists values

List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
for(int i = 0; i < listA.Count; i++)
{
text += listA[i] + " and " + listB[i];
}
How can I do this using a foreach loop ?
You can use Linq and the Zip method:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
foreach (var pair in listA.Zip(listB, (a,b) => new {A = a, B = b}))
{
text += pair.A + " and " + pair.B;
}
You cannot do this with a foreach any better than you are already doing it with for -- foreach is really only designed to work well when there is only one sequence to enumerate.
However, you can do it very conveniently with LINQ:
text = string.Join("", listA.Zip(listB, (a, b) => a + " and " + b));
This requires .NET 4 both for Zip and for the specific overload of string.Join.
Another way of doing this is doing that with simple Enumerator:
IEnumerator<string> enumerator = listB.GetEnumerator();
enumerator.MoveNext();
foreach(var stra in listA) {
text += stra + " and " + enumerator.Current.ToString() + ", ";
enumerator.MoveNext();
}
Use LINQ
string text = listA.Zip(listB, (a, b) => new {A = a, B = b}).Aggregate("", (current, pair) => current + (pair.A + " and " + pair.B));
And if you do not want to use LINQ and you want them to iterate parallel you have few options - with new classes etc. like below OR you can use foreach, but only for one of the lists, like this:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
string text = "";
int i = 0;
foreach (string s in listA) {
text += s + " and " + listB [i++] + "\n";
}
Console.WriteLine (text);
or make it a bit nicer using GetEnumerator:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
string text = "";
List<string>.Enumerator e = listB.GetEnumerator ();
foreach (string s in listA) {
e.MoveNext ();
text += s + " and " + e.Current + "\n";
}
Console.WriteLine (text);
also you can create yourself an Enumberable metacollection which will return out of that always a simple string array - for that you will need to create an Enumerator and a class which is derieves from IEnumerable:
First the Enumerator:
private class DoubleStringEnumerator : IEnumerator
{
private DoubleString _elemList;
private int _index;
public DoubleStringEnumerator(DoubleString doubleStringObjt)
{
_elemList = doubleStringObjt;
_index = -1;
}
public void Reset()
{
_index = -1;
}
public object Current {
get {
return _elemList.getNext();
}
}
public bool MoveNext ()
{
_index++;
if (_index >= _elemList.Length)
return false;
else
return true;
}
}
The Current method does not really reflects it's name in the given example - but it is for learning purposes.
Now the class:
public class DoubleString : IEnumerable
{
public int Length;
List<String> listA;
List<String> listB;
List<string>.Enumerator eA,eB;
public DoubleString(List<String> newA,List<String> newB)
{
if(newA.Count != newB.Count) {
throw new Exception("Lists lengths must be the same");
}
listA = newA;
listB = newB;
eA = listA.GetEnumerator ();
eB = listB.GetEnumerator ();
Length = listA.Count;
}
IEnumerator IEnumerable.GetEnumerator ()
{
return (IEnumerator)new DoubleStringEnumerator(this);
}
public string[] getNext ()
{
eA.MoveNext ();
eB.MoveNext ();
return new string[] {eA.Current ,eB.Current };
}
}
And the code itself:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
DoubleString newDoubleString = new DoubleString (listA, listB);
string text = "";
foreach (string[] s in newDoubleString) {
text += s[0] + " and " + s[1] + "\n";
}
Console.WriteLine (text);
Of course still better to use LINQ. The code is not optimsied, but I had no compiler with me so writting from my head - hope it will clarify few things. Feel free to ask questions.
List<int> = new [] { 1, 2, 3, 4 };
List<String> words = new [] { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (n, w) => new { Number = n, Word = w });
foreach(var nw in numbersAndWords)
{
Console.WriteLine(nw.Number + nw.Word);
}
Here is the solution using foreach:
string text = null;
int cnt = 0;
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
foreach (string i in listA)
{
text += listA[cnt] + " and " + listB[cnt];
cnt++;
}
Thanks & Regards,
Subhankar

c# array help on sorting

First of all sorry for my mistakes in English its not my primary language
i have a problem , i have a array like following
string[] arr1 = new string[] {
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"India:4,USA:3,Iran:2,UK:1,Pakistan:0"
};
now i just want to know that how many times Pakistan comes with 1 ,
how many times with 2 , 3 , 4
and i need to know this about all India , USA , Iran , UK
Thanks in advance , you guys are my last hope .
This linq will convert the array into a Dictionary>, where the outer dictionary contains the countries names, and inner dictionaries will contain the ocurrence number (the number after ':') and the count for each ocurrence.
string[] arr1 = new string[]
{
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"India:4,USA:3,Iran:2,UK:1,Pakistan:0"
};
var count = arr1
.SelectMany(s => s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(s => s.Split(':')[0], s => s.Split(':')[1])
.ToDictionary(g => g.Key,
g =>
{
var items = g.Distinct();
var result = new Dictionary<String, int>();
foreach (var item in items)
result[item] = g.Count(gitem => gitem == item);
return result;
});
// print the result
foreach(var country in count.Keys)
{
foreach(var ocurrence in count[country].Keys)
{
Console.WriteLine("{0} : {1} = {2}", country, ocurrence, count[country][ocurrence]);
}
}
I would use the String.Split(char[]) method and the String.SubString(int, int) method to inspect every 'country' inside your array and to get the number postfix of each country.
Try the following:
(The following code is now compiled and tested.)
Use a simple data structure to facilitate the task of holding the result of your operation.
public struct Result {
string Country { get; set; }
int Number { get; set; }
int Occurrences { get; set; }
}
// define what countries you are dealing with
string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", }
Method to provide the overall result:
public static Result[] IterateOverAllCountries () {
// range of numbers forming the postfix of your country strings
int numbersToLookFor = 4;
// provide an array that stores all the local results
// numbersToLookFor + 1 to respect that numbers are starting with 0
Result[] result = new Result[countries.Length * (numbersToLookFor + 1)];
string currentCountry;
int c = 0;
// iterate over all countries
for (int i = 0; i < countries.Length; i++) {
currentCountry = countries[i];
int j = 0;
// do that for every number beginning with 0
// (according to your question)
int localResult;
while (j <= numbersToLookFor) {
localResult = FindCountryPosition(currentCountry, j);
// add another result to the array of all results
result[c] = new Result() { Country = currentCountry, Number = j, Occurrences = localResult };
j++;
c++;
}
}
return result;
}
Method to provide a local result:
// iterate over the whole array and search the
// occurrences of one particular country with one postfix number
public static int FindCountryPosition (string country, int number) {
int result = 0;
string[] subArray;
for (int i = 0; i < arr1.Length; i++) {
subArray = arr1[i].Split(',');
string current;
for (int j = 0; j < subArray.Length; j++) {
current = subArray[j];
if (
current.Equals(country + ":" + number) &&
current.Substring(current.Length - 1, 1).Equals(number + "")
)
result++;
}
}
return result;
}
The following should enable you to run the algorithm
// define what countries you are dealing with
static string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", };
static string[] arr1 = new string[] {
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"Pakistan:4,India:3,USA:2,Iran:1,UK:0",
"India:4,USA:3,Iran:2,UK:1,Pakistan:0"
};
static void Main (string[] args) {
Result[] r = IterateOverAllCountries();
}
The data structure you are using is not rich enough to provide you with that information. Hence you need to parse your string and create a new data structure to be able to provide (sring[][]):
string[] arr1 = new string[] {
"Pakistan,India,USA,Iran,UK",
"Pakistan,India,USA,Iran,UK",
"India,USA,Iran,UK,Pakistan"
};
string[][] richerArray = arr1.Select(x=> x.Split('\'')).ToArray();
var countPakistanIsFirst = richerArray.Select(x=>x[0] == "Pakistan").Count();
UPDATE
You seem to have changed your question. The answer applies to the original question.

Using more than one object to iterate though in foreach loops

I have a multidimensional list:
List<string>[] list = new List<string>[2];
list[0] = new List<string>();
list[1] = new List<string>();
And I iterate though the list as follows - but this only iterates through one list:
foreach (String str in dbConnectObject.Select()[0])
{
Console.WriteLine(str);
}
Although I want to be able to do something like:
foreach (String str in dbConnectObject.Select()[0] & String str2 in dbConnectObject.Select()[1])
{
Console.WriteLine(str + str2);
}
If the lists are of the same size, you can use Enumerable.Zip:
foreach (var p in dbConnectObject.Select()[0].Zip(dbConnectObject.Select()[1], (a,b) => new {First = a, Second = b})) {
Console.Writeline("{0} {1}", p.First, p.Second);
}
If you want to sequentially iterate through the two lists, you can use IEnumerable<T>.Union(IEnumerable<T>) extension method :
IEnumerable<string> union = list[0].Union(list[1]);
foreach(string str int union)
{
DoSomething(str);
}
If you want a matrix of combination, you can join the lists :
var joined = from str1 in list[0]
from str2 in list[1]
select new { str1, str2};
foreach(var combination in joined)
{
//DoSomethingWith(combination.str1, combination.str2);
Console.WriteLine(str + str2);
}
You can try the following code. It also works if the sizes differ.
for (int i = 0; i < list[0].Count; i++)
{
var str0 = list[0][i];
Console.WriteLine(str0);
if (list[1].Count > i)
{
var str1 = list[1][i];
Console.WriteLine(str1);
}
}
Use LINQ instead:
foreach (var s in list.SelectMany(l => l))
{
Console.WriteLine(s);
}

Categories

Resources