วงเล็บลำดับตามลำดับคำในพจนานุกรม


9

ท้าทายถ่ายจากที่นี่และที่นี่ด้วย

nลำดับวงเล็บประกอบด้วยn (และn ) s

ลำดับวงเล็บที่ถูกต้องมีการกำหนดดังต่อไปนี้:

คุณสามารถหาวิธีการลบวงเล็บ "()" ที่อยู่ติดกันได้ซ้ำจนกว่ามันจะว่างเปล่า

ตัวอย่างเช่น(())เป็นวงเล็บที่ถูกต้องคุณสามารถลบคู่ในตำแหน่งที่ 2 และ 3 และมันจะกลายเป็น()แล้วคุณสามารถทำให้มันว่างเปล่า )()(ไม่ใช่วงเล็บที่ถูกต้องหลังจากที่คุณลบคู่ในตำแหน่งที่ 2 และ 3 มันจะกลายเป็น)(และคุณไม่สามารถลบอีกต่อไป

งาน

รับตัวเลขnคุณต้องสร้างลำดับวงเล็บที่ถูกต้องทั้งหมดตามลำดับพจนานุกรม

เอาต์พุตสามารถเป็นอาร์เรย์รายการหรือสตริง (ในกรณีนี้คือลำดับต่อบรรทัด)

คุณสามารถใช้คู่ที่แตกต่างกันของวงเล็บเช่น{}, [], ()หรือเข้าสู่ระบบเปิดปิดใด ๆ

ตัวอย่าง

  • n = 3

    ((()))    
    (()())    
    (())()    
    ()(())    
    ()()()
    
  • n = 2

    (())
    ()()
    

@ โจกิ้งใช่แน่นอน ฉันคิดว่าจะไม่สร้างความแตกต่างใด ๆ กับแนวคิดหลักของความท้าทาย
Luis felipe De jesus Munoz

เอ๊ะฉันสามารถนึกถึงภาษาไม่กี่ภาษาที่การตีความจะตีความต่างออกไป
โจคิง

1
ที่เกี่ยวข้อง: หมายเลขคาตาลัน (ผลลัพธ์ของความท้าทายนั้น = จำนวนบรรทัดผลลัพธ์ของความท้าทายนี้)
user202729

3
เหมือนกันแต่มีข้อ จำกัด แปลก ๆ บางอย่างเช่น "คุณอาจไม่เขียนฟังก์ชันแบบเรียกซ้ำ" /// A superset ของความท้าทายนี้ (อนุญาตให้ทุกวงเล็บ Brain-Flak)
user202729

"อาร์เรย์รายการหรือสตริง" "ของลำดับ" ของ "เครื่องหมายเปิดปิดใด ๆ " หมายความว่าเราสามารถแสดงรายการของรายการจำนวนเต็มสองจำนวน (เช่น1s และ-1s) ได้หรือไม่
Jonathan Allan

คำตอบ:


8

Perl 6 , 36 ไบต์

{grep {try !.EVAL},[X~] <[ ]>xx$_*2}

ลองออนไลน์!

ค้นหาชุดค่าผสมที่เรียงตามพจนานุกรมทั้งหมด s และกรองค่าที่ถูกต้อง โปรดทราบว่าชุดค่าผสมที่ถูกต้องทั้งหมด (แม้จะคล้าย) ประเมินเป็น(ซึ่งเป็นเท็จ แต่เรามัน ( ) เพื่อแยกความแตกต่างจากการกลับมา)2n []EVAL[][][]not!tryNil

คำอธิบาย:

{                                  }  # Anonymous code block
                        <[ ]>         # Create a list of ("[", "]")
                             xx$_*2   # Duplicate this 2n times
                   [X~]               # Find all possible combinations
 grep {          },                   # Filter from these
            .EVAL                     # The EVAL'd strings
       try !                          # That do not throw an error

3
ถ้าใครอยากรู้อยากเห็น[][]คือชิ้นส่วนเซนของอาเรย์ที่ว่างซึ่งให้ผลอาเรย์นั้น ชิ้นที่สามารถนำมาใช้หลายครั้งเพื่อประเมิน[][][][]... []นอกจากนี้[[]]อย่าสร้างอาร์เรย์ที่ซ้อนกัน แต่เป็นอาร์เรย์ว่างเนื่องจากกฎข้อโต้แย้งเดียว (คุณต้องเขียน[[],]สำหรับอาร์เรย์ที่ซ้อนกัน) ดังนั้นลำดับที่สมดุลใด ๆ ของ[]วงเล็บจะส่งผลให้เกิดอาเรย์ที่ว่างเปล่าซึ่งทำให้เป็นเท็จ
nwellnhof

6

R , 112 107 99 99 ไบต์

วิธีการแบบไม่เรียกซ้ำ เราใช้ "<" และ ">" เพราะมันหลีกเลี่ยงตัวละครใน regex เพื่อให้เราใช้ข้อมูลจำเพาะที่สั้นลงสำหรับช่วง ASCII เราสร้างสตริงอักขระ 3 ^ 2n 2n 2 ตัวของ "<", "=" และ ">" โดยใช้expand.grid(ผ่านรหัส ASCII 60, 61 และ 62) จากนั้นไปที่ ดูว่าชุดค่าผสมใดให้สมดุลเปิดและปิดวงเล็บ ความเป็นไปได้ "=" จะถูกละเว้นแน่นอน

ผ่านhttp://rachbelaid.com/recursive-regular-experession/

function(n)sort(grep("^(<(?1)*>)(?1)*$",apply(expand.grid(rep(list(60:62),2*n)),1,intToUtf8),,T,T))

ลองออนไลน์!

คำอธิบาย

"^(<(?1)*>)(?1)*$" = regex for balanced <> with no other characters
^ # match a start of the string
  ( # start of expression 1
    < # open <>
       (?1)* # optional repeat of any number of expression 1 (recursive)
  # allows match for parentheses like (()()())(()) where ?1 is (\\((?1)*\\))
    > # close <>
  ) # end of expression 1
  (?1)* # optional repeat of any number of expression 1
$ # end of string

function(n)
  sort(
    grep("^(<(?1)*>)(?1)*$", # search for regular expression matching open and close brackets
      apply(
        expand.grid(rep(list(60:62),2*n)) # generate 3^(2n) 60,61,62 combinations
      ,1,intToUtf8) # turn into all 3^(2n) combinations of "<","=",">"
    ,,T,T) # return the values that match the regex, so "=" gets ignored
  ) # sort them

R , 107 ไบต์

วิธีการเรียกซ้ำปกติ

-1 ขอบคุณ @Giuseppe

f=function(n,x=0:1)`if`(n,sort(unique(unlist(Map(f,n-1,lapply(seq(x),append,x=x,v=0:1))))),intToUtf8(x+40))

ลองออนไลน์!


1
อาฉันพยายามหาMapกอล์ฟ แต่ไม่สามารถก้มศีรษะไปรอบ ๆ มันได้ ฉันไม่มั่นใจว่าparse+ evalจะทำงานได้ตั้งแต่()()และข้อผิดพลาดในการโยนที่ไม่เหมาะสม
Giuseppe

4

C (gcc) , 114 ไบต์

f(n,x,s,a,i){char b[99]={};n*=2;for(x=1<<n;x--;s|a<0||puts(b))for(s=a=i=0;i<n;)a|=s+=2*(b[n-i]=41-(x>>i++&1))-81;}

ลองออนไลน์!

ควรทำงานกับ n <= 15

คำอธิบาย

f(n,x,s,a,i){
  char b[99]={};   // Output buffer initialized with zeros.
  n*=2;            // Double n.
  for(x=1<<n;x--;  // Loop from x=2**n-1 to 0, x is a bit field
                   // where 1 represents '(' and 0 represents ')'.
                   // This guarantees lexicographical order.
      s|a<0||puts(b))  // Print buffer if s==0 (as many opening as
                       // closing parens) and a>=0 (number of open
                       // parens never drops below zero).
    for(s=a=i=0;i<n;)  // Loop from i=0 to n-1, note that we're
                       // traversing the bit field right-to-left.
      a|=              // a is the or-sum of all intermediate sums,
                       // it becomes negative whenever an intermediate
                       // sum is negative.
        s+=            // s is the number of closing parens minus the
                       // number of opening parens.
                        x>>i++&1   // Isolate current bit and inc i.
                    41-(        )  // ASCII code of paren, a one bit
                                   // yields 40 '(', a zero bit 41 ')'.
             b[n-i]=               // Store ASCII code in buffer.
          2*(                    )-81;  // 1 for ')' and -1 for '(' since
                                        // we're going right-to-left.
}

4

Python 2 , 91 88 84 81 ไบต์

f=lambda n:n and sorted({a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)})or['']

