×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Combination without repitition

Combination without repitition

Combination without repitition

(OP)
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 ','}

RE: Combination without repitition

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.

RE: Combination without repitition

(OP)
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

RE: Combination without repitition

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.

RE: Combination without repitition

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close