Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it L'avatar di supermac
    Registrato dal
    Jun 2001
    Messaggi
    1,856

    [Funzione] Permutazione di stringhe

    Qualcuno per caso ha una funzioncina ricorsiva serverside che, data una stringa di parole, la splitti sul carattere spazio e restituisca un array di stringhe contenente tutte le possibili permutazioni senza ripetizioni?
    (la stringa in input avrà max 4 o 5 parole...)


    Qui ho trovato qualcuno che lo fa https://textmechanic.com/text-tools/...ion-generator/

    Già così sarebbe d'oro... volendo fare i difficili aggiungerei che se la stringa in input è, ad esempio di 3 parole, restituisca anche le permutazioni di 1 parola e di 2 parole (sempre senza ripetizioni!)... ma questo sarebbe grasso che cola.
    Ultima modifica di supermac; 06-06-2018 a 12:20
    W la Ferari effetrenavenave!
    il computer è un somaro veloce! (neanche tanto ndr)

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    se ci fai caso nel sorgente c'e' la funzione lato client che fa le permutazioni, basta UN ATTIMINO trasformarla lato server

    http://jsfromhell.com/array/permute


    codice:
    permute = function(v, m){
        for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
        for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
            ++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
        for(r = new Array(q); ++p < q;)
            for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
                x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
                for(x[3][i] = x[2][i], f = 0; !f; f = !f)
                    for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
        return r;
    };
    Ultima modifica di djciko; 07-06-2018 a 13:25

  3. #3
    Utente di HTML.it L'avatar di supermac
    Registrato dal
    Jun 2001
    Messaggi
    1,856
    sull'"attimino" dissento, ma grazie intanto... ci provo
    W la Ferari effetrenavenave!
    il computer è un somaro veloce! (neanche tanto ndr)

  4. #4
    Utente di HTML.it L'avatar di supermac
    Registrato dal
    Jun 2001
    Messaggi
    1,856
    Col JS è un'impresa....
    Avrei trovato questa http://www.vbi.org/Items/article.asp?id=133 (con anche il grasso che cola) che ho convertito per lavorare con le stringhe....
    codice:
        Public Shared Sub Permutate(ByVal ArrayCount As Long, ByRef Elements() As String, ByRef Order() As String, ByRef Orders As Collection)
            'ArrayCount is the number of elements in the original array to permutate, 
            'Elements() Is the array To permutate (remember, this will grow shorter As we work, so the ArrayCount parameter cannot be deduced from the length Of Elements). 
            'Order Is the temporary array where we store one permutation, And 
            'Orders Is the Collection where we store all permutations found. 
            Dim Position As Long
            Dim Element As String
            Dim i As Long
            Dim ArrayLen As Long
    
            ' The length of the Elements array. We need this
            ' for our calculations later on.
            ArrayLen = (UBound(Elements) - LBound(Elements) + 1)
    
            ' Position in the Order array of the first element in
            ' the permutated arrays.
            '
            ' Example: Given the array(a,b,c,d), where we want to permutate
            ' (b,c,d), the position in the new array for the first element
            ' will be 2 (since (a) will take up the first position).
            ' Likewise, when we permutate (c,d), the position of the first
            ' element will be 3, since the first two spots are taken by
            ' (a,b).
    
            Position = ArrayCount - ArrayLen + 1
    
            If ArrayLen = 1 Then
                ' The most primitive array we will permutate.
                ' The result is the array itself, and the result
                ' is inserted in the last position of the Order array.
                Order(Position) = Elements(LBound(Elements))
    
                ' This Order is now complete, since the final element has
                ' been filled in.
                Orders.Add(Order)
            Else
                ' The permutation of Elements is each distinct Element
                ' + all permutations of the remaining elements.
                For i = LBound(Elements) To UBound(Elements)
                    Element = Elements(i)
                    Order(Position) = Element
                    Permutate(ArrayCount, RemoveFromArray(Elements, Element), Order, Orders)
                Next i
    
            End If
    
        End Sub
    
        Public Shared Function RemoveFromArray(ByRef Elements() As String, ByVal Element As String) As String()
    
            Dim NewArray() As String
            Dim i As Long
            Dim newi As Long
    
            ' Will create a new array where Element has been left out.
    
            'ReDim NewArray(LBound(Elements) To UBound(Elements) - 1)
            ReDim NewArray(0 To UBound(Elements) - 1)
            For i = LBound(Elements) To UBound(Elements)
                If Elements(i) <> Element Then
                    newi = newi + 1
                    NewArray(newi) = Elements(i)
                End If
            Next
    
            RemoveFromArray = NewArray
    
        End Function
    tranne che non riesco a farla funzionare perchè mi va in indice oltre i limiti dell'array... mi aiutate?
    W la Ferari effetrenavenave!
    il computer è un somaro veloce! (neanche tanto ndr)

  5. #5
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    vai in debug e vedi cosa è .... ci vuole una prova

  6. #6
    Utente di HTML.it L'avatar di supermac
    Registrato dal
    Jun 2001
    Messaggi
    1,856
    Ho risolto con questa funzioncina ricorsiva:

    Public Shared Sub Permuta(ByVal Radice As String, ByVal combi As Integer, ByRef Risultati As List(Of String), ByVal inputarray As String())
    For Each voce As String In data_array
    If Trim(Radice).Contains(Trim(voce)) Then
    Continue For
    End If

    If combi <= 1 Then
    Risultati.Add(Radice + " " + voce)
    Else
    Permuta(Radice + " " + voce, combi - 1, Risultati, inputarray )
    End If
    Next
    End Sub

    dove radice è una stringa che inizialmente è vuota, combi è il numero delle parole da permutare (lo limito a max 4 anche se le parole sono di più per non allungare i tempi di elaborazione, vuol dire che otterrò permutazioni composte da max 4 parole), Risultati è l'array delle permutazioni generate in output e inputarray è l'array delle parole che devono essere permutate.
    Non so se matematicamente sia corretto, completo ed esaustivo ma a me pare faccia quello che mi serve per cui mi accontento :-)
    W la Ferari effetrenavenave!
    il computer è un somaro veloce! (neanche tanto ndr)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.