Compatta la stringa della query

Sep 29 2020

introduzione

Proprio sulle notizie di Hacker, John Resig pensa di trasformare una query "foo = 1 & foo = 2 & foo = 3 & blah = a & blah = b" in una simile: "foo = 1,2,3 & blah = a, b", https://johnresig.com/blog/search-and-dont-replace/. Afferma di "essere 10 righe più corte della soluzione di Mark".

Le stringhe di query sono costituite da sequenze di coppie nome-valore. Le coppie nome-valore sono costituite da un nome e un valore, separati da =. nomi e valori sono possibilmente sequenze vuote di caratteri alfanumerici. Le coppie nome-valore sono separate dal carattere & nella sequenza. I valori sono univoci per ogni nome.

Sfida

10 righe più corte della soluzione di Mark non sono sufficienti.

  • Leggi una stringa di query.
  • Combina coppie di valori nome con lo stesso nome in un'unica coppia di valori nome, con valori concatenati e separati da virgola.
  • Emette la stringa di query combinata, con le coppie nome-valore nell'ordine in cui i nomi si trovano da sinistra a destra nella stringa di query.

Questo è il codice del golf, le feritoie standard sono chiuse.

Input e output di esempio

Ingresso:

foo = 1 & foo = & blah = a & foo = 3 & bar = x & blah = b & = 1 & = 2

Produzione:

foo = 1,, 3 & blah = a, b & bar = x & = 1,2

Ingresso:

foo = bar & foo = foo

Produzione:

foo = bar, foo

Risposte

4 KevinCruijssen Oct 01 2020 at 13:34

05AB1E , 23 byte

'&¡'=δ¡.¡н}εø€Ù}…,=&vyý

Provalo online o verifica tutti i casi di test .

Spiegazione:

'&¡                     '# Split the (implicit) input-string on "&"
     δ                   # For each inner string:
   '= ¡                 '#  Split it on "="
       .¡ }              # Group all pairs by:
         н               #  Their first value
           ε             # Map over each group of pairs:
            ø            #  Zip/transpose, swapping rows/columns
             €           #  For both inner lists:
              Ù          #   Uniquify it
               }…,=&     # After the map: push string ",=&"
                    v    # Pop and loop over each character `y`:
                     yý  #  Join the inner-most list of strings with `y` as delimiter
                         # (after the loop, the result is output implicitly)

Provalo online con un output passo passo.

12 Arnauld Sep 29 2020 at 21:32

JavaScript (ES6),  103 99 87  86 byte

Salvato 1 byte grazie a @MatthewJensen

s=>Object.values(o=/(\w*=)(\w*)/g,s.replace(o,(s,k,v)=>o[k]=o[k]?o[k]+[,v]:s)).join`&`

Provalo online!

Commentato

s =>                  // s = query string
  Object.values(      // get the values of ...
    o =               //   ... the object of this regular expression, which is
      /(\w*=)(\w*)/g, //   re-used to store the keys and values of the query
    s.replace(        //   match each key with the '=' sign and the corresponding
      o,              //   value, using the regular expression defined above
      (s, k, v) =>    //   for each matched string s, key k and value v:
        o[k] =        //     update o[k]:
          o[k] ?      //       if it's already defined:
            o[k] +    //         get the current value
            [, v]     //         append a comma, followed by v
          :           //       else:
            s         //         set it to the entire matched string
                      //         (key, '=', first value)
    )                 //   end of replace()
  ).join`&`           // end of Object.values(); join with '&'
10 lucasreta Sep 29 2020 at 20:43

JavaScript, 263 201 196 194 190 189 188 173 161 byte

q=>{w={};q.split`&`.map(x=>{y=x.split`=`;if(!w[y[0]])w[y[0]]=[];w[y[0]].push(y[1])});z=`${Object.entries(w).map(a=>a=[a[0]+'='+a[1].join`,`]).join`&`}`;return z}

