• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Find looping between 2 lists

Hi there;

I got a C# application with 2 lists as follows:

List1 List2
1.     4
2.     3
3.     5
4.     6
5.     2
6.     1

I need to grab the conditions as follows
1 4, 4 6, 6 1
2  3, 3 5, 5 2

The starting number should end as above.

How can i find such looping in C#?

Kind regards.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 26
  • 18
  • 8
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jazz,

It's pretty easy to "walk" through a list of pointers.  The trick is knowing when to stop.  In your case, it could be that you quit walking after 3 nodes, when the new index is less than the current index, when the new index is less than the first index, or any of several other keys.

The example below walks you through the arrays.  It's just one of many possible solutions.


Good Luck,
Kent

int List1[7] = { 1, 2, 3, 4, 5, 6, 0 };
int List2[7] = { 4, 3, 5, 6, 2, 1, 0 };

int WalkArray (int start)
{
  int current;
  int new;
  
  current = start;
  while (1)
  {
    new = List2[Current];
    if (new < current)
      break;
    printf (" Start = %d, Nodes = %d,%d\n", start, current, new);
    current = new;
  }
}

Open in new window

0
 
jazzIIIloveAuthor Commented:
Ah, you haven't used List1 to compare?
0
 
jazzIIIloveAuthor Commented:
I believe that you haven't realize there is a loop between those 2 lists.

Kind regards.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like this out:  
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int StartingIndex = 1; // <-- Set the "starting index"

        private int Index = -1; // <-- Sentinel value of -1

        // Arrays in .Net are ZERO based so we have to pad them with a dummy value at Index 0 (zero):
        private int[] List1 = {-1, 1, 2, 3, 4, 5, 6 };
        private int[] List2 = {-1, 4, 3, 5, 6, 2, 1 };

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Index == -1)
            {
                Index = StartingIndex;
            }

            int NextIndex = List2[Index];

            listBox1.Items.Add(Index.ToString() + " --> " + NextIndex.ToString());

            Index = NextIndex;
        }
    }

Open in new window


Output: Idle-Mind-513187.flv
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jazz,

You don't need List1, except as the starting point.

int List1[7] = { 1, 2, 3, 4, 5, 6, 0 };
int List2[7] = { 4, 3, 5, 6, 2, 1, 0 };


Note that the zero should be first in the array, not last, but that doesn't change how you walk through it.

int List1[7] = {0, 1, 2, 3, 4, 5, 6};
int List2[7] = {0, 4, 3, 5, 6, 2, 1};

When you start at 1, you fetch a 4 from List2.  Now there's no need to go back to List1 as the 4 is the node that you want in List2.  List2[4] contains a 6, which becomes the next node.  List2[6] contains a 1, which signals that you've hit the end of the list.


Kent
0
 
jazzIIIloveAuthor Commented:
Hi kdo;

Thanks for the interest but simply i failed to utlize your idea to code sadly.

Give a hand?

Regards.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

I'll be glad to lend a hand.  Where do we start?

Kent
0
 
jazzIIIloveAuthor Commented:
Need a code?

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you look at my post at all?...

The crux of it is here:

            int NextIndex = List2[Index];

            listBox1.Items.Add(Index.ToString() + " --> " + NextIndex.ToString());

            Index = NextIndex;

I grab the "next" index using the "current" index and then do something with it.  Afterwards I set the current index to the next index value.

That's really all there is to it.

How is this going to be used in your actual application?  Perhaps understanding that would give us insight into what you're after...
0
 
jazzIIIloveAuthor Commented:
Hi there Idle_Mind:

Sorry for my delay, i checked your code but i don't think this is what i want:


 int[] List1 = {-1, 1, 2, 3, 4, 5, 6 };
 int[] List2 = {-1, 4, 3, 5, 6, 2, 1 };

As the lists are like this, my aim is to grab 5,2 (5th element of the lists) and 6,1.

The rule is:
1 4
4 6
6 1

one loop starting with 1 ending with 1.
->2<- 3
3 5
5 ->2<-
another loop starting and ending with 2

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I just showed how to deal with the first loop.  Did you watch the output in the ListBox?

For two loops, you'd just need to add another variable to track the index for that loop.

The loops are independent of each other right?

Again, how is this going to be used?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jazz,

Look at your tables.

 int[] List1 = {-1, 1, 2, 3, 4, 5, 6 };
 int[] List2 = {-1, 4, 3, 5, 6, 2, 1 };

