0

i am making an app as my own project on c#. its called Time Table Maker. the last piece of code i need to finish requires the following task to be done

We have an array of size N, where N is the TOTAL number of courses (Theory + Lab)

It is calculated by 2 variables that user gives.

We have Theory Count and Lab Count Separately

N = Theory Count + Lab Count

Now suppose N = 5;

Write a function which takes input 2 things.

1.) The Array To Permute.

2.) The char To Permute Till. Suppose (I) (eye) :v.

And it returns another Array which is permuted.

string[] PermuteOnce(string[] str, string PermuteTill)
    {
        string[] Permuted = new string[TheoryN];
        Permuted = str;
        // CODE HERE
        return Permuted;
    }

Sample Input Output;

String[5] ToPermute = {A,A,A,A,A}

Permuted = PermuteOnce(ToPermute, “I”);

Permuted now contains {A,A,A,A,B}

Calling it again and again now passing Permuted.

Permuted now contains {A,A,A,A,C}

Permuted now contains {A,A,A,A,D}

Permuted now contains {A,A,A,A,E}

Permuted now contains {A,A,A,A,F}

Permuted now contains {A,A,A,A,G}

Permuted now contains {A,A,A,A,H}

Permuted now contains {A,A,A,A,I}

Now…

Permuted now contains {A,A,A,B,A}

Permuted now contains {A,A,A,C,A}

Like this “I” is the limit to permute till.

A little more explanation….

This is what i did up till now... but does it does not work perfect...

str = PermuteOnce(str, "I");

 int Shifter = 4;
    int Count = 1;
    string ChangeTo(int C)
    {
        switch(C)
        {
            case 1:
                {
                    return "A";
                }
            case 2:
                {
                    return "B";
                }
            case 3:
                {
                    return "C";
                }
            case 4:
                {
                    return "D";
                }
            case 5:
                {
                    return "E";
                }
            case 6:
                {
                    return "F";
                }
            case 7:
                {
                    return "G";
                }
            case 8:
                {
                    return "H";
                }
            case 9:
                {
                    return "I";
                }
            default:
                return ".";
        }
    }


string[] PermuteOnce(string[] str, string PermuteTill)
    {
        string[] Permuted = new string[TheoryN];
        Permuted = str;
        char x = Convert.ToChar( Permuted[Shifter] );
        int Ascii = Convert.ToInt32(x);
        Ascii++;
        Permuted[Shifter] = Convert.ToString(Convert.ToChar(Ascii));
        if (Permuted[Shifter] == "J")
        {
            Permuted[Shifter] = ChangeTo(Count);
            if (Shifter > 0)
            Permuted[Shifter - 1] = ChangeTo(Count);
            Shifter--;
            if (Shifter == -1 && Count != 9)
            {
                Shifter = 4;
                Count++;
            }
        }
        return Permuted;
    }

Now, how will i know if my requirement is met? when one of the permutation is "AGABB". and it start from "AAAAA" and ends on "IIIII"

this is my function in which i need permutations ..

private void HRain_Click(object sender, EventArgs e)
    {
        // Set All Sections To A In The Start.
        for (int i = 0; i < TheoryN; i++) { Sections[i].Text = Convert.ToString(Convert.ToChar(65)); } 
        // Clear Display Screen / Allocate Query And str for processing.
        Result_Screen_L.Clear();
        Query = new string[TotalCourses];
        string[] str = new string[TheoryN];

        // Loop Should Run Untill All Permutations Are Printed. e.g (From 'AAAAA' , 'IIIII' is attained.)
        while (!(Sections[0].Text == "I" && Sections[1].Text == "I" && Sections[2].Text == "I" && Sections[3].Text == "I" && Sections[4].Text == "I"))
        {
            for (int i = 0; i < TheoryN; i++) { str[i] = Sections[i].Text.ToString(); } // move sections to str.
            MatchWithLabs(); // magic logic in this.
            for (int i = 0; i < TotalCourses; i++) { Query[i] = CourseName[i].Text + " (" + Sections[i].Text + ")"; } // set query to calculate timetable.
            //Result = new string[MaxItems * MaxItems]; // to store result in. 7*7.
            //CalculateTimeTable(); // magic here too.
            //if (CheckForClash(Result)) DisplayResultScreen(); // if no clash then print on screen <3.
            str = PermuteOnce(str, "I"); // your magic function.
            //rev = new Revision("ABCDEFGHI", "AAAAA");
            for (int i = 0; i < TheoryN; i++) { Sections[i].Text = Convert.ToString(str[i]); } // move permuted str back to Sections.
        }
    }
5D_cOOKIE
  • 13
  • 1
  • 7
  • https://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently Look at this for a permutation example – Sean Stayns Feb 24 '19 at 19:56
  • Assigning `Permuted` twice in a row doesn't make sense. Why not just `string[] Permuted = str;` – Rufus L Feb 24 '19 at 20:06
  • What is the `PermuteTill` argument supposed to be used for? It's not used anywhere in your method. – Rufus L Feb 24 '19 at 20:11
  • If you want to treat `str` as an array of characters (i.e. you do `Convert.ToChar(Permuted[Shifter]);`), then you should define it as a `char[]` (or just a `string`, which is a character array). – Rufus L Feb 24 '19 at 20:14
  • @SeanStayn looking at it. i ran a code from that part but its not working correct either.. – 5D_cOOKIE Feb 24 '19 at 20:14
  • @RufusL yea it should be used in the place where im using "J" – 5D_cOOKIE Feb 24 '19 at 20:15
  • @RufusL yeap that can also be done, but the problem is in the logic, not the methods and conversions... tbh its ok if the code is bad for now, it must work first hehe – 5D_cOOKIE Feb 24 '19 at 20:16
  • @SeanStayn those codes work, but not what im looking for, it gives permutations but not repeated ones... cant start it from 11111. it has to be 12345 – 5D_cOOKIE Feb 24 '19 at 20:21
  • Play with my example [here](https://stackoverflow.com/a/19637528/2330053). – Idle_Mind Feb 24 '19 at 20:29
  • https://www.geeksforgeeks.org/print-all-permutations-with-repetition-of-characters/ this is what i actually need, but in C#, any help please? im new to C# and the pointer things use something ref, im quite unsure how to use them... – 5D_cOOKIE Feb 24 '19 at 20:33
  • @Idle_Mind please guide with usage a little bit... – 5D_cOOKIE Feb 24 '19 at 20:35
  • With my example, you'd start it with: `rev = new Revision("ABCDEFGHI", "AAAAA");`, then you'd stop when you get a revision back of length 6. – Idle_Mind Feb 24 '19 at 20:35
  • @Idle_Mind i updated my post, look in the end, how can i call your code over there?... – 5D_cOOKIE Feb 24 '19 at 20:43
  • it wont let me access currentrev says "Error CS1955 Non-invocable member 'Revision.CurrentRevision' cannot be used like a method" – 5D_cOOKIE Feb 24 '19 at 20:54
  • It can. You'll need to declare it at the right scope and then access it. I'll post an example in a bit; need to work on other things at the moment. – Idle_Mind Feb 24 '19 at 20:55
  • okey thanks, ill try myself till then <3 i think this is it so close :D – 5D_cOOKIE Feb 24 '19 at 20:56
  • 1
    OMG IT WORKED! YOU ARE A LEGEND MAN! <3 im dying now hahahaha – 5D_cOOKIE Feb 24 '19 at 21:12
  • Yay! Glad you figured it out. If you like, upvote the linked question? – Idle_Mind Feb 24 '19 at 21:55

0 Answers0