Provalo online

7 mschauer Sep 29 2020 at 21:46

Julia 1.0 , 106 byte

f(a,z=split.(split(a,'&'),'='),u=first.(z))=join((i*'='*join(last.(z)[u.==i],',') for i in unique(u)),"&")

Provalo online!

7 HyperNeutrino Sep 29 2020 at 20:59

Python 3.8 (pre-rilascio) , 116 byte

lambda s:(a:=[k.split("=")for k in s.split("&")])and"&".join(b+"="+",".join(d for c,d in a if c==b)for b in dict(a))

Provalo online!

-46 byte grazie a ovs

-1 byte grazie a Jonathan Allan (in Py 3.8 PR, con walrus)

7 Jonah Sep 29 2020 at 22:11

Ruby , 88 80 76 64 byte

->s{s.scan(/(\w*=)(\w*)/).group_by(&:shift).map{|k,v|k+v*?,}*?&}

Provalo online!

-8 byte grazie a ovs per il puntamento potrei assegnare un lambda a una variabile

-12 byte grazie a Dingus!

5 Sisyphus Sep 30 2020 at 01:38

Python 3 , 94 byte

lambda s:'&'.join(k+'='+",".join(v)for k,v in parse_qs(s,1).items())
from urllib.parse import*

Provalo online!

Poche cose:

  • Funziona solo perché Python 3.6+ mantiene l'ordine del dizionario in base all'ora di inserimento. Ci piacerebbe usare Python 2 per un'importazione più breve ( urlparsevs urllib.parse) ma i dizionari non sono ordinati correttamente.
  • Per quanto io ami le corde in f, k+'='+",".join(v)è più corto di f'{k}={",".join(v)}'.
  • Il doppio .joinsembra odore di golf, ma non riesco a trovare un modo più breve.

Python 3 , 95 byte

from urllib.parse import*
d=parse_qs(input(),1)
print('&'.join(k+'='+','.join(d[k])for k in d))

Provalo online!

5 JonathanAllan Sep 29 2020 at 21:54

Gelatina , 28 byte

-2 Grazie a Zgarb!

ṣ”&ṣ€”=Ṗ€ĠṢịƲµZQ€j€”,j”=)j”&

Un collegamento monadico che accetta e fornisce un elenco di caratteri.

Provalo online!

Come?

ṣ”&ṣ€”=Ṗ€ĠṢịƲµZQ€j€”,j”=)j”& - Link: list of characters, S
ṣ”&                          - split at '&'s
   ṣ€”=                      - split each at '='s
                               call that A
            Ʋ                - last four links as a monad - f(A):
       Ṗ€                    -   all but last of each
         Ġ                   -   group indices by their values
          Ṣ                  -   sort (since Ġ orders by the values, not the indices)
           ị                 -   index into (A) (vectorises)
             µ          )    - for each:
              Z              -   transpose
               Q€            -   deduplicate each
                 j€”,        -   join each with ','s
                     j”=     -   join with '='s
                         j”& - join with '&'s
5 SomoKRoceS Sep 29 2020 at 22:13

05AB1E , 62 51 byte

(-11 da @kevin)

'&¡'=δ¡D€нÙÐV_UsvYyнk©Xsèyθ',««Xs®ǝU}X妨}Yζí'=ý'&ý

Provalo online!


Il mio 62 approccio:

'&¡ε'=¡}D€нÙ©DgÅ0Usvy¬®skDVXsèyθ',««XsYǝU}X妨}®ζεć'=s««˜}˜'&ý

Spiegazione:

'&¡ε'=¡}D€нÙ©DgÅ0Usvy¬®skDVXsèyθ',««XsYǝU}X妨}®ζεć'=s««˜}˜'&ý
'&¡                                                              split by &
   ε'=¡}                                                         foreach: split by =
        D                                                        duplicate
         €н                                                      foreach: push header (get the keys list)
           Ù                                                     uniquify
            ©                                                    save in register c
             Dg                                                  suplicate and get the length of that list of keys
               Å0                                                create a list of 0's with the length above
                 U                                               save in variable X
                  svy                    }                       for each set of key-value
                     ¨sk                                        find the index of that key in the keys list
                         DV                                      save the index in variable y
                           Xsè                                   get the current value of the element of X at index Y (in X we are keeping the concatenation of the values for key i)
                              yθ                                 extract the tail of the element in this iteration (a value to concatenate)
                                ',««                             concatenate with , in between
                                    XsYǝU                        update X with the new value of the element representing the key
                                          X妨}                  remove tail and head from each element of X (removing the trailing , and leading 0)
                                               ®                 push back the list of keys
                                                ζ                zip (list of keys and list of merged values)
                                                 εć'=s««˜}       foreach element in the zipped list, join with = in between such that the result is "key=values"
                                                          ˜      flat
                                                           '&ý   join with &

Provalo online!

5 Neil Sep 29 2020 at 22:41

JavaScript, 79 byte

f=
s=>(s=new URLSearchParams(s)).forEach((v,k)=>s.set(k,s.getAll(k)))||unescape(s)
<input oninput=o.textContent=f(this.value)><pre id=o>

Se l'I / O potesse essere stringhe di query effettive in base alla specifica WHATWG, piuttosto che stringhe non valide che assomigliano a stringhe di query ma non sono correttamente codificate in URL, allora 7 byte potrebbero essere salvati stringendo il risultato invece di eliminarlo.

4 Shaggy Sep 29 2020 at 21:17

JavaScript, 106 104 76 byte

Soluzione Retina di Port of Neil , pubblicata con autorizzazione.

f=q=>q==(q=q.replace(/(?<=^|&)((\w*=)[^&]*)(.*?)&\2(\w*)/,`$1,$4$3`))?q:f(q)

Provalo online!

Originale

Un po 'di golf da ubriaco su cui sono tornato a lavorare da sobrio, ma nel processo ho individuato la soluzione di Arnauld e ho capito che ero sulla strada per qualcosa di quasi identico, quindi l'ho lasciato così com'era.

q=>Object.keys(o={},q.split`&`.map(p=>o[[k,v]=p.split`=`,k]=[...o[k]||[],v])).map(x=>x+`=`+o[x]).join`&`

Provalo online!

3 Neil Sep 30 2020 at 15:23

Retina 0.8.2 , 45 byte

+1`(?<=^|&)((\w*=)[^&]*)(.*?)&\2(\w*)
$1,$4$3

Provalo online! Spiegazione: Fa corrispondere ripetutamente la prima chiave duplicata e il suo primo duplicato e unisce il valore a quello della chiave originale.

3 Shaggy Sep 30 2020 at 15:52

Japt , 29 28 27 26 24 byte

q& móÈk¶
ü@bøXÎîÕvÎqÃq&

Provalo

Salvato 2 byte grazie a qualche ispirazione da Kevin .

(Oh, se solo Japt avesse un metodo solo per il raggruppamento, piuttosto che anche per l'ordinamento, questo potrebbe essere 19 byte .)

Explnataion

q& móÈk¥\nü@bøXÎîÕvÎqÃq&     :Implicit input of string U
q&                            :Split on "&"
   m                          :Map
    ó                         :  Partition after each character that returns falsey (i.e., an empty string)
     È                        :  When passed through the following function
      k                       :    Remove all characters that appear in
       ¥                      :    Literal "=="
        \n                    :Reassign to U
          ü                   :Group & sort by
           @                  :Passing each X through the following function
            b                 :  First index in U
             ø                :  That contains
              XÎ              :    First element of X
                Ã             :End grouping
                 ®            :Map each Z
                  Õ           :  Transpose
                   v          :  Map first element to
                    Î         :    Get first element
                     q        :  Join resulting array
                      Ã       :End map
                       q&     :Join with "&"

Oppure, per fornire una procedura dettagliata:

Ingresso

"foo=1&foo=&blah=a&foo=3&bar=x&blah=b&=1&=2"

Diviso

["foo=1","foo=","blah=a","foo=3","bar=x","blah=b","=1","=2"]

Mappa e partizione

[["foo=","1"],["foo="],["blah=","a"],["foo=","3"],["bar=","x"],["blah=","b"],["=","1"],["=","2"]]

Raggruppa e ordina

[[["foo=","1"],["foo="],["foo=","3"]],[["blah=","a"],["blah=","b"]],[["bar=","x"]],[["=","1"],["=","2"]]]

Mappa e ...

Trasporre

[[["foo=","foo=","foo="],["1",null,"3"]],[["blah=","blah="],["a","b"]],[["bar="],["x"]],[["=","="],["1","2"]]]

Mappa il primo elemento sul suo primo elemento

[["foo=",["1",null,"3"]],["blah=",["a","b"]],["bar=",["x"]],["=",["1","2"]]]

Aderire

["foo=1,,3","blah=a,b","bar=x","=1,2"]

Aderire

"foo=1,,3&blah=a,b&bar=x&=1,2"
2 Noodle9 Sep 30 2020 at 11:38

Awk , 144 \$\cdots\$144 138 byte

Aggiunti 6 byte per correggere un bug.
Aggiunti 4 byte per correggere un bug gentilmente segnalato e risolto da Dominic van Essen .
Salvati 6 byte grazie a Dominic van Essen !!!

BEGIN{RS="&"}{if(!(j=i[$1]++))n[m++]=$1;a[$1][j]=$2}END{for(l in n){k=n[l];o=k"=";for(j in a[k])o=o (j>0?",":"")a[k][j];printf z o;z="&"}}

Provalo online!

2 CongChen Sep 30 2020 at 13:48

R , 195 byte

{Z=pryr::f
`/`=Z(a,b,el(regmatches(a,gregexpr(b,a))))
`-`=Z(a,b,paste(a,collapse=b))
Z(S,{L=S/'\\w*='
L=factor(L,unique(L))
U=tapply(S/'=\\w*',L,Z(a,trimws(a,,'=')-','))
paste0(names(U),U)-'&'})}

Provalo online!

2 valsaysReinstateMonica Sep 30 2020 at 18:15

Lua , 162 byte

l,t={},{}(...):gsub('(%w-)=(%w-)',load"k,v=...o=t[k]l[#l+1]=not o and k or _ t[k]=o and o..','..v or v")for i=1,#l do io.write(i>1 and'&'or'',l[i],'=',t[l[i]])end

Provalo online!

Questo è lungo, eh. Avrebbe potuto essere molto più breve se non fosse stato per l'ordinazione.

Spiegazione:

l,t={},{} -- list (by first inclusion), table (keys to string)
-- (ab)use string replacement function to callback over matches in input string
(...):gsub(
    -- Match key-value pairs
    '(%w-)=(%w-)',
    -- For each pair, run callback (braces are replaced for multiline)
    load[[
        k,v=... -- Assign key, value
        o=t[k] -- Look for already stored string if any
        l[#l+1]=not o and k or _ -- If key is new, store it in list
        t[k]=o and o..','..v or v -- Append to string if it is not new, store it if it is
    ]]
)
-- For every record in list
for i=1,#l do
    -- Write with no newlines
    io.write(
        i>1 and'&'or'', -- Output & before all values but first
        l[i],'=',t[l[i]] -- Print key-value pair
    )
end
2 LegionMammal978 Oct 01 2020 at 13:02

Wolfram Language (Mathematica) , 86 byte

StringRiffle[Last@Reap[Sow@@@StringExtract[#,"&"->;;,"="->{2,1}],_,List],"&","=",","]&

Provalo online! Funzione pura, accetta una stringa come input e restituisce un'altra stringa come output. Questo è ispirato e molto simile alla risposta di att , ma utilizza un algoritmo simile a quello nel post del blog (qui utilizzando Sow/ Reap). Ecco un esempio di come le sottoespressioni valutano su un input di "foo=1&bar=a&foo=":

StringExtract[#,"&"->;;,"="->{2,1}] == {{"1", "foo"}, {"a", "bar"}, {"", "foo"}}
Sow@@@...                           == {"1", "a", ""}; {"1", ""} sown w/ tag "foo"; {"a"} sown w/ tag "bar"
Reap[...,_,List]                    == {{"1", "a", ""}, {{"foo", {"1", ""}}, {"bar", {"a"}}}}
Last@...                            == {{"foo", {"1", ""}}, {"bar", {"a"}}}
StringRiffle[...,"&","=",","]       == "foo=1,&bar=a"
2 GalenIvanov Oct 01 2020 at 18:37

Fattore , 229 byte

: c ( s -- s s ) 1 <hashtable> swap "&"split [ "="split ] map
[ [ dup [ last ] dip first pick push-at ] each ]
[ [ first ] map dup union ] bi dup [ [ over at ","join ] map ] dip
[ "="append ] map swap zip [ concat ] map "&"join ;

Provalo online!

È lungo ma ne sono un po 'contento :)

2 Giuseppe Sep 30 2020 at 16:00

SNOBOL4 (CSNOBOL4) , 243 221 212 byte

	Q =INPUT
	T =TABLE()
N	Q (ARB '=') . L ARB . V ('&' | RPOS(0)) REM . Q	:F(O)
	T<L> =T<L> ',' V 	:(N)
O	R =CONVERT(T,'ARRAY')
I	X =X + 1
	R<X,2> ',' REM . V 	:F(P)
	O =O '&' R<X,1> V	:(I)
P	O '&' REM . OUTPUT
END

Provalo online!

TABLEin SNOBOL è strano. Va benissimo accettare un PATTERNlike ARBcome chiave, ma non la stringa vuota ''. Tuttavia, l'utilizzo <label>=come etichetta invece di <label>risolve perfettamente questo problema.

Spiegazione per un'iterazione precedente:

	E =RPOS(0)					;* alias for end of string 
	A =ARB						;* alias for ARBitrary match (as short as possible)
	Q =INPUT					;* read input
	T =TABLE()					;* create a TABLE (a dictionary)
N	Q A . L '=' A . V ('&' | E) REM . Q	:F(O)	;* in regex land, this is something like
	;* '(.*=)(.*)(&|$)' where you save \1 and \2 as L and V, respectively. If there's no match, goto O
	T<L> =T<L> V ','	:(N)		;* update the values list, then goto N
O	R =CONVERT(T,'ARRAY')				;* convert T to a 2D array of [Label,Value]
I	X =X + 1					;* increment array index
	R<X,2> A . V ',' E	:F(P)			;* remove the trailing ',' from the value list. If X is out of bounds, goto P
	O =O R<X,1> V '&'	:(I)			;* Append L and V to O with an '=' and '&', then goto I
P	O A . OUTPUT '&' E				;* Print everything except for the trailing '&'
END
2 Neil Oct 09 2020 at 12:50

PHP , 146 byte

<?=parse_str(str_replace('=','_[]=',$argv[1]),$a)??join('&',array_map(function($b,$c){return rtrim($b,'_').'='.join(',',$c);},array_keys($a),$a));

Provalo online! Spiegazione: parse_strnon è stato progettato per gestire valori ripetuti, ma puoi convincerlo a nominare ogni valore con un finale []. Inoltre non è stato progettato per gestire nomi vuoti, ma poiché sto aggiungendo []comunque posso anche aggiungere un _per soddisfare quel caso. Dopo aver analizzato la stringa di query, resta da ricongiungere tutto.

1 GalenIvanov Sep 30 2020 at 11:34

Rosso , 221 byte

func[s][m: copy #()s: split s"&"forall s[s/1: split s/1"="append s/1/1"="put m s/1/1
copy""]foreach v s[m/(v/1): append m/(v/1) rejoin[v/2","]]t: copy""foreach k keys-of
m[take/last m/:k repend t[k m/:k"&"]]take/last t t]

Provalo online!

Questo è terribilmente lungo e proverò a giocarci almeno un po '. Peccato che il rosso non abbia una joinfunzione utile ...

1 Neil Sep 30 2020 at 19:18

Carboncino , 42 byte

≔E⪪S&⪪ι=θW⁻Eθ§κ⁰υ⊞υ§ι⁰⪫Eυ⁺⁺ι=⪫EΦθ¬⌕λι⊟λ,¦&

Provalo online! Il collegamento è alla versione dettagliata del codice. Spiegazione:

≔E⪪S&⪪ι=θ

Dividi l'input su se &dividi ogni token su =s.

W⁻Eθ§κ⁰υ⊞υ§ι⁰

Crea un elenco di chiavi univoche in ordine della loro prima apparizione.

⪫Eυ⁺⁺ι=⪫EΦθ¬⌕λι⊟λ,¦&

Per ogni chiave, estrai e unisci i valori con ,, concatena con la chiave e il separatore e unisci il risultato complessivo con &.

1 ErikF Sep 30 2020 at 05:36

C (gcc) , 319304 byte

Grazie a Ceilingcat per i suggerimenti.

Questa funzione analizza ogni coppia tokenizzata (nome, valore) e aggiunge una voce di elenco per ogni nuovo nome incontrato, quindi aggiunge una voce di elenco per ogni valore. Dopo aver costruito gli elenchi, scorre ogni elenco e stampa i valori. Per risparmiare spazio, ho appiattito le strutture in array di void *.

f(s,t,v,i)char*s,*t;{void*d[3]={t=0},**f,**w;for(;t=strtok(t?0:s,"&");*w=calloc(8,2),w[1]=v){t[i=strcspn(t,"=")]=0;v=t-~i;for(f=&d;strcmp(f[1]?:t,t);f=*f);for(w=f[2]=f[1]?f[2]:(f[1]=t,*f=calloc(8,5))+24;w[1];w=*w);}for(f=&d;i=*f&&printf("&%s="+!!s,f[1]);f=*f)for(w=f[2];s=*w;w=s)i=!printf(",%s"+i,w[1]);}

Provalo online!

Versione non golfistica della proposta originale:

struct list {
  struct list *next;
  char *name;
  struct list *value;
};

void f(char *s) {
  char *tok=NULL, *value;
  struct list d={}, *e, *v;
  int i;

  for(; tok=strtok(tok?NULL:s, "&"); ) {
    tok[i=strcspn(tok, "=")]=0;
    value=tok+i+1;
    for(e=&d; e->name && strcmp(e->name, tok); e=e->next);
    if(!e->name) {
      e->next=calloc(sizeof(struct list), 2);
      e->name=tok;
      e->value=e->next+1;
    }
    for(v=e->value; v->name; v=v->next);
    v->next=calloc(sizeof(struct list), 1);
    v->name=value;
  }
  for(e=&d; e->next; e=e->next, s=0) {
    printf("&%s="+!!s, e->name);
    for(v=e->value, i=1; v->next; v=v->next, i=0)
      printf(",%s"+i, v->name);
  }
}

Provalo online!

1 att Sep 30 2020 at 07:25

Wolfram Language (Mathematica) , 90 88 byte

StringRiffle[S=StringSplit;List@@@Normal@Merge[Rule@@@S[#~S~"&","=",2],#&],"&","=",","]&

Provalo online!

-2 grazie a LegionMammal978

S=StringSplit;
Rule@@@S[#~S~"&","=",2]         (* Convert to a list of Rules *)
Merge[ % ,#&]                   (* Combine rules into an Association, leaving values unchanged *)
Normal@ %                       (* Convert Association back into a list of Rules, *)
List@@@ %                       (* and turn Rules into Lists *)
StringRiffle[ % ,"&","=",","]   (* Concatenate, using "&", "=", and "," as separators *)
1 Chris Sep 29 2020 at 22:03

Perl 5 , -pF\&flag, 73 57 byte

Utilizza per -pF\&eseguire il loop sugli input e attivare lo splittaggio automatico &.

Risultati non ordinati, in concorrenza parallela.

/=/,$z{$`}.=$z{$`}?",$'":"$`=$'"for@F;$_=join'&',values%z

Provalo online!

Utilizza un hash %zper tenere traccia dei valori per i singoli nomi, quindi li stampa tutti alla fine. -16 byte grazie a NahuelFouilleul.

1 DominicvanEssen Sep 30 2020 at 09:26

R , 174166 byte

function(s,S=strsplit,A=sapply,P=paste,e=A(S(P(el(S(s,'&')),'=',sep=''),'='),c),n=unique(m<-e[1,]))P(n,A(n,function(x)P(e[2,m==x],collapse=',')),sep='=',collapse='&')

Provalo online!

Per qualche motivo inspiegabile ho pensato che questa sfida non avrebbe sofferto della gestione delle stringhe terribilmente prolissa di R.
Questo non è risultato essere il caso, almeno sulla base del mio tentativo finora ...

Commentato prima del golf:

compactify=
function(s,                         # s = string to compactify
S=strsplit,                         # S = alias to strsplit() function
A=sapply,                           # A = alias to sapply() function
P=paste,                            # P = alias to paste() function
a=el(                               # a = the first element of ...
  S(s,'&'))                         #  ...s split on '&'
b=S(a,,'=')                         # b = each element of a, split on '='
                                    # Now, unfortunately if there's nothing after the '=',
                                    # the strsplit() function fails to add an empty string ''
                                    # so we need to do this ourselves:
e=A(b,function(x)c(x,'')            # e = for each element of b, add a '' ...  
                        [1:2])      #  ...and then take the first two elements
                                    # This gives us a 2-row matrix, with the names in row 1,
                                    # and the values in row 2
n=unique(m<-e[1,]))                 # n = unique names, m = all names of name-value pairs
m=A(n,function(x)                   # m = for each element of n...
      P(e[2,m==x],collapse=','))    #  ...paste together the values for this name, using ',' as separator
P(n,m,sep='=',collapse='&')         # Finally, paste together the pairs of elements in m, using '=' as separator...
                                    #  ...and collapse them all into one string using '&' as separator
1 Giuseppe Sep 30 2020 at 19:07

R , 162 157 151 byte

function(s,S=substring)paste0(z<-unique(k<-S(x<-el(strsplit(s,"&")),1,y<-regexpr('=',x))),sapply(split(S(x,y+1),k),paste,collapse=',')[z],collapse='&')

Provalo online!

-6 byte grazie a Dominic van Essen

Una bella funzione a riga singola. Ungolfed:

function(s,S=substr){
pairs <- el(strsplit(s,"&"))					# split on '&' getting list of pairs
loc <- regexpr('=',pairs)					# find location of each '=' in each pair 
keys <- substr(pairs,1,loc)					# get the key, including the '='
values <- substr(pairs,loc + 1,nchar(pairs))			# get the values (everything after '=')
unq_keys <- unique(keys)					# uniquify the keys. This retains the order.
split_vals <- split(values,keys)				# group the values into sublists by which key they are associated with
collapsed_values <- sapply(split_vals,paste,collapse=',')	# join each subgroup of values by ','
collapsed_values <- collapsed_values[unq_keys]			# and reorder them to match the order of the keys
paste0(unq_keys,collapsed_values,collapse='&')			# concatenate keys and values and join by '&'
}