Open in new window


Now key on the items in List2.  There are two chains within the list

 int[] List1 = {-1, 1, 2, 3, 4, 5, 6 };
 int[] List2 = {-1, x, 3, 5, x, 2, x };

Open in new window


and

 int[] List1 = {-1, 1, 2, 3, 4, 5, 6 };
 int[] List2 = {-1, 4, x, x, 6, x, 1 };

Open in new window


No matter where you start in the table, you'll follow one of these chains.  All that List1 does is establish a starting point.  List1 defines every node so that:

  List1[n] = n;

You could jumble the items in List1 into any order that you like.  All it will do is jumble where a loop through a chain begins.  The chaining is self contained within List2.


Kent
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Does this help illustrate better?

It shows how each starting position "chains":
private int[] List1 = { -1, 1, 2, 3, 4, 5, 6};
        private int[] List2 = { -1, 4, 3, 5, 6, 2, 1 };

        private void timer1_Tick(object sender, EventArgs e)
        {
            ListBox LB;
            int NextIndex;
            for (int i = 1; i <= List1.GetUpperBound(0); i++)
            {
                NextIndex = List2[List1[i]];

                LB = (ListBox)this.Controls["listBox" + i.ToString()];
                LB.Items.Add(List1[i].ToString() + " --> " + NextIndex.ToString());

                List1[i] = NextIndex;
            }
        }

Open in new window

Idle-Mind-513275.flv
0
 
jazzIIIloveAuthor Commented:
you are going to hate me but:


When I do this, I couldn't see the loop;

I mean I just want to output the forwarding loop:

There is a loop, 1 4, 4 6, 6 1
...

but not 4 6, 6 1, 1 4 as it's already covered in the above loop.

Regards.
private static List<int> List1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        private static List<int> List2 = new List<int> { 4, 3, 5, 6, 2, 1 };

        static void Main(string[] args)
        {            
            int NextIndex;
            for (int i = 1; i <= List1.Max(); i++)
            {
                NextIndex = List2[List1[i]];

                Console.WriteLine(i.ToString());
                Console.WriteLine(List1[i].ToString() + " --> " + NextIndex.ToString());

                List1[i] = NextIndex;
            }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Look at this output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        private static List<int> List1 = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        private static List<int> List2 = new List<int> { 0, 4, 3, 5, 6, 2, 1, 8, 7, 9};

        static void Main(string[] args)
        {
            List<List<int>> loops = new List<List<int>>();

            for (int i = 1; i < List1.Count; i++)
            {
                OutputLoop(i);
            }
            
            Console.Write("Press Enter to Quit");
            Console.ReadLine();
        }

        private static void OutputLoop(int StartingIndex)
        {
            Console.Write("Loop #" + StartingIndex.ToString() + ": ");
            int counter = 0;
            int Index = StartingIndex;
            int NextIndex;
            do
            {
                NextIndex = List2[Index];
                Console.Write(Index.ToString() + " --> " + NextIndex.ToString() + "   ");
                Index = NextIndex;
                counter++;
            } while (Index != StartingIndex && counter < List2.Count);
            Console.WriteLine("");
        }

    }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
...and what about something like this?

        private static List<int> List1 = new List<int> { 0, 1, 2, 3};
        private static List<int> List2 = new List<int> { 0, 2, 1, 1};

The first two are:

    1 --> 2, 2 --> 1
    2 --> 1, 1 --> 2

But the last one is:

    3 --> 1, 1 --> 2, 2 --> 1

The third loop includes the first two, but is not equal to them.
0
 
jazzIIIloveAuthor Commented:
good, thanks, the looping should be just forward.

in the case
List 1: 0, 1, 2, 3
List 2: 0, 2, 1, 1

1 2, 2 1 done

but if the following is the case;
List 1: 0, 1, 2, 4
List 2: 0, 2, 4, 1

1 2, 2 4, and 4, 1 so there is a loop.
0
 
jazzIIIloveAuthor Commented:
unfortunately, your code fails for the given lists:
  private static List<int> List1 = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23 };
        private static List<int> List2 = new List<int> { 0, 3, 3, 6, 5, 2, 2, 9, 8, 11, 1, 13, 14, 12, 19, 18, 20, 16, 22, 21, 23 };

I will provide my code tomorrow.

Kind regards.

