|
-
Jul 29th, 2020, 06:47 AM
#1
Thread Starter
Junior Member
Vb net permutation list only between odds...
Code:
using System;
using System.Collections.Generic;
namespace PermutationsOfInteger
{
class Program
{
static void Main(string[] args)
{
PrintResult(
Permute(new int[] { 1,2,3 })
);
}
static IList<IList<int>> Permute(int[] nums)
{
var list = new List<IList<int>>();
return DoPermute(nums, 0, nums.Length - 1, list);
}
static IList<IList<int>> DoPermute(int[] nums, int start, int end, IList<IList<int>> list)
{
if (start == end)
{
// We have one of our possible n! solutions,
// add it to the list.
list.Add(new List<int>(nums));
}
else
{
for (var i = start; i <= end; i++)
{
Swap(ref nums[start], ref nums[i]);
DoPermute(nums, start + 1, end, list);
Swap(ref nums[start], ref nums[i]);
}
}
return list;
}
static void Swap(ref int a, ref int b)
{
var temp = a;
a = b;
b = temp;
}
static void PrintResult(IList<IList<int>> lists)
{
Console.WriteLine("[");
foreach (var list in lists)
{
Console.WriteLine($" [{string.Join(',', list)}]");
}
Console.WriteLine("]");
}
}
}
Here is good code for permutation from the link: https://www.chadgolden.com/blog/find...ray-in-c-sharp
Question: How can I change this so it can do permutation only
- even with even
- odd with odds
for example:
Here is list ALL pemutatitions for {"AAA", "BBB", "CCC", "DDD", "EEE"}
AAA+BBB+CCC+DDD+EEE
AAA+BBB+CCC+EEE+DDD
AAA+BBB+DDD+CCC+EEE
AAA+BBB+DDD+EEE+CCC
AAA+BBB+EEE+DDD+CCC
AAA+BBB+EEE+CCC+DDD
AAA+CCC+BBB+DDD+EEE
AAA+CCC+BBB+EEE+DDD
AAA+CCC+DDD+BBB+EEE
AAA+CCC+DDD+EEE+BBB
AAA+CCC+EEE+DDD+BBB
AAA+CCC+EEE+BBB+DDD
AAA+DDD+CCC+BBB+EEE
AAA+DDD+CCC+EEE+BBB
AAA+DDD+BBB+CCC+EEE
AAA+DDD+BBB+EEE+CCC
AAA+DDD+EEE+BBB+CCC
AAA+DDD+EEE+CCC+BBB
AAA+EEE+CCC+DDD+BBB
AAA+EEE+CCC+B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CCC can be permutated with AAA and EEE only....
Thanks
-
Jul 29th, 2020, 11:06 AM
#2
Re: Vb net permutation list only between odds...
Hi Fikri,
First of all your code is C#, this is a vb.net forum. If you would like to convert it to vb.net here's a useful link: https://converter.telerik.com/
Evens with events and odds with odds? Well, you are seeding the class with an array such as { 1,2,3 } in your example. I would say use something like {2, 4, 6, 8} for evens and {1, 3, 5, 7, 9} for odds. Does that help?
Peter
-
Jul 29th, 2020, 12:15 PM
#3
Thread Starter
Junior Member
Re: Vb net permutation list only between odds...
 Originally Posted by Peter Swinkels