ลองออนไลน์!

-3 ไบต์ขอบคุณ pizzapants184


ใช้งานได้กับ Python 3 ด้วยฉันคิดว่า
SuperStormer

คุณสามารถแทนที่set(...)ด้วยชุดความเข้าใจ ( {...}) เป็น -3 ไบต์ลองออนไลน์!
pizzapants184

@ pizzapants184 ขอบคุณ :)
TFeld

3

05AB1E , 13 ไบต์

„()©s·ãʒ®õ:õQ

ลองมันออนไลน์หรือตรวจสอบกรณีทดสอบบางมากขึ้น

คำอธิบาย:

„()            # Push string "()"
   ©           # Store it in the register without popping
    s·         # Swap to get the (implicit) input, and double it
      ã        # Cartesian product that many times
       ʒ       # Filter it by:
        ®      #  Push the "()" from the register
         õ:    #  Infinite replacement with an empty string
           õQ  #  And only keep those which are empty after the infinite replacement


3

Japt 15 15ไบต์

ç>i<)á Ôke/<>

ลองมัน


คำอธิบาย

ç                 :Input times repeat the following string
 >i<              :  ">" prepended with "<"
    )             :End repeat
     á            :Get unique permutations
       Ô          :Reverse
        k         :Remove any that return true (non-empty string)
         e/<>     :  Recursively replace Regex /<>/

