Denis Jakus

making tech less cryptic

CTO x Unique People

Denis Jakus

making tech less cryptic

CTO x Unique People

CTO Note

INTERVIEW JOB CODING TASKS – Calculate permutations1 min read

19 February 2020 .NET
INTERVIEW JOB CODING TASKS – Calculate permutations1 min read

Today we are going to continue with interview coding tasks.

I really do hope this, solved, coding task is going to help you during your interview.

Task: Depending on input value i.e. (123) create all permutations i.e:(123,213,231,132,312,321)

Solution:


private static void Main(string[] args)
{
	WritePermutations();
        Console.ReadLine();
}
public static void WritePermutations()
{
	var permutations = GetPermutations("123");

	foreach (var permutation in permutations)
	{
		Console.WriteLine(permutation);
	}
}
public static List<string> GetPermutations(string inputString)
{
	return IsPermutated(inputString, out var permutations, out var list) ? list : ListOfPermutations(inputString, permutations);
}
private static bool IsPermutated(string inputString, out List<string> permutations, out List<string> list)
{
            permutations = new List<string>();
            list = null;

            if (inputString == null) return true;
            if (inputString.Length != 0) return false;
            
            permutations.Add("");
            {
                list = permutations;
                return true;
            }
}

private static List<string> ListOfPermutations(string inputString, List<string> permutations)
{
	var firstCharacter = inputString[0];
	var remainder = inputString.Substring(1);
	var words = GetPermutations(remainder);
	foreach (var word in words)
	{
		for (var i = 0; i <= word.Length; i++)
		{
			permutations.Add(InsertCharacterAt(word, firstCharacter, i));
		}
	}

	return permutations;
}
public static string InsertCharacterAt(string word, char character, int i)
{
  var start = word.Substring(0, i);
  var end = word.Substring(i);
   return $"{start}{character}{end}";
}

Cheers

Taggs:
Related Posts
INTERVIEW JOB CODING TASKS – XML file manipulation

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like It’s been a while, once…

INTERVIEW JOB CODING TASKS – Calculate Tree depth and a number of leaves

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like Season 1 – Prologue So……

4 Comments
  • Brianmuh 22:08 24 March 2020 Reply

    Great looking internet site. Presume you did a lot of your very own html coding.

  • Brianmuh 11:19 04 April 2020 Reply

    Sustain the excellent work and delivering in the crowd!

  • Jamesjus 06:20 24 April 2020 Reply

    Keep up the excellent work and generating the group!

  • 파라오카지노 12:35 16 October 2020 Reply

    Hello There. I found your weblog the use of msn. This is a very smartly
    written article. I will make sure to bookmark it and come back to learn more of
    your helpful info. Thanks for the post. I will definitely return.

Write a comment