Znajdź wszystkie anagramy i subanagramy!

13

To pytanie jest w dużej mierze oparte na tym pytaniu , ale powinno stanowić szereg dodatkowych trudności.

Twoje zadanie

Musisz napisać program lub funkcję, która po otrzymaniu ciągu wypisze wszystkie możliwe anagramy. Na potrzeby tego pytania anagram jest ciągiem zawierającym ten sam znak, co ciąg oryginalny, ale nie jest ciągiem oryginalnym. Podanagram jest anagramem podciągu wprowadzonego ciągu. Anagramy i podanagramy nie muszą być ani zawierać rzeczywistych słów.

Wejście

Możesz zaakceptować ciąg znaków, który może mieć dowolną długość> 0, dowolną standardową metodą wprowadzania. Może zawierać dowolne znaki ASCII.

Wynik

Możesz wyprowadzić wszystkie możliwe anagramy i podanagramy wprowadzonego ciągu w dowolny standardowy sposób. Nie wolno wyprowadzać tego samego ciągu dwa razy ani wyprowadzać ciągu równego wejściu.

Inne zasady

Standardowe luki są niedozwolone

Punktacja

To jest , najmniej bajtów wygrywa.

Gryf
źródło
Czy pusty ciąg jest możliwym anagramem?
Digital Trauma
Czy dozwolone jest wyświetlanie oryginalnego ciągu / sustrów?
CalculatorFeline
@CalculatorFeline „Nie wolno wyprowadzać tego samego ciągu dwa razy ani wyprowadzać ciągu równego wejściowemu.”
Jonathan Allan
@DigitalTrauma, „Za pomocą dowolnej standardowej metody wprowadzania danych możesz zaakceptować ciąg znaków, który może mieć dowolną długość> 0 ”. (wyróżnienie dodane)
Gryphon
4
Pomocne byłyby niektóre przypadki testowe
Pan Xcoder,

Odpowiedzi:

8

05AB1E , 7 bajtów

Œ€œ˜Ù¹K

Funkcja, która przyjmuje ciąg znaków z wejścia i pozostawia listę ciągów na stosie. Jako pełny program drukowana jest reprezentacja listy.

Wypróbuj online!

W jaki sposób?

        - push input
Π      - all substrings
 €œ     - for €ach: all permutations
   ˜    - flatten
    Ù   - de-duplicate
     ¹  - push 1st input onto top of stack
      K - pop a,b; push a without any b's (remove the copy of the input from the list)
        - as a full program: implicit print of the top of the stack
Jonathan Allan
źródło
I ... udało ci się zrobić coś jeszcze krótszego.
Gryphon
To ten sam algorytm, tylko mniej bajtów.
Jonathan Allan,
Tak, zmiana języka była cała, ale wciąż imponująca.
Gryphon
@ ais523 Wygląda na to, że źle zrozumiałem!
Jonathan Allan,
@ ais523 Myślę, że to naprawiono.
Jonathan Allan
9

Brachylog (2), 7 bajtów

{sp}ᶠdb

Wypróbuj online!

Wyjaśnienie

{sp}ᶠdb
{  }ᶠ    Find all
  p        permutations of
 s         a substring of {the input}
     d   Remove duplicates (leaving the list otherwise in the same order)
      b  Remove the first (the input itself)

źródło
Co oznacza (2)?
Gryphon
@Gryphon (afaik) istnieją 2 wersje branchylog, używa V2.
John Hamilton,
1
Ok, nie byłem pewien, czy jest to numer wersji, czy możliwa liczba bajtów przy użyciu innej, być może nielegalnej metody.
Gryphon
1
To już drugi raz, kiedy mnie pytają. Chyba będę musiał zacząć pisać jako (v2).
7

Galaretka , 9 bajtów

ẆŒ!€;/QḟW

Monadyczny link akceptujący listę i zwracający listę wszystkich odrębnych podanagramów z wyjątkiem samego wejścia.

Wypróbuj online! (stopka ładnie drukuje wynikową listę, łącząc się z nowymi liniami).

W jaki sposób?

ẆŒ!€;/QḟW - Link: list of characters, s
Ẇ         - all contiguous sublists of s
 Œ!€      - all permutations for €ach sublist now a list of lists of lists)
     /    - reduce by:
    ;     -   concatenation (flattens the list by one level)
      Q   - de-duplicate (gets the unique entries)
        W - wrap s in a list (otherwise filtering will attempt to remove characters)
       ḟ  - filter discard from left if in right (remove the one equal to the input)
Jonathan Allan
źródło
4

Pyth, 12

-{.n.pM.:Q)]

Test online .

         Q       # input
       .: )      # all substrings
    .pM          # all permutations of all substrings
  .n             # flatten
 {               # deduplicate
-          ]Q    # subtract (list of) (implicit) input
Cyfrowa trauma
źródło
@ ais523 Redone - Myślę, że teraz jest poprawne.
Cyfrowa trauma
3

Japt , 10 bajtów

à má c â Å

Wypróbuj online!

Muszę użyć à, ái âwszystko w jednej odpowiedzi, również w porządku. Co za zbieg okoliczności...

Wyjaśnienie

 à má c â Å
Uà má c â s1  // Ungolfed
              // Implicit: U = input string
Uà            // Take all combinations of characters in the input string.
   má         // Map each combination to all of its permutations.
      c       // Flatten into a single array.
        â     // Uniquify; remove all duplicates.
          s1  // Remove the first item (the input) from the resulting array.
              // Implicit: output resulting array, separated by commas
ETHprodukcje
źródło
1
Ty też sobie poradziłeś.
Gryphon
1

Mathematica, 60 bajtów

DeleteCases[""<>#&/@Permutations[c=Characters@#,Tr[1^c]],#]&

Permutationspobiera opcjonalny argument liczbowy, który informuje, ile wartości wejściowych należy zastosować do permutacji. Jeśli podamy mu długość danych wejściowych, wygeneruje permutacje dla wszystkich podzbiorów danych wejściowych bez duplikatów. Wszystko, co musimy zrobić, to usunąć dane wejściowe.

Martin Ender
źródło
1

Java 8, 313 312 306 bajtów

import java.util.*;s->{Set l=new HashSet();for(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));l.remove(s);l.forEach(System.out::println);}void p(String p,String s,Set l){int n=s.length(),i=0;if(n<1)l.add(p);else for(;i<n;p(p+s.charAt(i),s.substring(0,i)+s.substring(i+++1,n),l));}

Zmodyfikowana wersja mojej odpowiedzi tutaj , gdzie p("",s,l);została zastąpionafor(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));

-6 bajtów dzięki @ OlivierGrégoire w mojej połączonej odpowiedzi.

Wyjaśnienie tej części:

Wypróbuj tutaj.

for(int l=s.length(),i=0,j;i<l;i++)
                               // Loop (1) from 0 to the length of the String (exclusive)
  for(j=i+1;j<=l;              //  Loop (2) from 1 to the length of the String (exclusive)
    p("",                      //   Call the permutation-method,
    s.substring(i,j+++1),l));  //   with a substring from `i` to `j` (inclusive)
                               //  End of loop (2) (implicit / single-line body)
                               // End of loop (1) (implicit / single-line body)
Kevin Cruijssen
źródło
0

Perl 6 , 75 bajtów

{unique(flat $_,.comb.combinations.skip».permutations.map(*».join)).skip}

Spróbuj

Rozszerzony:

{                    # bare block lambda with implicit parameter 「$_」

  unique(            # return each (sub)anagram once

    flat             # unstructure the following (List of Lists into flat List)
      $_,            # the input (so we can skip it at the end)

      .comb          # split the input into graphemes
      .combinations  # get all the combinations
      .skip\         # skip the first empty combination
      ».permutations # get all the permutations of each combination
      .map(*».join)  # join the inner permutations

  ).skip             # skip the first value (the input)
}
Brad Gilbert b2gills
źródło