Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combination without repitition

Status
Not open for further replies.

blue_3377

Systems Engineer
Dec 9, 2017
2
GB
Hi guys

I am hoping someone can help me with this PowerShell script.

The script lists the unique combination of 26 letter alphabet ($List) based on 3 letter ($k)

If I keep the $List shorter like A to K for instance, it works. But with 26 letter it doesn't work

Thank you

############

$List = "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
$k = 3

Add-Type @"
public class Shift {
public static int Right(int x, int count) { return x >> count; }
public static uint Right(uint x, int count) { return x >> count; }
public static long Right(long x, int count) { return x >> count; }
public static ulong Right(ulong x, int count) { return x >> count; }
public static int Left(int x, int count) { return x << count; }
public static uint Left(uint x, int count) { return x << count; }
public static long Left(long x, int count) { return x << count; }
public static ulong Left(ulong x, int count) { return x << count; }
}
"@

function CombinationWithoutRepetition ([int]$k, $List)
{
Function IsNBits ([long]$value, $k, $length)
{
$count = 0
for ($i = 0 ; $i -le $length ; $i++)
{
if ($value -band 1)
{
$count++
}
$value = [shift]::Right($value,1)
}

if ($count -eq $k)
{
return $true
}
else
{
return $false
}
}

Function BitsToArray ([long]$value, $List)
{
$res = @()
for ($i = 0 ; $i -le $List.length ; $i++)
{
if ($value -band 1)
{
$res += $List[$i]
}
$value = [shift]::Right($value,1)
}

return ,$res
}

[long]$i = [Math]::pow(2, $List.Length)
$res = @()
for ([long]$value=0 ; $value -le $i ; $value++)
{
if ((IsNBits $value $k $List.Length) -eq $true)
{
#write-host $value
$res += ,(BitsToArray $value $List)
}
}
return ,$res
}

Clear-Host
$res = CombinationWithoutRepetition $k $List
$res.count
$res | Sort-Object | % { $_ -join ','}
 
If you're just looking to create a list of three-letter combinations this should work for you

Code:
$List = "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
$k = $list.count

$res = @()

for ($h=0; $h -lt $k; $h++)
	{
	 for ($i=0; $i -lt $k; $i++)
		{
		 for ($j=0; $j -lt $k; $j++)
			{
			 $tla = $list[$h]+$list[$i]+$list[$j]
			 $res += $tla
			}
		}
	}


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Thank you blister911

I am looking for unique list like ABC, ABD etc. Also position of letters doesn't matter. i.e ABC = BCA, so BCA shouldn't be listed
 
I don't know PowerShell but I think blister911's code would omit duplicate letter combinations if $i was initialized to $h+1 (instead of 0) and $j was initialized to $i+1.
 
Dave,
I don't think that would work, because you'd never get AA*. The first TLA would be ABC.

Blue,
It isn't very elegant, but try this:

Code:
$List = "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
$k = $list.count

$res = @()

for ($h=0; $h -lt $k; $h++)
	{
	 for ($i=0; $i -lt $k; $i++)
		{
		 for ($j=0; $j -lt $k; $j++)
			{
			 $letter_array = @()
			 $letter_array += $list[$h]
			 $letter_array += $list[$i]
			 $letter_array += $list[$j]
			 
			 $letter_array = $letter_array | sort
			 $tla = $letter_array[0]+$letter_array[1]+$letter_array[2]
			 
			 if ($res -notcontains $tla)
				{$res += $tla}
			}
		}
	}


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top