List<string> list = new List<string>();
list.Add("A");
list.Add("B");
List<string> list1 = new List<string>();
list.Add("a");
list.Add("b");
for (int i = 0; i < list.Count; i++)
{
// print another list items.
for (int j = 0; j < list1.Count; j++)
{
Console.WriteLine("/" + list[i] + "/" + list1[j]);
}
}
I want to code like this string tmpS =+ list[i];
to Join the next list item togeter.
then print tmpS
but compile error CS0023: Operator '+' cannot be applied to operand of type 'string'.
How to print all the items below.(any sort is ok)
A Aa Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba
(The Caps number No swap. the small characters should be swaped. and always follow Caps Numbers Append small characters.)
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
Using the * symbol to print a list in Python. To print the contents of a list in a single line with space, * or splat operator is one way to go. It passes all of the contents of a list to a function. We can print all elements in new lines or separated by space and to do that, we use sep=”\n” or sep=”, ” respectively.
Method 1:Using Set's & property Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: Python3.
it makes a long time I did not worked on a pure algorithmic problem!
This program should do the trick:
class Program
{
static void Main(string[] args)
{
List<string> uppers = new List<string>();
uppers.Add("A");
uppers.Add("B");
List<string> lowers = new List<string>();
lowers.Add("a");
lowers.Add("b");
List<string> combinedUppers = GetCombinedItems(uppers);
List<string> combinedLowers = GetCombinedItems(lowers);
List<string> combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers);
foreach (string combo in combinedUppersLowers)
{
Console.WriteLine(combo);
}
Console.Read();
}
static private List<string> GetCombinedItems(List<string> list)
{
List<string> combinedItems = new List<string>();
for (int i = 0; i < list.Count; i++)
{
combinedItems.Add(list[i]);
for (int j = 0; j < list.Count; j++)
{
if (list[i] != list[j])
{
combinedItems.Add(String.Format("{0}{1}", list[i], list[j]));
}
}
}
return combinedItems;
}
static private List<string> GetCombinedList(List<string> list1, List<string> list2)
{
List<string> combinedList = new List<string>();
for (int i = 0; i < list1.Count; i++)
{
combinedList.Add(list1[i]);
for (int j = 0; j < list2.Count; j++)
{
combinedList.Add(String.Format("{0}{1}", list1[i], list2[j]));
}
}
for (int i = 0; i < list2.Count; i++)
{
combinedList.Add(list2[i]);
for (int j = 0; j < list1.Count; j++)
{
combinedList.Add(String.Format("{0}{1}", list2[i], list1[j]));
}
}
return combinedList;
}
}
Regards.
This program gives you this output:
A Aa Aab Ab Aba AB ABa ABab ABb ABba B Ba Bab Bb Bba BA BAa BAab BAb BAba a aA aAB aB aBA ab abA abAB abB abBA b bA bAB bB bBA ba baA baAB baB baBA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With