P.S. Maybe, i couldn't define the problem well...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are only 21 slots in the arrays, using indexes 0 thru 20, so how can you jump to positions 21, 22, or 23?!
0
 
jazzIIIloveAuthor Commented:
hmm, you are right. My mistake, sorry.
0
 
jazzIIIloveAuthor Commented:
Ok, one final thing is that i got lost in the output and cannot find a way in your code to output the start of the loop which is e.g.:

1 3
2 4
3 4
4 1

The first occurence of the loop is as:
1 3
3 4
4 1

The output should be 1 3 line as it's the first loop occuring (precedence over the 2. line).

So the output should be just 1 3 (just) but i couldn't solve it how i can do this in your code.

Kind regards and thanks for your interest, moreover patience.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you want the pair in the loop sequence that has the lowest starting value?
0
 
jazzIIIloveAuthor Commented:
yep, bulls eye.
0
 
jazzIIIloveAuthor Commented:
Ok, one final thing is that i got lost in the output and cannot find a way in your code to output the start of the loop which is e.g.:

1 3
2 4
3 4
4 1

1 3
3 4
4 1

The output should be 1 3 line as it's the first loop occuring (precedence over the 2. line).

So the output should be just 1 3 (just) but i couldn't solve it how i can do this in your code.

Kind regards and thanks for your interest, moreover patience.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this out:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        private static List<int> List1 = new List<int> { 0, 1, 2, 3, 4, 5, 6};
        private static List<int> List2 = new List<int> { 0, 4, 3, 5, 6, 2, 1};

        static void Main(string[] args)
        {
            Dictionary<string, List<string>> loops = new Dictionary<string, List<string>>();

            List<string> loop;
            List<string> tmpLoop;
            for (int i = 1; i < List1.Count; i++)
            {
                loop = OutputLoop(i);

                tmpLoop = new List<string>();
                tmpLoop.AddRange(loop.ToArray());
                tmpLoop.Sort();

                if (!loops.ContainsKey(tmpLoop[0]))
                {
                    loops.Add(tmpLoop[0], loop);
                }
            }

            Console.WriteLine("Unique Loops:");
            foreach (KeyValuePair<string, List<string>> UniqueLoop in loops)
            {
                Console.WriteLine("Starts with: " + UniqueLoop.Key + "  |  Sequence: " + String.Join(", ", UniqueLoop.Value.ToArray()));
            }

            Console.Write("Press Enter to Quit");
            Console.ReadLine();
        }

        private static List<string> OutputLoop(int StartingIndex)
        {
            List<string> loop = new List<string>();

            int counter = 0;
            int Index = StartingIndex;
            int NextIndex;
            do
            {
                NextIndex = List2[Index];
                loop.Add(Index.ToString() + " " + NextIndex.ToString());
                Index = NextIndex;
                counter++;
            } while (Index != StartingIndex && counter < List2.Count);
            return loop;
        }

    }
}

Open in new window

0
 
jazzIIIloveAuthor Commented:
ah, above fails for longer lists regarding index out of bound but I couldn't find out the solution to this problem.

and is it compulsory to start with 0 in the list?

Kind regards.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jazz,

Post the longer list that is failing.

The first item is zero because C uses "relative zero" referencing.  If you want List[x] to be position x in the array, you need to put a dummy element in the front of the array as a place holder.


Kent
0
 
jazzIIIloveAuthor Commented:
List1 List2
1      3
2      3
3      6
4      5
5      2
6      2
7      9
9      11
10      1
11      13
13      14
14      12
15      19
17      18
19      20
20      16
21      22
22      21
23      23
24      23


Regards.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jazz,

The largest element in each of those lists is larger than the number of items in the list.

I count 19 items in each.  Since C uses "relative 0" indexing, the largest value in the list can only be 18.


