0

Can anyone please help me with this?

It should be a ConsoleApp which asks for the first and the last name. From the all name, it should generate 4 characters. I have to provide different possible combinations of the strings (e.g. 1. and 3. char of fn and 2. 3. char of ln ...) The Algorithm should be capable of providing all possible combinations of 4 characters where 2 are from the first name and 2 of the last name.

Till now I did only the combinations of 2 for the first name and the last name.

static void Main(string[] args) 
{ 
   Console.WriteLine("Enter your first name");
   string firstName = Console.ReadLine(); 

   Console.WriteLine("Enter your last name"); 
   string lastName = Console.ReadLine();

        string[] result = GetAllCombinations(firstName);
        string[] code = GetAllCombinations(lastName);

        PrintTheCombinations(result);
        PrintTheCombinations(code);


    }
    private static void PrintTheCombinations(string[] list)
    {
        foreach (var results in list)
        {
            Console.WriteLine(results);
        }
    }
    private static string[] GetAllCombinations(string word)
    {
        int arraylength = word.Length * word.Length;
        var ret = new string[arraylength];

        for (int i = 0; i < word.Length; i++)
        {
            for (int j = 0; j < word.Length; j++)
            {
                ret[i * word.Length + j] = string.Concat(word[i], word[j]);
            }
        }
             return ret;
    }

Now I need to print the 4 characters, 2 of the fn and 2 of the ln, but I'm stuck. Hope you guys understand what i mean

Diado
  • 2,229
  • 3
  • 18
  • 21
M16
  • 1
  • 4
  • 1
    You should be able to use almost exactly the same logic in a new method that takes 2 `string[]`. Just now the length of the result will be the length of array 1 * the length of array 2. And the `i` will loop over the length of array 1 and the `j` over the length of array 2. –  Apr 21 '20 at 07:29
  • 1
    Does order of characters matter? Are multiple uses of same character, like `word[1]` and `word[1]` permitted? – Kacper Apr 21 '20 at 07:49
  • Can you give me some example of input and output? – Hoàng Minh Thông Apr 21 '20 at 08:21
  • Well, yes, i'd like to be without duplicates :) – M16 Apr 21 '20 at 08:24
  • @HoàngMinhThông it should be input: Simon Smith and the outputs: SISM, SIMI, SIIT, SITH, IMSM... and so on till it gets all the combinations – M16 Apr 21 '20 at 08:28
  • The characters of each string are fixed? example the first name is always the 1st and 3nd char or is variable? – Douglas Ferreira Apr 21 '20 at 08:32
  • 1
    Does this answer your question? [Listing all permutations of a string/integer](https://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer) This [How to get all the possible 3 letter permutations?](https://stackoverflow.com/questions/13891200/how-to-get-all-the-possible-3-letter-permutations) might be helpful too – Pavel Anikhouski Apr 21 '20 at 08:32
  • @DouglasFerreira no, it's not fixed. I gave an example up how it should look the outputs. – M16 Apr 21 '20 at 08:34

2 Answers2

0

it can be done in such way

static void Main(string[] args)
{
    //Console.WriteLine("Enter your first name");
    //string firstName = Console.ReadLine().ToUpper();

    //Console.WriteLine("Enter your last name");
    //string lastName = Console.ReadLine().ToUpper();
    string firstName = "abc".ToUpper();
    string lastName = "123".ToUpper();
    var first = new HashSet<string>();
    var second = new HashSet<string>();
    foreach (var permutation in Permutations(firstName))
    {
        var value = $"{permutation[0]}{permutation[1]}";
        if (!first.Contains(value))
        {
            first.Add(value);
        }
    }
    foreach (var permutation in Permutations(lastName))
    {
        var value = $"{permutation[0]}{permutation[1]}";
        if (!second.Contains(value))
        {
            second.Add(value);
        }
    }
    var result = new HashSet<string>();

    foreach (var begin in first)
    {
        foreach (var end in second)
        {
            var value = $"{begin}{end}";
            if (!result.Contains(value))
            {
                result.Add(value);
                Console.WriteLine(value);
            }
        }
    }
}
static IEnumerable<string> Permutations(string word)
{
    if (word == null || word.Length <= 1)
    {
        yield return word;
        yield break;
    }

    char firstChar = word[0];
    foreach (string subPermute in Permutations(word.Substring(1)))
    {
        int indexOfFirstChar = subPermute.IndexOf(firstChar);
        if (indexOfFirstChar == -1) indexOfFirstChar = subPermute.Length;

        for (int index = 0; index <= indexOfFirstChar; index++)
            yield return subPermute.Insert(index, new string(firstChar, 1));
    }
}
Radik
  • 2,715
  • 1
  • 19
  • 24
  • Your answer relies on a link to another answer. You [should](https://stackoverflow.com/help/how-to-answer) make sure your answer is still useful in case link becomes broken. – Kacper Apr 21 '20 at 08:51
0

Following code will print all existing combinations of two letters from from firstName and firstName. It assumes that order of letters matters and letter may repeat only if they are used multipla times in first and/or last name.

using System;

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter your first name");
        string firstName = Console.ReadLine();

        Console.WriteLine("Enter your last name");
        string lastName = Console.ReadLine();
        Console.WriteLine("All combinations:");
        PrintAllCombinations(firstName, lastName);

        Console.ReadLine();
    }

    private static void PrintAllCombinations(string word1, string word2)
    {
        for (int i = 0; i < word1.Length; i++)
        {
            for (int j = i + 1; j < word1.Length; j++)
            {
                for (int k = 0; k < word2.Length; k++)
                {
                    for (int l = k + 1; l < word2.Length; l++)
                    {
                        char[] chars = new char[] { word1[i], word1[j], word2[k], word2[l] };
                        //create all permutations of this collection
                        for (int ii = 0; ii < 4; ii++)
                        {
                            for (int jj = 0; jj < 4; jj++)
                            {
                                if (ii == jj) continue;
                                for (int kk = 0; kk < 4; kk++)
                                {
                                    if (ii == kk || jj == kk) continue;
                                    for (int ll = 0; ll < 4; ll++)
                                    {
                                        if (ii == ll || jj == ll || kk == ll) continue;
                                        Console.Write(chars[ii]);
                                        Console.Write(chars[jj]);
                                        Console.Write(chars[kk]);
                                        Console.Write(chars[ll]);
                                        Console.WriteLine("");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
Kacper
  • 520
  • 5
  • 20
  • @M16 I'm glad to hear that. If my answer solves your problem you can accept it as an answer to your question ;) – Kacper Apr 21 '20 at 10:12