Hi Fikri,
First of all your code is C#, this is a vb.net forum. If you would like to convert it to vb.net here's a useful link: https://converter.telerik.com/
Evens with events and odds with odds? Well, you are seeding the class with an array such as { 1,2,3 } in your example. I would say use something like {2, 4, 6, 8} for evens and {1, 3, 5, 7, 9} for odds. Does that help?
Peter
Well,
Maybe I was not clear. I need even and odds positions, not values.
Thanks
-
Jul 29th, 2020, 12:27 PM
#4
Re: Vb net permutation list only between odds...
Could you post an example of the output you expect using { 1,2,3}?
-
Jul 29th, 2020, 12:32 PM
#5
Thread Starter
Junior Member
Re: Vb net permutation list only between odds...
AAA-BBB-CCC-DDD-EEE
AAA-DDD-CCC-BBB-EEE
CCC-BBB-AAA-DDD-EEE
EEE-DDD-AAA-DDD-AAA
AAA, CCC, EEE - can be on positions 1,3,5
BBB,DDD - can be on positions 2,4
Thanks
-
Jul 29th, 2020, 12:37 PM
#6
Re: Vb net permutation list only between odds...
Hmmm, the only thing that comes to mind is sorting the data before displaying it. At this moment I can't access vb.net.
-
Jul 29th, 2020, 01:45 PM
#7
Re: Vb net permutation list only between odds...
Off the top of my head, but not trying anything, I would think you would combine the evens into a list, and do the permutations, and the odds in a list and do the permutations.
Then, I would think you would just do a nested loop through the two lists, combining them.
Code:
Combine even position values into a list.
Combine odd position values into a list.
Permute the even list.
permute the odd list.
For each even permutation in the even list
For each odd permuation in the odd list
Combine the even and odd positions into a result list
Next
Next
I don't know if that would give what you want, but it seems like it would.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jul 30th, 2020, 12:19 AM
#8
Thread Starter
Junior Member
Re: Vb net permutation list only between odds...
 Originally Posted by passel
Off the top of my head, but not trying anything, I would think you would combine the evens into a list, and do the permutations, and the odds in a list and do the permutations.
Then, I would think you would just do a nested loop through the two lists, combining them.
Code:
Combine even position values into a list.
Combine odd position values into a list.
Permute the even list.
permute the odd list.
For each even permutation in the even list
For each odd permuation in the odd list
Combine the even and odd positions into a result list
Next
Next
I don't know if that would give what you want, but it seems like it would.
Hm..., that is hard way for me, I am trying to remove from list combinations I don't need...
-
Jul 30th, 2020, 12:39 AM
#9
Thread Starter
Junior Member
Re: Vb net permutation list only between odds...
Problem with removing items from list is when there is same value on odd and even positions like:
AAA-AAA-CCC-DDD-EEE
Idea was to remove items from unwanted positions...
Maybe it will be better to do it with array not by list, because on that way we can save positions no matter about values...??
-
Jul 31st, 2020, 08:16 AM
#10
Re: Vb net permutation list only between odds...
Why is that a problem?
If you want permutations based on position, then the contents shouldn't matter.
If you get the permutations for the odd positions and the even positions, then combine the two list (re-interleaving the even and odd positions), you shouldn't get any values that were in an odd position ending up in an even position, and no items that were in an even position ending up in an odd position.
If having an item with the same value in both an even and odd position causes a problem, what is the problem, how would you want it resolve?
An expanded example of what was described in post #7
Code:
AAA-BBB-CCC-DDD-EEE Initial list.
Split into two lists, containing even positions in one, and odd position in the second
Determine the permutations of the two lists
AAA-CCC-EEE BBB-DDD
AAA-EEE-CCC DDD-BBB
CCC-AAA-EEE
CCC-EEE-AAA
EEE-AAA-CCC
EEE-CCC-AAA
Combine the two lists in a nested loop
AAA-BBB-CCC-DDD-EEE (Combine Even list with odd list item BBB-DDD)
AAA-BBB-EEE-DDD-CCC
CCC-BBB-AAA-DDD-EEE
CCC-BBB-EEE-DDD-AAA
EEE-BBB-AAA-DDD-CCC
EEE-BBB-CCC-DDD-AAA
AAA-DDD-CCC-BBB-EEE (Combine Even list with odd list item DDD-BBB)
AAA-DDD-EEE-BBB-CCC
CCC-DDD-AAA-BBB-EEE
CCC-DDD-EEE-BBB-AAA
EEE-DDD-AAA-BBB-CCC
EEE-DDD-CCC-BBB-AAA
So, you end up with twelve total permutations by restricting the odd position to only swap with odd positions, and even positions to only swap with even positions.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jul 31st, 2020, 11:32 PM
#11
Thread Starter
Junior Member
Re: Vb net permutation list only between odds...
Yes, that was solution. very good idea. Working good and fast.
Thanks.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|