Kent
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Assuming you provide VALID input Arrays, this should handle finding the "starting pair" better than the previous approach which used a string comparison:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        private static List<int> List1 = new List<int> { 0, 1, 2, 3, 4, 5, 6 };
        private static List<int> List2 = new List<int> { 0, 4, 3, 5, 6, 2, 1 };

        static void Main(string[] args)
        {
            Dictionary<string, Sequence> Sequences = new Dictionary<string, Sequence>();

            Sequence sq;
            for (int i = 1; i < List1.Count; i++)
            {
                sq = GenerateSequence(i);

                if (!Sequences.ContainsKey(sq.StartingPair.ToString()))
                {
                    Sequences.Add(sq.StartingPair.ToString(), sq);
                }
            }

            Console.WriteLine("Unique Sequences:");
            foreach (KeyValuePair<string, Sequence> UniqueLoop in Sequences)
            {
                Console.WriteLine("Starts with: " + UniqueLoop.Key.ToString() + "  |  Sequence: " + UniqueLoop.Value.ToString());
            }

            Console.WriteLine("");
            Console.Write("Press Enter to Quit");
            Console.ReadLine();
        }

        private static Sequence GenerateSequence(int StartingIndex)
        {
            List<SequencePair> sequence = new List<SequencePair>();
            int counter = 0;
            int Index = StartingIndex;
            int NextIndex;
            do
            {
                NextIndex = List2[Index];
                sequence.Add(new SequencePair(Index, NextIndex));
                Index = NextIndex;
                counter++;
            } while (Index != StartingIndex && counter < List2.Count);

            return new Sequence(sequence);
        }

    }

    public class Sequence
    {

        public SequencePair StartingPair = null;
        public List<SequencePair> Pairs;

        public Sequence(List<SequencePair> Pairs)
        {
            this.Pairs = Pairs;
            foreach (SequencePair sp in Pairs)
            {
                if (this.StartingPair == null || sp.value1 < StartingPair.value1)
                {
                    this.StartingPair = sp;
                }
            }
        }

        public override string ToString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (SequencePair sp in this.Pairs)
            {
                sb.Append(sp.ToString() + ", ");
            }
            return sb.ToString().TrimEnd(", ".ToCharArray());
        }
    }

    public class SequencePair
    {

        public int value1;
        public int value2;

        public SequencePair(int value1, int value2)
        {
            this.value1 = value1;
            this.value2 = value2;
        }

        public override string ToString()
        {
            return value1.ToString() + " " + value2.ToString();
        }

    }

}

Open in new window

0
 
jazzIIIloveAuthor Commented:
Kdo:
Ok, but no way to fix this as I was losing my mind to find a way for this...

Idle_Mind:
I will check soon, and my list is integer for the record. It won't hurt, right?

Kind regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"... but no way to fix this"

Where are you getting your input from?
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Fix it?  Just make sure that the array has at least as many elements as the largest value that you're going to store.


I'm curious about your application.  How did you happen to get ahold of it?


Kent
0
 
jazzIIIloveAuthor Commented:
Idle_Mind:

from a database table having 2 integer columns, I just read the content...

Regards.

Kdo:

>> Just make sure that the array has at least as many elements as the largest value that you're going to store.

So, largest value in the list is 25, the list must have 25 elements (so no skip during the increment of the value)

1
3
4
5
..fails. True?

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The current implementations have assumed that the numbers correlate with an absolute Index value in the Arrays.

Given your last inputs of:

    List1:  1,  2,  3,  4,  5,  6,  7,  9, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23, 24
    List2:  3,  3,  6,  5,  2,  2,  9, 11,  1, 13, 14, 12, 19, 18, 20, 16, 22, 21, 23, 23

We could instead search List1 for the matching "next value" entry from List2 and then take the corresponding value in List2 that is at the same Index of the found entry in List1.

This doesn't make sense either, though, since List1 doesn't have an entry for 12, 18, or 16.

So what would you expect to get back from those inputs?

I see:

1 3, 3 6, 6 2, 2 3, 3 6, 6 2, ...
2 3, 3 6, 6 2, ...
3 6, 6 2, 2 6, ...
4 5, 5 2, 2 3, 3 6, 6 2, ...
5 2, 2 3, 3 6, 6 2, ...
6 2, 2 3, 3 6 , ...

7 9, 9 11, 11 13, 13 14, 14 12, ???
9 11, 11 13, 13 14, 14 12, ???

10 1, 1 3, 3 6, 6 2, 2 3, ...

11 13, 13 14, 14 12, ???
13 14, 14 12, ???
15 19, 19 20, 20 16, ???
17 18, ???
19 20, 20 16, ???
20 16, ???

21 22, 22 21, ...
22 21, 21 22, ...

23 24, 24 23, ...
24 23, 23 24, ...
0
 
Kent OlsenData Warehouse Architect / DBACommented:
>>>> Just make sure that the array has at least as many elements as the largest value that you're going to store.

>>So, largest value in the list is 25, the list must have 25 elements (so no skip during the increment of the value)


  25 + 1