3

K (ngn / k) , 36 35 ไบต์

{"()"(&/-1<+\1-2*)#(x=+/)#+!2|&2*x}

ลองออนไลน์!

+!2|&2*x เวกเตอร์ไบนารีทั้งหมดที่มีความยาว 2 * n

(x=+/)# เฉพาะผู้ที่รวมถึง n

(&/-1<+\1-2*)# เฉพาะผู้ที่มีผลรวมบางส่วนซึ่งถือว่า 0/1 เป็น 1 / -1 จะไม่มีค่าลบ

"()" ใช้ 0/1 เป็นดัชนีในสตริงนี้



2

Perl 6 , 42 ไบต์

{grep {!S:g/\(<~~>*\)//},[X~] <( )>xx$_*2}

ลองออนไลน์!

ใช้ regex แบบเรียกซ้ำ การทดแทนทางเลือก:S/[\(<~~>\)]*//

38 ไบต์ด้วย 0 และ 1 เป็นสัญลักษณ์เปิด / ปิด:

{grep {!S:g/0<~~>*1//},[X~] ^2 xx$_*2}

ลองออนไลน์!

คำอธิบาย

{                                        }  # Anonymous block
                              <( )>         # List "(", ")"
                                   xx$_*2   # repeated 2n times
                         [X~]  # Cartesian product with string concat
                               # yields all strings of length 2n
                               # consisting of ( and )
 grep {                },  # Filter strings
        S:g/             # Globally replace regex match
            \(           #   literal (
              <~~>       #   whole regex matched recursively
                  *      #   zero or more times
                   \)    #   literal )
                     //  # with empty string
       !                 # Is empty string?

2

เรติน่า 0.8.2 , 50 ไบต์

.+
$*
1
11
+%1`1
<$'¶$`>
Gm`^(?<-1>(<)*>)*$(?(1).)

ลองออนไลน์! ใช้<>s คำอธิบาย:

.+
$*

แปลงเป็นเอก

1
11

เพิ่มผลลัพธ์เป็นสองเท่า

+%1`1
<$'¶$`>

แจกแจงทั้งหมดของ2²ⁿเลขฐานสอง 2n <>บิตแมปตัวเลขไป

Gm`^(?<-1>(<)*>)*$(?(1).)

เก็บลำดับที่สมดุลเท่านั้น วิธีนี้ใช้เคล็ดลับวงเล็บสมดุลที่ค้นพบโดย @MartinEnder



2

สีแดง , 214, 184 136 ไบต์

func[n][g: func[b n][either n = length? b[if not error? try[load b][print b]return 0][g append copy b"["n g append copy b"]"n]]g""2 * n]

ลองออนไลน์!

ใช้วิธีการของ Jo King ค้นหาการจัดเรียงที่เป็นไปได้ทั้งหมดของวงเล็บโดยใช้การเรียกซ้ำ (จะถูกสร้างขึ้นตามลำดับพจนานุกรม) และพิมพ์หากการจัดเรียงประเมินว่าเป็นบล็อกที่ถูกต้อง


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.