header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Permutations
Last Post 10 May 2010 03:31 AM by Maks77. 0 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
Maks77User is Offline
New Member
New Member
Posts:6
Avatar

--
10 May 2010 03:31 AM

    Hi, I am trying to create a powershell program that is able to print out all permutations of
    a word.

    As an easy example, let's take the following character combination:

    abcd

    The outcome of this permutation should be:

    abcd
    abdc
    adbc
    adcb
    a........


    What I have written so far is the following code, but it does not work. What's wrong here?
    I tried already many changes. Change k from 0 to 1, change ther perm function without the -1
    but one of the issues is also the offset of the powershell array that is beginning from 0 and the number of characters in a word that begins at 1. But this should be solved by the perm(anzahl-1) and k beginning at 0.

    But anyway, the code ends before all permutations are done or leaves some of them just away.
    What could be wrong? This is coed I tried to translate from a Java example. So I tested already everything some hours. Would be nice if somebody has some additional ideas.


    # Funktion Ausgabe
    function ausgabe($anzahl)
    {
       for ( $k1 = 0; $k1 -le $anzahl; $k1++ )
       {
          Write-Host "k: " $k 
          Write-Host " a von k1 unter ausgabe: " $neuesWort[$k1] " "
       }
    }

    # Funktion Perm
    function perm ($n)
    {
      $hilf = ''
      Write-Host "n in perm: " $n
      if ($n -eq 1)
      {
        Write-Host " n ist gleich 1: " $n
        $p = $p + 1
        Write-Host "p: " $p

      }
     
      else
      {
        Write-Host " else: "
        # $n = ($n-1)
        for ($k=($n-1); $k -ge 1; $k=$k-1)
        {
           Write-Host " k: " $k
           Write-Host " n: " $n
           Write-Host " a von k: " $a[$k]
           # Tausche a[k] und a[n]
           $hilf = $a[$k]
           Write-Host " hilf: " $hilf
          
           $a[$k] = $a[$n]
           Write-Host " a von k: " $a[$k]
          
           $a[$n] = $hilf
           Write-Host " a von n: " $a[$n]
          
           $neuesWort = $a
           Write-Host "neuesWort: " $neuesWort

           #Funktion perm aufrufen
           perm ($n-1)

           # Ruecktausch von a[k] und a[n]
           $hilf=$a[$k]
           Write-Host " hilf: " $hilf
          
           $a[$k]=$a[$n]
           Write-Host " a von k: " $a[$k]
          
           $a[$n]=$hilf
           Write-Host " a von n: " $a[$n]
        } # Ende der Zaehlschleife
      } # Ende der Verzweigung
    } # Ende der Methode perm


    $p = 0
    $anzahl = 0
    $a = @()

    $eingabe = Read-Host "Bitte Zeichenkette eingeben:"
    $anzahl = $eingabe.length
    Write-Host "Anzahl eingabe length: " $anzahl

    for ($i=0; $i -le $anzahl-1; $i=$i+1)
    {
      $a += $eingabe.Substring($i,1)
      Write-Host " a: " $a

    }

    perm ($anzahl-1)

    Write-Host ""
    Write-Host ""
    Write-Host $p " Permutationen"


    # -eq (equals; =, ==)
    # -ne (not equals; !=, <>)
    # -gt (greater than, >)
    # -ge (greater or equal, >=)
    # -lt (lower than, <)
    # -le (lower or equal, <=)
    # Hier ein Beispiel für einen Vergleich der „True“ liefert: 5 –lt 10

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer