Tüm Anagramları ve Subanagramları da bulun!


13

Bu soru büyük ölçüde bu soruya dayanmaktadır , ancak bir takım ek zorluklar içermelidir.

Senin görevin

Bir dize alırken, tüm olası anagramlarını yazdıran bir program veya işlev yazmalısınız. Bu sorunun amacı için bir anagram, orijinal dize ile aynı karakteri içeren ancak orijinal dize olmayan bir dizedir. Bir altanagram, girilen bir dizenin bir alt dizisinin bir anagramıdır. Anagramların ve altanagramların gerçek kelimeler olması veya içermesi gerekmez.

Giriş

Herhangi bir standart giriş yöntemiyle herhangi bir uzunlukta> 0 olabilecek bir dizeyi kabul edebilirsiniz. Herhangi bir ASCII karakteri içerebilir.

Çıktı

Girilen dizgenin tüm olası anagramlarını ve altanagramlarını herhangi bir standart şekilde çıktılayabilirsiniz. Aynı dizeyi iki kez çıkarmamalı veya girdiye eşit bir dize çıkarmamalısınız.

Diğer Kurallar

Standart Loopholes'a izin verilmiyor

puanlama

Bu , en az bayt kazanır.


Boş dize olası bir anagram mı?
Dijital Travma

Orijinal dizenin / dizelerin çıktısına izin veriliyor mu?
CalculatorFeline

@CalculatorFeline "Aynı dizeyi iki kez çıkarmamalı veya girdiye eşit bir dize çıkarmamalısınız."
Jonathan Allan

@DigitalTrauma, " Herhangi bir standart giriş yöntemiyle > 0 herhangi bir uzunlukta olabilen bir dize kabul edebilirsiniz ". (vurgu eklendi)
Gryphon

4
Bazı Test
senaryoları

Yanıtlar:


8

05AB1E , 7 bayt

Œ€œ˜Ù¹K

Girdiden bir dizeyi kabul eden ve yığın üzerinde dizelerin listesini bırakan bir işlev. Tam bir program olarak listenin bir temsili yazdırılır.

Çevrimiçi deneyin!

Nasıl?

        - 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

Ve ... daha kısa bir şeyi başardın.
Gryphon

Aynı algoritma, sadece daha az bayt.
Jonathan Allan

Evet, dil değişikliği her şeydi, ama yine de etkileyiciydi.
Gryphon

@ ais523 Görünüşe göre her ikisi de yanlış yol var!
Jonathan Allan

@ ais523 Düzeltildiğini düşünüyorum.
Jonathan Allan

9

Brachylog (2), 7 bayt

{sp}ᶠdb

Çevrimiçi deneyin!

açıklama

{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)

(2) ne anlama geliyor?
Gryphon

@Gryphon (afaik) branchylog'un 2 versiyonu var, bu V2 kullanıyor.
John Hamilton

1
Tamam, sürüm numarası veya farklı ve muhtemelen yasa dışı bir yöntem kullanarak olası bir bayt sayısı olup olmadığından emin değildi.
Gryphon

1
Bu bana ikinci kez soruldu. Sanırım yazmaya başlamam gerekecek (v2).

7

Jöle , 9 bayt

ẆŒ!€;/QḟW

Bir listeyi kabul eden ve girdinin kendisi dışındaki tüm farklı alt anagramların bir listesini döndüren monadik bir bağlantı.

Çevrimiçi deneyin! (altbilgi, yeni satırlarla birleştirerek elde edilen listeyi güzel bir şekilde yazdırır.)

Nasıl?

ẆŒ!€;/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)

4

Pyth, 12

-{.n.pM.:Q)]

Çevrimiçi test .

         Q       # input
       .: )      # all substrings
    .pM          # all permutations of all substrings
  .n             # flatten
 {               # deduplicate
-          ]Q    # subtract (list of) (implicit) input

@ ais523 Redone - Sanırım şimdi doğru.
Dijital Travma

3

Japt , 10 bayt

à má c â Å

Çevrimiçi deneyin!

Ben kullanmak lazım à, áve âçok sırayla, tek cevap tüm. Ne tesadüf...

açıklama

 à 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

1
Å bile başardın.
Gryphon

1

Mathematica, 60 bayt

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

Permutationspermütasyonlar için kaç giriş değerinin kullanılacağını söyleyen isteğe bağlı bir sayısal argüman alır. Girdinin uzunluğunu verirsek, yinelemesiz girdinin tüm alt kümeleri için permütasyonlar oluşturur. Tek yapmamız gereken girişi kaldırmak.


1

Java 8, 313 312 306 bayt

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));}

Cevabımın değiştirilmiş sürümü , burada p("",s,l);değiştirildifor(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));

Bağlantılı cevabımda @ OlivierGrégoire sayesinde -6 bayt .

Bu bölümün açıklaması:

Burada deneyin.

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)

0

Perl 6 , 75 bayt

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

Dene

Expanded:

{                    # 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)
}
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.