Element 0 (list[0]) has to be allocated, too.


Kent
0
 
jazzIIIloveAuthor Commented:
Idle_Mind; just forwarding ones,  turning back in the list.
1 3, 3 6, 6 2, 2 3, 3 6, 6 2, ...
2 3, 3 6, 6 2, ...
3 6, 6 2, 2 6, ...

This loop is not true as 1 3 6 2 is not the regular list 2 cannot come after 2.

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
?...

Not following you there sorry.

Given those lists, what output were you expecting to get?
0
 
jazzIIIloveAuthor Commented:
I will give the most definitive comment tomorrow.

Regards and thanks for not giving up.
0
 
jazzIIIloveAuthor Commented:
Ok, my mistake

In fact the lists are just the extraction of a table record of an MSSQL database,

both lists are indicating the columns which form a composite key.

The list1 is incremental but a row may be deleted by the user.
           a  b   c    d   e   f   g   h
List1:  1,  2,  3,  4,  5,  6,  7,  9, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23, 24
List2:  3,  3,  6,  5,  2,  2,  9, 11,  1, 13, 14, 12, 19, 18, 20, 16, 22, 21, 23, 23

Given the lists above

1 3 3 6 6 6 2 is not forming a loop,  (a, c, f) You cannot go back as 2 6 (which is b), you have to go forward, and the output of the program should be the first row which is 1 3(a)

2 3 3 6 6 2  is another loop (b, c, f) and the output of the program should be as 2 3

3 6 6 2 is another loop but a loop within loop, so it shouldn't be outputted.

4 5 5 2 is not a loop

7 9 9 11 11 13 13 14 14 12 is not a loop

15 19 19 20 20 16 is not a loop

21 22 22 21 is a loop

23 23 is a loop

