1

I'm trying to convert a Python script to PowerShell but I don't have any Python experience and it's getting long for a little piece of code.

def combinliste(seq, k):
    p = []
    i, imax = 0, 2**len(seq)-1
    while i<=imax:
        s = []
        j, jmax = 0, len(seq)-1
        while j<=jmax:
            if (i>>j)&1==1:
                s.append(seq[j])
            j += 1
        if len(s)==k:
            p.append(s)
        i += 1
    return p

I have made something but I really don't know if it's correct. What is += in PowerShell, is it same as Python?

function combinliste {
    Param($seq, $k)
    $p = @()
    $i; $imax = 0, [Math]::Pow(2, $seq.Count) - 1
    while ($i -le $jmax) {
        $s = @()
        $j, $jmax = 0, $seq.Count - 1
        while ($j -le $jmax) {
            if ($i -shr $j -band 1 -eq 1) {
                $s + ($seq ???? #I don't know how to do it here
            }
            $j #humm.. 1
        }
        if ($s.Count -eq $k) {
            $p + $s
        }
        $i #humm.. 1
        return $p
    }
}

I have tried few variations, but I'm lost.

2
  • I would recommend that you explain exactly what it is you are trying to accomplish. When every variable name is a single character your code becomes difficult to read and interpret. The more you can explain your code in clear language, along with the output you receive, the output you expect, the difference between the two, and where you're stuck, the more likely you are to get a constructive answer. Commented Dec 30, 2018 at 22:34
  • Read get-help about_assignment_operators Commented Dec 30, 2018 at 23:11

2 Answers 2

3
function combinliste { param($seq,$k) 

$p = New-Object System.Collections.ArrayList

$i, $imax = 0, ([math]::Pow(2, $seq.Length) - 1)

while ($i -le $imax) {

     $s = New-Object System.Collections.ArrayList
     $j, $jmax = 0, ($seq.Length - 1)
     while ($j -le $jmax) {
         if((($i -shr $j) -band 1) -eq 1) {$s.Add($seq[$j]) | Out-Null}
         $j+=1
         }
     if ($s.Count -eq $k) {$p.Add($s) | Out-Null }
     $i+=1
   }

  return $p
}

$p = combinliste @('green', 'red', 'blue', 'white', 'yellow') 3

$p | foreach {$_ -join " | "}
Sign up to request clarification or add additional context in comments.

4 Comments

$p.Add($s.ToCharArray()) might be closer depending on the use case.
Wow! I believe in Christmas, I wake up and I see under the tree a piece of freshly translated code. Thank you so much!
Just a question, you use ArrayList for $p but not for $s , is there a reason? or an impact on the simplicity of the code ( ex: $p.Add($s) ) ?
@WhyPok Just updated my answer. I had $s as a string because I assumed that's the input. If you use arrays then arraylist is a better choice then. Actually += operation is pretty inefficient so it's probably not even good for strings if output array is huge.
0

The append() method updates an array in-place. The + operator in PowerShell doesn't do that. You need the += assignment operator for it.

$s += $seq[$j]

and

$p += $s

Alternatively you can use ArrayList collections instead of plain arrays:

$s = New-Object 'Collections.ArrayList'

and use their Add() method:

$s.Add($seq[$j]) | Out-Null

The trailing Out-Null is to suppress the index of the appended item that Add() outputs by default.


Side note: you probably need to put the return $p after the outer while loop, and $i; $imax = ... must be $i, $imax = ... for assigning two values to two variables in one statement.

1 Comment

Many thanks to you too ! I saw your answer before going to bed, it makes me sleep well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.