23 23 24 23 is another loop (not an inner loop as it's different than above, 23 23)

Hope this clarifies.

Kind regards.
0
 
jazzIIIloveAuthor Commented:
I had asked a similar question but with MSSQL idea, here you;
http://mobile.experts-exchange.com/Q_27400368.html
Same rules are applying there too.

Kind regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so 1 3 3 6 6 6 2 return 1 3.

...wouldn't 4 5 5 2 2 3 3 6 6 2 return 4 5 then?

They both are "not loops?

21 22 22 21 is a loop but it doesn't return anything?  Wouldn't it return 21 22?

22 21 21 22 would be omitted right?  (since it's a duplicate of  21 22)

Does 23 23 return 23 23?

24 23 23 23 would return 24 23?  (based on the first example returning 1 3)

Do you need to assign a property to these things to indicate the possible return states?
(1) Complete loop - starts and ends on the same number
(2) Duplicate loop
(3) Incomplete loop - loops but doesn't return to starting number
(4) Does not loop

0
 
jazzIIIloveAuthor Commented:
...wouldn't 4 5 5 2 2 3 3 6 6 2 return 4 5 then?

No, in this context, after the second 5, you cannot go back to 2, so, no.

21 22 22 21 is a loop and woukd return 21 22

23 23 will return 23 23 in this context.

>>24 23 23 23 would return 24 23?  (based on the first example returning 1 3)

yes.


Ok...so 1 3 3 6 6 6 2 return 1 3.

...wouldn't 4 5 5 2 2 3 3 6 6 2 return 4 5 then?

They both are "not loops?

21 22 22 21 is a loop but it doesn't return anything?  Wouldn't it return 21 22?

22 21 21 22 would be omitted right?  (since it's a duplicate of  21 22)

Does 23 23 return 23 23?

24 23 23 23 would return 24 23?  (based on the first example returning 1 3)

Do you need to assign a property to these things to indicate the possible return states?
(1) Complete loop - starts and ends on the same number
(2) Duplicate loop
(3) Incomplete loop - loops but doesn't return to starting number
(4) Does not loop

No need to output 3 and 4

Duplication of a loop can/should be omitted.

For 1, yes.

I believe that the question gets longer because of my lacking explanation, sorry for this.

Regards.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Still don't understand why these two are different:

    1 3 3 6 6 2 2 3
    4 5 5 2 2 3 3 6 6 2

The first one starts with 1 3, loops 3 6 6 2 2 3, and returns 1 3.

The second one starts with 4 5 5 2, loops 2 3 3 6 6 2 and returns nothing?
0
 
jazzIIIloveAuthor Commented:
1 3 3 6 6 2
from 6 to 2, not allowed, you are going back

Even from 6 to 2, there is a loop, it's not allowed in this context. Forward, always forward.

1 3 3 6 6 7 7 1

see, forward, so valid.

In your examples, you are going back for the transition, always forward.

Regards.

So 4 5 5 2 is a dead end, cannot be considered as a loop, because you want to go back to 2, not allowed.

Hope it's clear, I am terribly sorry, Idle_Mind, you are a good guy, still stick to this question, I really appreciate your effort and interest.

You are a good guy...

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No, no...I see it now.  Got it!  So only the last number, forming the actual loopback, can be less than the previous number.

I'll see if I can modify the code to take that into account.
0
 
jazzIIIloveAuthor Commented:
I asked too many questions, but u are the one who is the most patient one, 42 comments, we have had and u still stay in my question, if any admin watches out this thread, i hereby declare u as the most patient one in EE.

How come? :)

Regards.
0
 
jazzIIIloveAuthor Commented:
Ok this is not being patient but passion :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See if this one works as expected:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        private static List<int> List1 = new List<int> { 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23, 24 };
        private static List<int> List2 = new List<int> { 3, 3, 6, 5, 2, 2, 9, 11, 1, 13, 14, 12, 19, 18, 20, 16, 22, 21, 23, 23 };

        static void Main(string[] args)
        {
            Dictionary<string, Sequence> Sequences = new Dictionary<string, Sequence>();

            Sequence sq;
            for (int i = 1; i < List1.Count; i++)
            {
                sq = GenerateSequence(List1[i]);
                if (sq != null)
                {
                    if (!Sequences.ContainsKey(sq.StartingPair.ToString()))
                    {
                        Sequences.Add(sq.StartingPair.ToString(), sq);
                    }
                }
            }

            Console.WriteLine("Unique Sequences:");
            foreach (KeyValuePair<string, Sequence> UniqueLoop in Sequences)
            {
                Console.WriteLine("Starts with: " + UniqueLoop.Key.ToString() + "  |  Sequence: " + UniqueLoop.Value.ToString());
            }

            Console.WriteLine("");
            Console.Write("Press Enter to Quit");
            Console.ReadLine();
        }

        private static Sequence GenerateSequence(int StartingValue)
        {
            List<SequencePair> sequence = new List<SequencePair>();
            int counter = 0;
            int Value = StartingValue;
            int NextValue;
            int NextValueIndex;
            do
            {
                NextValueIndex = List1.IndexOf(Value);
                if (NextValueIndex != -1)
                {
                    NextValue = List2[NextValueIndex];
                    if (NextValue >= Value)
                    {
                        sequence.Add(new SequencePair(Value, NextValue));
                        Value = NextValue;
                    }
                    else
                    {
                        foreach (SequencePair sp in sequence)
                        {
                            if (sp.value1 == NextValue)
                            {
                                // we found a loop!
                                sequence.Add(new SequencePair(Value, NextValue));
                                return new Sequence(sequence);
                            }
                        }
                        return null;
                    }
                }
                else
                {
                    return null;
                }

                counter++;
            } while (Value != StartingValue && counter < List2.Count);

            return new Sequence(sequence);
        }

    }

    public class Sequence
    {

        public SequencePair StartingPair = null;
        public List<SequencePair> Pairs;

        public Sequence(List<SequencePair> Pairs)
        {
            this.Pairs = Pairs;
            foreach (SequencePair sp in Pairs)
            {
                if (this.StartingPair == null || sp.value1 < StartingPair.value1)
                {
                    this.StartingPair = sp;
                }
            }
        }

        public override string ToString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (SequencePair sp in this.Pairs)
            {
                sb.Append(sp.ToString() + ", ");
            }
            return sb.ToString().TrimEnd(", ".ToCharArray());
        }
    }

    public class SequencePair
    {

        public int value1;
        public int value2;

        public SequencePair(int value1, int value2)
        {
            this.value1 = value1;
            this.value2 = value2;
        }

        public override string ToString()
        {
            return value1.ToString() + " " + value2.ToString();
        }

    }

}

Open in new window

0
 
jazzIIIloveAuthor Commented:
This question is my shame but concluded with a success definitely.

Regards.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Glad that worked for you.  =)
0
 
jazzIIIloveAuthor Commented:
Patient you are Idle_Mind! Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 26
  • 18
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now